我有一个 Facelets/JSF Managed Beans/Hibernate 应用程序。现在我正在尝试通过注释在我的 bean 中实现 DI。我有 2 个:LoginBean、RegistrationBean(每个负责相应的页面)。问题是,每当我尝试在 RegistrationBean 中自动装配属性时,我都会得到 NPE。但是,如果我将相同的类字段放入 LoginBean - 它们是自动装配的,没有问题!下面是我的 ManagedBeans、applicationContext.xml(数据库设置没问题,所以我不会发布 db.properties)和一个我正在尝试自动装配的类 Stacktrace。
最诚挚的问候。
纳扎尔
应用程序上下文.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/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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.dataart.mediaportal"/>
<!-- DATA SOURCE AND PERSISTENCE SETTINGS-->
<bean id="propertiesPlaceholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:db.properties</value>
</list>
</property>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dmDataSource"/>
<property name="packagesToScan" value="com.dataart.mediaportal"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${db.dialect}</prop>
<prop key="hibernate.show_sql">${db.show_sql}</prop>
<prop key="hibernate.hbm2ddl.import_files">${db.import_files}</prop>
<prop key="hibernate.hbm2ddl.auto">${db.hbm2ddl_auto}</prop>
<prop key="connection.pool_size">${db.pool_size}</prop>
<prop key="current_session_context_class">${db.current_session_context_class}</prop>
<!--<prop key="hibernate.cache.provider_class">${db.provider_class}</prop>-->
<!--<prop key="hibernate.cache.use_second_level_cache">${db.use_second_level_cache}</prop>-->
<!--<prop key="hibernate.cache.use_query_cache">${db.use_query_cache}</prop>-->
</props>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="dataSource" ref="dmDataSource" />
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="dmDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${db.driver}" />
<property name="url" value="${db.url}" />
<property name="username" value="${db.username}" />
<property name="password" value="${db.password}" />
<property name="maxWait" value="5000" />
<property name="maxActive" value="5" />
</bean>
</beans>
登录豆:
package com.dataart.mediaportal.controller.bean;
import com.dataart.mediaportal.dao.impl.UserDAOImpl;
import com.dataart.mediaportal.model.User;
import java.io.IOException;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
@RequestScoped
@ManagedBean(name = "loginBean")
public class LoginBean extends BaseBean {
private static final Logger logger = LoggerFactory.getLogger(LoginBean.class);
private User user;
private String login;
private String password;
@Autowired
private UserDAOImpl userDAO;
public String getLogin() {
return login;
}
public void setLogin(String login) {
this.login = login;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String authorize() {
logger.debug("Searching user in database...");
user = userDAO.login(login, password);
if (user != null) {
logger.debug("User found.");
getSession(false).setAttribute("user", user);
return "login";
}
logger.warn("User has not been found. Login: " + login + ", Password: " + password);
return null;
}
public void logout() throws IOException {
logger.debug("Loging out.");
ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext();
logger.debug("Invalidating session...");
ec.invalidateSession();
logger.debug("Done. Forwarding to home page.");
ec.redirect("login.xhtml");
}
}
注册豆:
package com.dataart.mediaportal.controller.bean;
import com.dataart.mediaportal.dao.impl.UserDAOImpl;
import com.dataart.mediaportal.model.User;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
@Component
@RequestScoped
@ManagedBean(name = "registration")
@Scope("request")
public class RegistrationBean extends BaseBean {
@Autowired(required = false)
private UserDAOImpl userDAO;
private String regLogin;
private String regPassword;
private String name;
private String surname;
@Autowired(required = true)
private User user;
public String getRegLogin() {
return regLogin;
}
public void setRegLogin(String regLogin) {
this.regLogin = regLogin;
}
public String getRegPassword() {
return regPassword;
}
public void setRegPassword(String regPassword) {
this.regPassword = regPassword;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSurname() {
return surname;
}
public void setSurname(String surname) {
this.surname = surname;
}
public String register() throws NoSuchAlgorithmException {
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(regPassword.getBytes());
String hash = new BigInteger(1, md.digest()).toString(16);
user.setUserLogin(regLogin);
user.setUserPassword(hash);
user.setUserName(name);
user.setUserLastname(surname);
user.setRoleId(0);
if (userDAO.insertUser(user)) {
getSession(false).setAttribute("user", user);
return "home";
} else {
FacesContext.getCurrentInstance().
addMessage(null, new FacesMessage("Registration failed!"));
return null;
}
}
}
用户:
package com.dataart.mediaportal.model;
import java.io.Serializable;
import java.util.List;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import org.springframework.stereotype.Component;
@Entity
@Table(name = "users")
@NamedQueries({
@NamedQuery(name = "User.findAll", query = "SELECT u FROM User u")})
@Component
public class User implements Serializable {
private static final long serialVersionUID = 1L;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 2147483647)
@Column(name = "user_login")
private String userLogin;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 32)
@Column(name = "user_password")
private String userPassword;
@Size(max = 2147483647)
@Column(name = "user_name")
private String userName;
@Size(max = 2147483647)
@Column(name = "user_lastname")
private String userLastname;
@Column(name = "role_id")
private Integer roleId;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "user_id")
private Integer userId;
@OneToMany(mappedBy = "userId", fetch = FetchType.LAZY)
private List<Album> albumList;
public User() {
}
public User(Integer userId) {
this.userId = userId;
}
public User(Integer userId, String userLogin, String userPassword) {
this.userId = userId;
this.userLogin = userLogin;
this.userPassword = userPassword;
}
public String getUserLogin() {
return userLogin;
}
public void setUserLogin(String userLogin) {
this.userLogin = userLogin;
}
public String getUserPassword() {
return userPassword;
}
public void setUserPassword(String userPassword) {
this.userPassword = userPassword;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserLastname() {
return userLastname;
}
public void setUserLastname(String userLastname) {
this.userLastname = userLastname;
}
public Integer getRoleId() {
return roleId;
}
public void setRoleId(Integer roleId) {
this.roleId = roleId;
}
public Integer getUserId() {
return userId;
}
public void setUserId(Integer userId) {
this.userId = userId;
}
public List<Album> getAlbumList() {
return albumList;
}
public void setAlbumList(List<Album> albumList) {
this.albumList = albumList;
}
}
堆栈跟踪:
javax.faces.FacesException: #{registration.register()}: java.lang.NullPointerException
at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:89)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:198)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:646)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.myfaces.webapp.filter.ExtensionsFilter.doFilter(ExtensionsFilter.java:374)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:936)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1004)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:662)
Caused by: javax.faces.FacesException: #{registration.register()}: java.lang.NullPointerException
at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:118)
at javax.faces.component.UICommand.broadcast(UICommand.java:315)
at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:790)
at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1282)
at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
... 25 more
Caused by: javax.faces.el.EvaluationException: java.lang.NullPointerException
at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:101)
at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
... 29 more
Caused by: java.lang.NullPointerException
at com.dataart.mediaportal.controller.bean.RegistrationBean.register(RegistrationBean.java:69)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.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:278)
at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:274)
at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:87)
... 30 more