Java @自动连线会导致NullPointerException

Java @自动连线会导致NullPointerException,java,spring,hibernate,jsf,dependency-injection,Java,Spring,Hibernate,Jsf,Dependency Injection,我有一个Facelets/JSF托管Beans/Hibernate应用程序。现在我正试图通过注释将DI实现到我的bean中。我有两个:LoginBean、RegistrationBean,每个负责相应的页面。问题是,每当我尝试在RegistrationBean中自动连接属性时,我最终都会使用NPE。但是,如果我将相同的类字段放入LoginBean,它们是自动连接的,没有问题!下面是我的ManagedBeans,applicationContext.xml数据库设置正常,所以我不会发布db.pro

我有一个Facelets/JSF托管Beans/Hibernate应用程序。现在我正试图通过注释将DI实现到我的bean中。我有两个:LoginBean、RegistrationBean,每个负责相应的页面。问题是,每当我尝试在RegistrationBean中自动连接属性时,我最终都会使用NPE。但是,如果我将相同的类字段放入LoginBean,它们是自动连接的,没有问题!下面是我的ManagedBeans,applicationContext.xml数据库设置正常,所以我不会发布db.properties和一个类,我正在尝试自动连接Stacktrace

致以最良好的问候

纳扎尔

applicationContext.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>
注册bean:

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;
        }
    }

}
用户:


尝试从RegistrationBean中删除属性“userDAO”的“required=false”


谢谢。

当您尝试将jsf表单输入绑定到用户bean时,堆栈跟踪指示注册bean第69行的NPE。在登录bean中,它可以正常工作,因为您没有进行任何数据绑定,而是调用Dao并将返回值分配给用户对象

NPE导致的数据绑定问题可能与JSF表单有关,但与AutoWire设置无关


此外,我还将在RegistrationBean中为您的用户字段设置适当的getter和setter方法。要解决此问题,必须执行的所有操作都将替换:

@Component
@RequestScoped
@ManagedBean(name = "loginBean")


对于您拥有的每个ManagedBean,它都有自己的作用域和道具。

没有帮助。还有其他线索吗?你有没有尝试从RegistrationBean中删除“@Scoperequest”,这可能会限制DI注入bean?是的,我已经尝试过了。我就是搞不懂。为什么DI在一个类中工作得很好而拒绝在另一个类中工作..请查看下面的教程以更好地理解JSF和Spring集成。您的代码有太多问题。尝试寻找一个关于JSF/Spring交互的更合适的教程。如果稍加搜索,您可以找到一些有关堆栈溢出的示例。至于为什么,这是因为@Autowired不能与@ManagedBean一起工作,正如您错误地预期的那样。正如你所理解的,两个建议的答案都不能回答你的问题。相反,要么让bean由Spring管理,从而删除所有与JSF相关的注释,要么通过@ManagedProperty注入bean,从而删除所有与Spring相关的内容。谢谢:这很好。如果您有getter/setter或没有,那么@Autowire就不重要了。
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
@Component
@RequestScoped
@ManagedBean(name = "loginBean")
@Component
@Scope("request")