Spring 注入 SessionFactory 并提供 NullPointers 继承
在我的设计中,我的所有 daos 都继承自父类,该父类包含 hibernateTemplate 字段和 setSessionFactory,它在使用 spring 设置会话时创建 hibernateTemplate
这里的问题是,即使它似乎已设置,但当我实际执行代码和调用 hibernateTemplate 对象的 daos 似乎为 null。然而,当我使用会话工厂注入 Dao 对象而不是父泛型类时,它的工作方式就像一个魅力
AbstractDaoSupport 类的部分
/** The hibernate template. */
private HibernateTemplate hibernateTemplate;
/**
* Sets the session factory.
*
* @param sessionFactory the new session factory
*/
public void setSessionFactory(SessionFactory sessionFactory) {
this.setHibernateTemplate(new HibernateTemplate(sessionFactory));
}
/**
* Sets the hibernate template.
*
* @param hibernateTemplate the hibernateTemplate to set
*/
public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
this.hibernateTemplate = hibernateTemplate;
}
这是当前有问题的代码,其中 hibernateTemplate 在运行时为 null
<!-- the DataSource for application usage -->
<bean id="applicationDataSource" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://127.0.0.1:3306/taxidb"/>
<property name="username" value="root"/>
<property name="password" value="abc"/>
</bean>
<bean id="daoSessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="applicationDataSource" />
<property name="annotatedClasses">
<list>
<value>com.iit.awt.application.domain.Driver</value>
<value>com.iit.awt.application.domain.DriverRealTimeCurrentLocation</value>
<value>com.iit.awt.application.domain.Journey</value>
<value>com.iit.awt.application.domain.Customer</value>
<value>com.iit.awt.application.domain.SystemConstants</value>
<value>com.iit.awt.application.domain.DriverRequest</value>
<value>com.iit.awt.application.domain.Account</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="daoSessionFactory"/>
</bean>
<!-- JPA Daos -->
<bean id="abstractDaoSupport" class="com.iit.awt.platform.support.AbstractDaoSupport">
<property name="sessionFactory" ref="daoSessionFactory" />
</bean>
这是当代码工作而不是上面“JPA Daos”注释下的最后一段代码还有以下
<bean id="driverLocationDao" class="com.iit.awt.application.dao.impl.DriverLocationDaoImpl">
<property name="sessionFactory" ref="daoSessionFactory" />
</bean>
另一件事需要注意的是,该类以前是抽象的,我尝试使用“abstract=true”以及让它成为非抽象的当前类 ?
有谁知道为什么 hibernateTemplate 对象为空吗
非常感谢任何帮助
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的问题
您实际上没有在此处设置
sessionFactory
,因此它将保持null
更好的方式
我按照下面的方式完成了它,为什么要自己创建模板..
AbstractDAO
HibernateTemplate Bean 定义
使用会话工厂注入
HibernateTemplate Bean 定义
使用会话数据源注入和其他属性设置器注入
Your issue
you are not setting
sessionFactory
actually here, so it would staynull
Better Way
I'd done it following way, why to create template on our own..
AbstractDAO
HibernateTemplate Bean Defination
with session factory injection
HibernateTemplate Bean Defination
with session datasource injection and other property setter injection
谁在您的代码中注入 hibernateTemplate ?如果您要扩展 HibernateDaoSupport ,则只需注入 sessionFactory 。 getHibernateTemplate() 方法在其父类中查找 hibernate 模板。这就是为什么下面的代码可以工作的原因
这里不需要 hibernate 模板初始化。如果您不扩展 hibernateDaoSuppor,则必须像 @Jigar Joshi 提到的那样注入模板。
请注意,根据 Spring 3 建议,不建议使用 Hibernate 模板 此处
Who is injecting the hibernateTemplate in your code ? If you're extending HibernateDaoSupport , you only need to inject the sessionFactory . the getHibernateTemplate() method looks for the hibernate template in it's parent class. That is why the code below works
Here hibernate template initialization is not required . If you're not extending hibernateDaoSuppor, you will have to inject the template as @Jigar Joshi has mentioned .
Please note, Hibernate template use is not recommended as per Spring 3 recommendations here