Java sessionFactory.openSession中的NullPointerException
我正在使用SpringHibernate开发一个web应用程序。在那里,我在DAO类中有一个方法 当我通过jsp文件调用它时,它工作得很好。但当我通过servlet调用它时,它给出了一个Java sessionFactory.openSession中的NullPointerException,java,spring,hibernate,servlets,Java,Spring,Hibernate,Servlets,我正在使用SpringHibernate开发一个web应用程序。在那里,我在DAO类中有一个方法 当我通过jsp文件调用它时,它工作得很好。但当我通过servlet调用它时,它给出了一个NullPointerException。下面是我提出的方法 @Autowired private SessionFactory sessionFactory; @SuppressWarnings("unchecked") public List<Employee> listEmployees()
NullPointerException
。下面是我提出的方法
@Autowired
private SessionFactory sessionFactory;
@SuppressWarnings("unchecked")
public List<Employee> listEmployees() {
Session session = sessionFactory.openSession();//line 95
Criteria crit= session.createCriteria(Employee.class);
crit.add(Restrictions.eq("EmployeeId",2 ));
List<Employee> employeelist= crit.list();
session.close();
return employeelist;
}
这是我的sessionFactory配置
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="annotatedClasses">
<list>
<value>abc.model.Employee</value>
</list>
</property>
<property name="hibernateProperties" >
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
</props>
</property>
</bean>
您能告诉我如何解决这个问题吗,因为我想通过servlet调用它。在第95行之前包含下面的代码行。假设您尚未创建SessionFactory对象
SessionFactory sessionFactory=new Configuration().configure().buildSessionFactory();
Session session=sessionFactory.openSession();
session.beginTransaction();
//Your operation
Session.getTransaction().commit();
当我通过jsp文件调用它时,它工作得很好
这是因为DAO中定义了以下语句:
@Autowired
private SessionFactory sessionFactory;
调用listEmployees
方法的DAO实例是一个Spring托管bean,您在配置文件中定义的Spring托管的SessionFactory
已经注入到DAO bean中
但当我通过servlet调用它时,它会给出一个NullPointerException
这不起作用:
EmployeeDao employeeDao=new EmployeeDao();
employeeDao.listEmployees();
因为调用listEmployees
的EmployeeDao
实例不是Spring管理的bean。这只是EmployeeDao
的一个新实例。因此,您在EmployeeDao
中的SessionFactory
实例是null
现在,如果您想将Spring托管的EmployeeDao
注入Servlet,可以通过覆盖Servlet的init
方法来实现,如下所示:
private EmployeeDao ed;
public void init(ServletConfig config) throws ServletException {
super.init(config);
ApplicationContext context = WebApplicationContextUtils.getRequiredWebApplicationContext(this.getServletContext());
ed = (EmployeeDao) context.getBean("employeeDao");
}
现在,您可以将getConfirm
方法重新写入:
public void getConfirm(HttpServletRequest request, HttpServletResponse response) {
Logger.getLogger(this.getClass()).warning("Inside Confirm Servlet");
response.setContentType("text/html");
// do some works
//EmployeeDao employeeDao=new EmployeeDao();
employeeDao.listEmployees(); //line 55
}
编辑:
在web.xml中输入以下条目:
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/your-spring-config-file.xml
</param-value>
</context-param>
org.springframework.web.context.ContextLoaderListener
上下文配置位置
/WEB-INF/your-spring-config-file.xml
对于我来说,hibernate.cfg.xml
中的映射属性指向不正确的模型。异常状态sessionFactory
引用的是null
,正如它所期望的那样sessionFactory
对象。检查您的sessionFactory
obejct初始化。您如何定义sessionFactory
?您可以将您的sessionFactory配置和注入代码共享给EmployeeDaoImpl类吗?您正在构建自己的dao实例(不由spring管理),并期望spring神奇地注入依赖项。自动关联依赖项,如果不可能,则使用应用程序上下文检索spring配置的实例。还可以使用getCurrentSession
而不是openSession
,让spring通过应用正确的声明性事务管理来管理会话。我也尝试过。当我使用它时,它停止给出错误。但是没有传递任何值。我更新了我的代码,尝试这种方式,如果仍然存在任何错误,请让我通知。谢谢您的解释。我明白错在哪里。当我尝试您的解决方案时,它在ApplicationContext=webapplicationcontextils行中给出了错误。。。错误为java.lang.IllegalStateException:未找到WebApplicationContext:未注册ContextLoaderListener?位于org.springframework.web.context.support.WebApplicationContextUtils.getRequiredWebApplicationContext(WebApplicationContextUtils.java:84),位于net.pearson.mail.ConfirmServlet.init(ConfirmServlet.java:38),位于org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1280)。。。
public void getConfirm(HttpServletRequest request, HttpServletResponse response) {
Logger.getLogger(this.getClass()).warning("Inside Confirm Servlet");
response.setContentType("text/html");
// do some works
//EmployeeDao employeeDao=new EmployeeDao();
employeeDao.listEmployees(); //line 55
}
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/your-spring-config-file.xml
</param-value>
</context-param>