2013-04-23 76 views
1

我開發一個primefaces - Spring Framework的應用程序,但我有我的豆管理bean字段爲空當調用Bean方法primefaces JSF

我使用Spring註解在Java代碼中的一個大問題,發現我的豆子,服務或組件

但問題是,當我AS啓動JBoss 7.1和豆類創建我可以看到我的豆ListUsersBean

被成功地創建和自動裝配製定者成功後,但是當我嘗試後調用test()方法

來自jsf頁面的自動裝配屬性爲空。

我不知道發生了什麼

非常感謝,如果有人可以幫助我

我使用

彈簧框架3.2.2 作爲JBoss 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

回答

3

問題是你是混合了JSF,CDI和Spring註解。在您的課程UserServiceImpl中使用@Component。因爲如果使用@Named,創建的bean的生命週期將由CDI管理,因爲它的CDi註釋。因此,如果您使用@Autowired註釋來注入Spring未知的bean,將獲得null。因此,在注入Bean時使用@Autowired,注入bean必須處於spring管理的上下文環境中,並且您應該使用@Component。爲進一步閱讀,你可以看到這個不錯的tutorial