@ManagedBean(name="helloBean")
@SessionScoped
public class HelloBean implements Serializable {
@Inject
@Named("loginService")
LoginService loginService;
private String name;
public String getName() {
return name+"-->"+loginService.serviceDetails();
}
public void setName(String name) {
this.name = name;
}
}
…
@Named("loginService")
public class LoginServiceImpl implements LoginService {
public String serviceDetails() {
return "==From Service==";
}
}
ApplicationContext.xml file:
<beans
<context:component-scan base-package="com.myapp"/>
</beans>
I am getting null pointer exception for loginService at return
name+"-->"+loginService.serviceDetails();
1) How can I solve above problem
2) instead of @ManagedBean(name="helloBean") can I use @Inject? how to do that?
I don’t think you can use
@ManagedBeanand and spring at the same time. If you want your beans to be managed by spring, you need to use the springELResolver(in faces-config.xml) and then use@Namedand@Scope("session")on the JSF beans as well. Then you can (and must) use@Injectrather than@ManagedXAnd as a sidenote – avoid referring to non-jsf beans by name – rely on their type only – it will save you headaches with refactoring, at least. And for the JSF beans you can rely on the default naming and use
@Namedwithout arguments.