foxnet
foxnet

Reputation: 293

managed bean field is null when invoke bean method primefaces jsf

I am developing a primefaces - spring framework application but i have a big problem with my beans

i'm using spring annotations in java code to discover my beans, services, or component

but the problem is when i start jboss 7.1 AS and the beans are created i can see that my bean ListUsersBean

is created succesfully and the autowired setters are set successfully, but after when i try to invoke a test() method

from jsf page the autowired attributes are null.

I dont know what is happening

thanks a lot if anyone may help me

i'm using

spring-framework 3.2.2 JBOSS AS 7.1 primefaces 3.5

this is my bean

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

this is my service

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 files

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>

*

spring-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

Upvotes: 1

Views: 3196

Answers (1)

Sazzadur Rahaman
Sazzadur Rahaman

Reputation: 7126

The problem is you are mixing up jsf, CDI and spring annotations. Use @Component in your class UserServiceImpl. Because if you use @Named, the created bean's life cycle will be managed by CDI, because its a CDi annotation. So as you are using @Autowired annotation to inject the bean, which is unknown to Spring, will get null. So as you are using @Autowired while injecting the bean, the injecting bean must be in spring managed context and to do that you should have use @Component. For further reading you can see this nice tutorial.

Upvotes: 3

Related Questions