0

I have a web application with JSF + Spring but when I reference an Autowired object got an NullPointerException error.

My web.xml

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

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

<context-param>
    <param-name>javax.faces.PROJECT_STAGE</param-name>
    <param-value>Development</param-value>
</context-param>
<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>/faces/*</url-pattern>
</servlet-mapping>
<session-config>
    <session-timeout>
        30
    </session-timeout>
</session-config>
<welcome-file-list>
    <welcome-file>faces/login.xhtml</welcome-file>
</welcome-file-list>

My spring-context.xml

<context:annotation-config/>

<context:component-scan base-package="pe.abc.defghi"/>

 <aop:aspectj-autoproxy />


<tx:annotation-driven/>

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath:/META-INF/settings.properties</value>
            </list>
        </property>
</bean>

<bean id="dataSource"
      class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="driverClassName" value="${driver}"/>
    <property name="url" value="${url}"/>
    <property name="username" value="${username}"/>
    <property name="password" value="${password}"/>
</bean>

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <property name="persistenceUnitName" value="PU_JMAD"/>
</bean>

<bean class="org.springframework.orm.jpa.JpaTransactionManager"
    id="transactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>

My interface:

public interface PerfilService {
public List<Perfil> buscar();     
public Perfil buscarXID(Integer id);
}

My Impl:

@Service("perfilService")
@Qualifier("impl1")
public class PerfilServiceImpl implements PerfilService {


@Override
public List<Perfil> buscar() {
           return null; 
}

@Override
public Perfil buscarXID(Integer id) {
            return null;
}     

}

My Bean:

@Component
@ManagedBean(name = "listados")
@ApplicationScoped
public class ListadoBean {

@Autowired
@Qualifier("impl1")
PerfilService perfilService;

public List<Perfil> getPerfiles() {
    perfiles = perfilService.buscar();
    return perfiles;
}

}

Error is produced when I call perfiles = perfilService.buscar(); because perfilService is null

jotajota
  • 1
  • 2
  • Possible duplicate http://stackoverflow.com/questions/4455223/how-to-autowire-into-jsf-managed-beans and http://stackoverflow.com/questions/6192914/spring-beans-injected-into-jsf-managed-beans?rq=1 – Shailendra Jun 15 '15 at 16:48
  • Please translate your question to English. – Jesper Jun 15 '15 at 19:48
  • translated! @Jesper, please help, thnks! – jotajota Jun 15 '15 at 22:28
  • Have a look at the links that Shailendra posted, maybe those are useful to you. – Jesper Jun 16 '15 at 06:59
  • Yes, it was, "Both JSF and Spring can act as bean containers..." and I was using @ ManagedBean(name = "listados", I just used @ManagedBean (without name) because I suposse having two names gives problems. Other option was remove @ ManagedBean but I prefere be explicit. – jotajota Jun 16 '15 at 20:30

0 Answers0