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