1

I created a simple application that s using JSF and managed beans.From the managed bean I m trying to instantiate a spring bean that stores data to the database. However "@Autowired" annotation doesnt seem to work since I get a nullPointerExcpetion. I read all the related topics of how to use JSF and managed beans with Spring but couldnt solve the problem . Maybe someone could have a look at the following piece of code and give me a hint?

########## ManagedBean #########

    @Component
    @Scope("request")
    @Qualifier("memberBean")
    public class ProjectEntityHandlerBean
    {

      private  List projectList;

      @Autowired
      private ProjectBeanLocal projectBean;

    public ProjectEntityHandlerBean()
    {

    }

    public List getProjectList() {  
        return projectList;  
    } 


        public String getAllProjects()
        {       
            projectList = projectBean.getAllProjects();
            return "true";      
        }

The Service Bean


    @Service
    public class ProjectBean implements ProjectBeanLocal {

          @PersistenceContext
          private EntityManager em;

        /**
         * Default constructor. 
         */

        public ProjectBean() {
            // TODO Auto-generated constructor stub
        }

        @Transactional
        public List getAllProjects()

        {   
            System.out.println("ProjectBean invoked");

            Query query = em.createQuery("SELECT p FROM Project p");

            @SuppressWarnings("unchecked")




        List projects = query.getResultList();                 
            return projects;
        }

The faces.config.xml

 <application>
     <el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>
    </application>

    <managed-bean>
        <managed-bean-name>loginBean</managed-bean-name>
        <managed-bean-class>com.example.controller.LoginBean</managed-bean-class>
        <managed-bean-scope>session</managed-bean-scope>
    </managed-bean>

    <managed-bean>
        <managed-bean-name>ProjectEntityHandlerBean</managed-bean-name>
        <managed-bean-class>com.example.controller.ProjectEntityHandlerBean</managed-bean-class>
        <managed-bean-scope>request</managed-bean-scope>
    </managed-bean>
panipsilos
  • 2,219
  • 11
  • 37
  • 53

2 Answers2

1

If you see your configuration @component is a Spring annoatation. And you are instating ProjectEntityHandlerBean in faces-config.xml. So, first of all your ProjectEntityHandlerBean get instantiated as JSF ManagedBean.

So, you have to @ManagedProperty or <managed-property> to inject your service class into JSF managed bean like this.In this specific case being you defined your bean in faces-config.xml you to have inject like this.

<managed-bean>
  <managed-bean-name>ProjectEntityHandlerBean</managed-bean-name>
  <managed-bean-class>com.example.controller.ProjectEntityHandlerBean</managed-bean-class>
  <managed-bean-scope>request</managed-bean-scope>
  <managed-property>
   <property-name>ProjectBeanLocal</property-name>
   <property-class>Qualified path for this class</property-class>
   <value>#{projectBeanLocal}</value>
  </managed-property>
 </managed-bean>

Remove @Autowired out of the above ProjectEntityHandlerBean.

Important : You can only Inject one spring bean another spring bean using @Autowired annoatation.If you want to Inject a Spring Bean into JSF Managed Bean or one JSF ManagedBean into another JSF ManagedBean you have use @ManagedProperty if you are using JSF 2.0 or higher version or <managed-propety> for jsf 1.2 version.

SRy
  • 2,901
  • 8
  • 36
  • 57
0

Srinivas , thnx for your help.That worked!!!

So what I did is: I removed the @Autowire and

I changed the faces-config.xml as you proposed, with a slight difference that:

<property-name>projectBean</property-name>
<property-class>Qualified path for this class</property-class>
<value>#{projectBean}</value>

I need to mention that I m using JSF2.0. I used the <managed-property> and I used nowhere the @ManagedProperty. Still it works fine. Is it something that I should worry about?I m saying that because you mentioned that with JSF2.0 or higher I should use @ManagedProperty

PS: I m sorry that I post it as "asnwer" but I couldn really find a way to update my original question.

panipsilos
  • 2,219
  • 11
  • 37
  • 53
  • You can click "edit" under your question and update the answer.Anyway I mentioned `'@ManagedPropery` only goes with JSF 2.0 cause, from JSF 2.0 ,sun implemented annoatations. Lesser versions doesn't support annoatations.So, you must specify them in `faces-config.xml`.And more thing is If you create your bean in `faces-config.xml` and annotate same bean with `@ManagedBean`at the class level ,container only considers definition in xml file.If you define in `faces-config.xml` and If you do `@ManagedPropery` to inject another bean then your will get Null pointer exception.You should be careful. – SRy Nov 21 '12 at 18:18