【问题标题】:NullPointerException when injecting Spring service bean to JSF ManagedBean将 Spring 服务 bean 注入 JSF ManagedBean 时出现 NullPointerException
【发布时间】:2012-05-12 03:48:19
【问题描述】:

Tomcat 7、JSF 2、Spring 3、Java 6

Error: NullPointerException on userService.checkUser(getLogin()) (userService is null) in UserBean.java 访问jsf页面时。

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
          http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
           version="2.5">

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <listener>
        <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
    </listener>

    <servlet>
        <servlet-name>Faces Servlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>*.jsf</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>/faces/*</url-pattern>
    </servlet-mapping>
</web-app>

faces-config.xml

<faces-config xmlns="http://java.sun.com/xml/ns/javaee"
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd"
              version="2.0">
    <application>
       <el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>
        <resource-bundle>
            <base-name>i18n</base-name>
            <var>msg</var>
        </resource-bundle>
        <locale-config>
            <default-locale>en</default-locale>
            <supported-locale>ru</supported-locale>
        </locale-config>
    </application>
</faces-config>

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-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <!-- Services Beans -->
    <bean id="userService" class="service.UserServiceImpl">
        <property name="userDao" ref="userDao"/>
    </bean>

    <!-- DAOs -->
    <bean id="userDao" class="dao.UserDaoImpl">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>

    <!-- Hibernate session factory -->
    <bean id="sessionFactory"
          class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="configLocation">
            <value>classpath:/hibernate.cfg.xml</value>
        </property>
    </bean>

    <tx:annotation-driven/>
    <bean id="transactionManager"
          class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>
</beans>

UsersBean.java

package beans;

import service.UserService;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import java.io.Serializable;

@ManagedBean(name="usersBean")
@SessionScoped
public class UsersBean implements Serializable{
    private String login;
    private String password;
    @ManagedProperty(name = "userService", value = "#{userService}")
    private UserService userService;

    ...Getters, setters for every field...

    public void checkRegistred(){
        userService.checkUser(getLogin());
    }
}

xhtml 文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html
        PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:ui="http://java.sun.com/jsf/facelets">

<h:head>
    <title>Simple JSF Facelets page</title>
</h:head>

<h:body>
    <ui:composition template="layout.xhtml">
        <ui:define name="content">
            <h:link value="First page" outcome="index.xhtml"></h:link>
            <br/>
            <h:link value="Third page" outcome="third.xhtml"></h:link>
            <br/>
            Hello #{usersBean.login} with pass #{usersBean.password}
            <br/>
            You are #{usersBean.checkRegistred()}
        </ui:define>
    </ui:composition>
</h:body>

</html>

【问题讨论】:

    标签: java spring jsf-2 dependency-injection


    【解决方案1】:

    查看this question 及其答案。在您的情况下,当您使用 XML 配置时,为了使用 @autowired 注释,您还需要修改您的 userServide 声明如下:

    <bean id="userService" class="service.UserServiceImpl" autowire-candidate="true">
        <property name="userDao" ref="userDao"/>
    </bean>
    

    autowire-candidate 属性设置为 true,userService 将可用于自动连线。

    更新:

    package beans;
    
    import ...
    
    @SessionScoped
    public class UsersBean implements Serializable {
    
        private String login;
    
        private String password;
    
        @Autowired
        private UserService userService;
    
        ...Getters, setters for every field...
    
        public void checkRegistred(){
            userService.checkUser(getLogin());
        }
    
    }
    

    此外,它应该在您的 XML 中找到:

    <context:component-scan base-package="beans.*" />
    <context:annotation-config />
    

    【讨论】:

    • 现在我有另一个错误:javax.el.PropertyNotFoundException: /index.xhtml @23,114 value="#{usersBean.login}": Target Unreachable, identifier 'usersBean' resolve to null 它开启行:
    • 您是否按照我上面的描述定义了您的UsersBean?看起来错误和之前一样:usersBean is null.
    • 是因为faces-config.xml没有在WEB-INF目录下。愚蠢的错误。感谢您的帮助。
    猜你喜欢
    • 2011-12-10
    • 2015-01-11
    • 2012-05-21
    • 1970-01-01
    • 2015-04-16
    • 1970-01-01
    • 2013-06-25
    • 2019-06-04
    • 2021-08-04
    相关资源
    最近更新 更多