1

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

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

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

成功创建并成功设置了自动装配的设置器,但是当我尝试调用 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();
    }      
}

配置文件

面孔-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>

*

弹簧数据库.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>

在此处输入图像描述 在此处输入图像描述

4

1 回答 1

3

问题是你混淆了 jsf、CDI 和 spring 注释。@Component在你的课堂上使用UserServiceImpl。因为如果使用@Named,创建的 bean 的生命周期将由 CDI 管理,因为它是一个 CDi 注释。因此,当您使用@Autowired注解注入 Spring 未知的 bean 时,将获得null. 因此,当您在注入 bean 时使用@Autowired时,注入 bean 必须在 spring 托管上下文中,并且要做到这一点,您应该使用@Component. 如需进一步阅读,您可以查看这个不错的教程

于 2013-04-23T05:29:35.707 回答