I am trying to use the bean created in the spring Container in the JSF ManagedBean using @ManagedProperty annotation.But I am getting null pointer when using that bean.Once I start my server I can see my beans are created Here
Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@9d532ae: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalPersistenceAnnotationProcessor,userBean,userService];
HomepageBean.java
package come.test.backingbean
@ManagedBean
@sessionScoped
public Class HomepageBean{
@ManagedProperty(value="#{userBean}")
private UserBean userBean;// getters and setters
public String doLogin() {
String url = "login.xhtml";
LoginBean manager = new LoginBean(); // This bean has a condition which check for Username and password entered by user.
if (manager.auth(username, password)) {
isLoggedIn = true;
url = "homepage";
String username=sample;
userBean.getUserInfo(username);
} else {
FacesContext context = FacesContext.getCurrentInstance();
context.addMessage(username, new FacesMessage(
"Invalid Username and or Password"));
}
return url;
}
UserBean.java
package com.test.mypackage
@Component
Public Class UserBean{
@Autowired
private UserService userServie // getters and setters.
public void getUserInfo(String userId){
userService.findByUserId(userId)
}
}
}
UserService.java
package com.test.service;
public interface UserService {
public void save(User User);
public void update(User user);
public void delete(User user);
public User findByUserId(String userId);
}
I can see when my server started the bean I am trying to use is pre-instantiated.I am defining my applicationContext.xml in web.xml as a Context-param. And I am defining all the beans in my Spring.xml like this
spring.xml
<?xml version="1.0" encoding="UTF-8"?>
<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:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:annotation-config />
<context:component-scan base-package="com.test" />
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
</beans>
which is in my class path and importing that as a resource in applicationContext.xml.
applicationContext.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:security="http://www.springframework.org/schema/security"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<import resource="classpath:database/DataSource.xml" />
<import resource="classpath:database/Hibernate.xml" />
<import resource="classpath:config/Spring.xml" />
</beans>
my faces-confi.xml
<application>
<el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>
<resource-bundle>
<base-name>com.test.boundles.messages</base-name>
<var>msg</var>
</resource-bundle>
</application>
Any problem with my approach.
There are multiple things that should have been handled better.
1) You just need
<context:component-scan base-package="com.test" />, remove annotation-config and AutowiredAnnotationBeanPostProcessor.See Reason: Difference between <context:annotation-config> vs <context:component-scan>
and Documentation
2) You do not have scope on your UserBean, if you do not mention any scope the default scope will be Singleton which I do not think is desirable in this context.
3) You are trying to use an interface instead of an instantiable class that implements this interface.
4) You should then mark the implementation class with @Service to be autowired.
5) I hope you have getters and setters instead of just those comments.
For a good example refer to this link
See also: