我已经开始通过一项作业学习 hibernate 和 spring,其中我尝试通过 spring 使用 session 工厂实例。我理解 hibernate 部分,但无法继续 Spring 。我已经尝试了很多教程和示例,但就是无法让我的 spring 工作。虽然当我直接实例化它时它可以工作。 以下是我的项目的问题相关详细信息...
applicationContext.xml(WEB-INF 内)
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:lang="http://www.springframework.org/schema/lang"
xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
<context:annotation-config />
<context:component-scan
base-package="com.nagarro.training.assignment6.dao.entity.Student" />
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
<property name="hibernateProperties">
<value>
hibernate.dialect=org.hibernate.dialect.HSQLDialect
</value>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<tx:annotation-driven />
<bean id="studentDAO"
class="com.nagarro.training.assignment6.dao.impl.StudentDAOImplementation">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
</beans>
编辑:包括建议的更改 StudentDAOImplementaion.java(使用它的文件)
public class StudentDAOImplementation implements StudentDAO {
/**
* single instance of hibernate session
*/
@Autowired
private SessionFactory sessionFactory;
// HibernateUtil.getSessionFactory().openSession()
/**
* @param sessionFactory
*/
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
/**
* @return the sessionFactory
*/
public SessionFactory getSessionFactory() {
return sessionFactory;
}
private Session getSession() {
return this.sessionFactory.getCurrentSession();
}
/**
* @return list of all the students
*/
public List<Student> getStudentList() {
System.out.println(this.getSession().getStatistics());
return (List<Student>) this.getSEssion.createQuery("from Student")
.list();
}
}
这是 lib 文件夹中我的 jar 文件的片段:
我认为我不需要包含 hibernate 文件和bean,因为它在没有 spring 的情况下工作正常。这是我无法上类的 Spring 。我在网上尝试了许多不同的实现,但我就是无法让它工作。它只是在 StudentDAOImplementation 中的 ** System.out.println(sessionFactory.getStatistics());** 行上显示空指针异常。
调用 StudentDAO 的测试类
public class Test {
public static void main(String[] args) {
StudentDAOImplementation sd = new StudentDAOImplementation();
List<Student> list = sd.getStudentList();
for(Student s : list) {
System.out.println(s.getName());
}
}
}
堆栈跟踪
Exception in thread "main" java.lang.NullPointerException
at StudentDAOImplementation.getStudentList(StudentDAOImplementation.java:116)
at Test.main(Test.java:13)
最佳答案
您的 sessionFactory
变量的命名有误导性,因为该类型实际上是 Session
。 Session
!= SessionFactory
。您在 sessionFactory.getStatistics()
上收到 NPE,因为 Spring 无法将 Session Autowiring 到这样的 DAO 中。如果您在 NPE 之前没有看到错误,那么您实际上并没有使用 Spring 实例化 DAO,否则您会收到有关无法找到 Session 类型的依赖项的错误。使用基于 Hibernate 的 DAO 的正确方法是使用 SessionFactory
注入(inject)它,并在需要 Session
的方法中调用 getCurrentSession()
>。请参阅"Implementing DAOs based on plain Hibernate 3 API"有关此方法以及设置适当的事务管理的详细信息,请参阅下文。
更新:再看一眼,我发现您的组件扫描包已设置为 com.nagarro.training.assignment6.dao.entity.Student
,它看起来完全像一个类,而不是一个包。它也与您实际想要进行组件扫描的任何内容相差甚远。也许你不明白组件扫描的用途。它包含在 "Annotation-based container configuration" 下在引用指南中。
更新2:关于您的“测试”代码:您根本没有使用Spring,因此您不妨删除XML并省去麻烦。另一方面,如果您想实际使用 Spring,则需要根据所述 XML 文件在 main 方法中创建一个上下文,例如:
ApplicationContext context = new FileSystemXmlApplicationContext(locationOfXmlFile);
然后,如果您想要一个 Spring 管理的 DAO,则不能仅使用 new
创建一个。 Spring 并不神奇。它不会仅仅因为您将其加载到同一个 JVM 中的某个位置就夺取您的控制权。*您必须向 Spring 请求它创建的 DAO,例如:
StudentDAO dao = context.getBean(StudentDAO.class);
请注意,我使用的是接口(interface)类型,而不是具体类型。出于多种原因,这始终是一种明智的做法。
这(不是启动 Spring)是你的第一个问题。一旦执行此操作,您就会遇到其他配置问题。如果您需要帮助解决其中一个问题,您应该发布一个新问题。
关于java - 可以直接实例化hibernate session Factory但不能通过spring来实例化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15194860/