当调用bean方法primefaces jsf时,托管bean字段为null

时间:2013-04-23 01:41:53

标签: java spring jsf cdi

我正在开发一个primefaces - spring框架应用程序,但我的bean有一个大问题

我在java代码中使用spring注释来发现我的bean,服务或组件

但问题是,当我启动jboss 7.1 AS并创建bean时,我可以看到我的bean ListUsersBean

成功创建并且自动装配的setter设置成功,但在我尝试调用test()方法之后

从jsf页面,自动装配的属性为空。

我不知道发生了什么

非常感谢,如果有人可以帮助我

我正在使用

spring-framework 3.2.2 JBOSS AS 7.1 primefaces 3.5

这是我的豆子

package edu.unbosque.beans;

import java.io.Serializable;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.bean.ManagedProperty;

import edu.unbosque.model.User;
import edu.unbosque.services.IUserService;
import edu.unbosque.services.TestService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;


@Component
@ManagedBean
@SessionScoped
public class ListUsersBean implements Serializable{
    /**
     * Acceso A Datos De Usuario
     */
    private IUserService userService;


    private TestService testService;
    /**
     * Listado De Usuarios
     */
    private List<User> usersList;
    /**
     * Usuario Seleccionado
     */
    private User selectedUser;
    /**
     * Carga Los Usuarios
     */
    private void populateUsers(){       
        this.usersList = this.userService.getUsers();
    }
    public IUserService getUserService() {
        return userService;
    }

    @Autowired(required = true)
    @Qualifier("userServiceImpl")
    public void setUserService(IUserService userService) {
        this.userService = userService;
    }
    public List<User> getUsersList() {
        return usersList;
    }
    public void setUsersList(List<User> usersList) {
        this.usersList = usersList;
    }
    public User getSelectedUser() {
        return selectedUser;
    }
    public void setSelectedUser(User selectedUser) {
        this.selectedUser = selectedUser;
    }   

    public TestService getTestService() {
        return testService;
    }

    @Autowired
    public void setTestService(TestService testService) {
        this.testService = testService;
    }

    public void test(){ 

        this.testService = this.getTestService();

        int i = 1;
        i = i + 2;
    }
}

这是我的服务

package edu.unbosque.services;

import java.util.List;

import javax.faces.bean.ManagedProperty;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;


import edu.unbosque.*;
import edu.unbosque.dao.UserDao;
import edu.unbosque.model.User;
import javax.inject.Named;

@Named("userServiceImpl")
public class UserServiceImpl implements IUserService{

    @Autowired
    private UserDao userDao; 

    public UserDao getUserDao() {
        return userDao;
    }

    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {                       
        return (UserDetails) userDao.getUserByUserName(username);
    }

    @Override
    public List<User> getUsers() {
        return getUserDao().getUsers();
    }      
}

配置文件

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>
    </application>

    <lifecycle>
        <phase-listener>edu.unbosque.listeners.LoginPhaseListener</phase-listener>
    </lifecycle>
</faces-config>

*

弹簧database.xml

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"        
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans    
        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.2.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">

       <context:annotation-config />
       <context:component-scan base-package="edu.unbosque.model" />
       <context:component-scan base-package="edu.unbosque.dao" />
       <context:component-scan base-package="edu.unbosque.services" />
       <context:component-scan base-package="edu.unbosque.beans" />    

       <context:property-placeholder location="classpath:jdbc.properties" />
       <tx:annotation-driven transaction-manager="hibernateTransactionManager" />
       <bean id="hibernateTransactionManager"
              class="org.springframework.orm.hibernate4.HibernateTransactionManager">
              <property name="sessionFactory" ref="sessionFactory" />
       </bean>
       <bean id="dataSource"
              class="org.springframework.jdbc.datasource.DriverManagerDataSource">
              <property name="driverClassName" value="${database.driver}" />
              <property name="url" value="${database.url}" />
              <property name="username" value="${database.user}" />
              <property name="password" value="${database.password}" />
       </bean>

       <bean id="sessionFactory"
              class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
              <property name="dataSource" ref="dataSource" />
              <property name="annotatedClasses">
                     <list>
                           <value>edu.unbosque.model.User</value>
                           <value>edu.unbosque.model.Authority</value>                           
                     </list>
              </property>
               <property name="hibernateProperties">
                     <props>
                           <prop key="hibernate.dialect">${hibernate.dialect}</prop>
                           <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
                            <prop key="hibernate.hbm2ddl.auto">update</prop>

                     </props>
              </property>         
        </bean>

       <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>             
</beans>

的web.xml

<web-app id="WebApp_ID" version="2.4"
    xmlns="http://java.sun.com/xml/ns/j2ee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <listener>
        <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
    </listener>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>           
            /WEB-INF/spring-security.xml,
            /WEB-INF/spring-database.xml
        </param-value>
    </context-param>


    <display-name>Semilleros Universidad El Bosque</display-name>

    <!-- Change to "Production" when you are ready to deploy -->
    <context-param>
        <param-name>javax.faces.PROJECT_STAGE</param-name>
        <param-value>Development</param-value>
    </context-param>

    <!-- Welcome page -->
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>

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

    <!-- Map these files with JSF -->
    <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>
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>*.xhtml</url-pattern>
    </servlet-mapping>

    <context-param>
        <param-name>primefaces.THEME</param-name>
        <param-value>bootstrap</param-value>
    </context-param>

    <!-- Spring Security -->
    <filter>
        <filter-name>springSecurityFilterChain</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>springSecurityFilterChain</filter-name>
        <url-pattern>/*</url-pattern>
        <dispatcher>FORWARD</dispatcher>
        <dispatcher>REQUEST</dispatcher>
    </filter-mapping>

</web-app>

enter image description here enter image description here

1 个答案:

答案 0 :(得分:3)

问题是你混淆了jsf,CDI和spring注释。在班级@Component中使用UserServiceImpl。因为如果使用@Named,创建的bean的生命周期将由CDI管理,因为它是CDi注释。因此,当您使用@Autowired注释来注入Bean时,Spring将会获得null。因此,当您在注入bean时使用@Autowired时,注入bean必须位于spring托管上下文中,为此,您应该使用@Component。如需进一步阅读,您可以看到这个很好tutorial