我想在我的项目中使用基于xml的配置进行spring-hibernate配置。当打印sessionFactory对象时,它应该返回一个像某个内存地址的值,但是它给出了null值。
我想使用@Autowire注释在bean中注入sessionFactory。 我将使用hibernate托管交易。
我的代码是
spring-config.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation=
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd&#34; &GT;
<context:annotation-config />
<context:property-placeholder location="classpath:application.properties" />
<bean id="dataSource" class="org.apache.tomcat.jdbc.pool.DataSource"
destroy-method="close">
<property name="driverClassName" value="${database.driverClassName}" />
<property name="url" value="${database.url}" />
<property name="username" value="${database.username}" />
<property name="password" value="${database.password}" />
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}
</prop>
<prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
</props>
</property>
</bean>
<bean id="abc" class="pkg.A"/>
</beans>
这是我的界面
Abc.java
package pkg;
public interface Abc {
public void m1();
}
界面实施
A.java
package pkg;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
@Repository
public class A implements Abc {
@Autowired
SessionFactory sessionFactory;
public SessionFactory getSessionFactory() {
System.out.println("sys factory : "+sessionFactory);
return sessionFactory;
}
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
public void m1() {
System.out.println("session factory obj : "+sessionFactory);
}
}
它给我null
tomcat输出:
Jul 21, 2017 8:38:47 PM org.springframework.web.context.ContextLoader initWebApplicationContext
INFO: Root WebApplicationContext: initialization completed in 9610 ms
Jul 21, 2017 8:38:47 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-nio-8082"]
Jul 21, 2017 8:38:47 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-nio-8011"]
Jul 21, 2017 8:38:47 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 24169 ms
session factory obj : null
这是我正在调用sessionfactory对象的jsp文件,如
<%@page import="pkg.A"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h2>H2 heading</h2>
<%
A a = new A();
a.m1();
%>
</body>
</html>
答案 0 :(得分:0)
您忘记将<context:component-scan base-package="pkg"/>
添加到spring-config.xml
答案 1 :(得分:0)
在对依赖注入进行一些研究之后,我得到了答案
首先我的配置是正确的..问题出在我所遵循的方法中......按照春天的说法,你的应用程序应该松散地耦合到bean的注入依赖。对于松耦合接口应该使用。 在bean中自动连接这些接口之后,spring会自动将依赖项添加到bean
中