我在我的演示应用程序中使用 JSF2+Spring3.1+Hibernate4,我想使用注释来创建会话工厂,但是我的 DAO 类没有在 Jsf Managed Bea 类中初始化,所以我得到空指针异常。我的applicationContext.xml
<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/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
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">
<context:annotation-config></context:annotation-config>
<context:component-scan base-package="com.otv"></context:component-scan>
<!-- Data Source Declaration -->
<bean id="DataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" >
<property name="driverClass" value="org.postgresql.Driver" />
<property name="jdbcUrl" value="jdbc:postgresql://localhost:5432/postgres" />
<property name="user" value="postgres" />
<property name="password" value="hariom" />
<property name="maxPoolSize" value="10" />
<property name="maxStatements" value="0" />
<property name="minPoolSize" value="5" />
</bean>
<!-- Session Factory Declaration -->
<bean id="SessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean ">
<property name="dataSource" ref="DataSource" />
<property name="annotatedClasses">
<list>
<value>com.otv.model.User</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
<property name="packagesToScan" value="com.otv.user"></property>
</bean>
<!-- Transaction Manager is defined -->
<bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager" >
<property name="sessionFactory" ref="SessionFactory"/>
</bean>
<!-- Enable the configuration of transactional behavior based on annotations -->
<tx:annotation-driven transaction-manager="txManager"/>
</beans>
UserDAO.java 类
package com.otv.user.dao;
import java.util.List;
import com.otv.model.User;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
/**
*
* User DAO
*
* @author onlinetechvision.com
* @since 25 Mar 2012
* @version 1.0.0
*
*/
@Repository
@Transactional
public class UserDAO {
@Autowired
private SessionFactory sessionFactory;
/**
* Get Hibernate Session Factory
*
* @return SessionFactory - Hibernate Session Factory
*/
/* public SessionFactory getSessionFactory() {
return sessionFactory;
}
*//**
* Set Hibernate Session Factory
*
* @param SessionFactory - Hibernate Session Factory
*//*
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}*/
/**
* Add User
*
* @param User user
*/
public void addUser(User user) {
sessionFactory.getCurrentSession().save(user);
}
/**
* Delete User
*
* @param User user
*/
public void deleteUser(User user) {
sessionFactory.getCurrentSession().delete(user);
}
/**
* Update User
*
* @param User user
*/
public void updateUser(User user) {
sessionFactory.getCurrentSession().update(user);
}
/**
* Get User
*
* @param int User Id
* @return User
*/
public User getUserById(int id) {
List list = sessionFactory.getCurrentSession()
.createQuery("from User where id=?")
.setParameter(0, id).list();
return (User)list.get(0);
}
/**
* Get User List
*
* @return List - User list
*/
public List<User> getUsers() {
List list = sessionFactory.getCurrentSession().createQuery("from User").list();
return list;
}
}
和我的 managedBean 类
package com.otv.managed.bean;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import com.otv.model.User;
import com.otv.user.dao.UserDAO;
import com.otv.user.service.IUserService;
/**
*
* User Managed Bean
*
* @author onlinetechvision.com
* @since 25 Mar 2012
* @version 1.0.0
*
*/
@ManagedBean(name="userMB")
@RequestScoped
public class UserManagedBean implements Serializable {
private static final long serialVersionUID = 1L;
private static final String SUCCESS = "success";
private static final String ERROR = "error";
//Spring User Service is injected...
List<User> userList;
private int id;
private String name;
private String surname;
@Autowired
private UserDAO userDAO;
/**
* Add User
*
* @return String - Response Message
*/
public String addUser() {
try {
User user = new User();
user.setId(getId());
user.setName(getName());
user.setSurname(getSurname());
userDAO.addUser(user);
return SUCCESS;
} catch (DataAccessException e) {
e.printStackTrace();
}
return ERROR;
}
/**
* Reset Fields
*
*/
public void reset() {
this.setId(0);
this.setName("");
this.setSurname("");
}
/**
* Get User List
*
* @return List - User List
*/
public List<User> getUserList() {
userList = new ArrayList<User>();
userList.addAll(userDAO.getUsers());
return userList;
}
/**
* Get User Service
*
* @return IUserService - User Service
*/
/**
* Set User List
*
* @param List - User List
*/
public void setUserList(List<User> userList) {
this.userList = userList;
}
/**
* Get User Id
*
* @return int - User Id
*/
public int getId() {
return id;
}
/**
* Set User Id
*
* @param int - User Id
*/
public void setId(int id) {
this.id = id;
}
/**
* Get User Name
*
* @return String - User Name
*/
public String getName() {
return name;
}
/**
* Set User Name
*
* @param String - User Name
*/
public void setName(String name) {
this.name = name;
}
/**
* Get User Surname
*
* @return String - User Surname
*/
public String getSurname() {
return surname;
}
/**
* Set User Surname
*
* @param String - User Surname
*/
public void setSurname(String surname) {
this.surname = surname;
}
}
现在在 Managedbean 方法中,DAO 对象为空,我得到空指针异常
警告:#{userMB.addUser}:java.lang.NullPointerException javax.faces.FacesException:#{userMB.addUser}:com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:118) 处的 java.lang.NullPointerException ) 在 javax.faces.component.UICommand.broadcast(UICommand.java:315) 在 javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:794) 在 javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:第1259章org.apache.catalina.core.ApplicationFilterChain 的 javax.faces.webapp.FacesServlet.service(FacesServlet.java:593) 的 .execute(LifecycleImpl.java:118)。internalDoFilter(ApplicationFilterChain.java:305) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:225) at org.apache .catalina.core.StandardContextValve.invoke(StandardContextValve.java:123) 在 org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472) 在 org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java :168) 在 org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927) 在 org.apache.catalina.core 的 org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)。 StandardEngineValve.invoke(StandardEngineValve.java:118) 在 org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407) 在 org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:585) 在 org.apache.tomcat.util.net 的 org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1001) .JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312) 在 java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886) 在 java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908 ) 在 java.lang.Thread.run(Thread.java:662) 引起:javax.faces.el.EvaluationException: java.lang.NullPointerException 在 javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:102) 在com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102) ... 23 更多原因:java。lang.NullPointerException 在 com.otv.managed.bean.UserManagedBean.addUser(UserManagedBean.java:57) 在 sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 在 sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) 在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at org.apache.el.parser.AstValue.invoke(AstValue.java:264)在 org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:278) 在 com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105) 在 javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter .java:88) ... 24 更多addUser(UserManagedBean.java:57) 在 sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 在 sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) 在 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)在 java.lang.reflect.Method.invoke(Method.java:597) 在 org.apache.el.parser.AstValue.invoke(AstValue.java:264) 在 org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java :278) at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105) at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:88) ... 24 更多addUser(UserManagedBean.java:57) 在 sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 在 sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) 在 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)在 java.lang.reflect.Method.invoke(Method.java:597) 在 org.apache.el.parser.AstValue.invoke(AstValue.java:264) 在 org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java :278) at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105) at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:88) ... 24 更多invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at org.apache.el.parser.AstValue .invoke(AstValue.java:264) 在 org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:278) 在 com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105) 在 javax。 faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:88) ... 24 更多invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at org.apache.el.parser.AstValue .invoke(AstValue.java:264) 在 org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:278) 在 com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105) 在 javax。 faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:88) ... 24 更多在 com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105) 处调用(MethodExpressionImpl.java:278) 在 javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:88) ... 24更多的在 com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105) 处调用(MethodExpressionImpl.java:278) 在 javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:88) ... 24更多的