I am trying to inject a service into my bean but it is always null
.
I get the following error: WELD-001000 Error resolving property userBean against base null.
The error message could be a hint, that the JVM wasn't able to create an instance of UserBean
. The following is some guessing and would have to be proven:
Dependecy Injection requires a dependency injector, a tool that injects an instance of UserService
into a UserBean
. In your code you're already using this injected instance during instantiation of the bean: you call the injected service in the constructor.
If the dependency injector starts it's work after the bean is created, then the call to the service inside the constructor will raise a NullPointerException
(because service
is still null
at that time). It's worth checking that by trying to catch NPEs in the UserBean
constructor for a moment. If you catch one - voilà - the dependency injector starts runnning after the bean has been created and, as a consequence, we can't use injected services during class instantiation (= in the constructor)
Workaround idea: implement a small service provider helper class - inner class could work:
public class UserBean implements Serializable {
static class UserServiceProvider {
@Inject static UserService service;
}
// ...
public UserBean() {
this.user = UserServiceProvider.service.findUser("kaas");
}
// ...
}
Untested but could work - the service should be injected in the provider class before you use it in the beans constructor.
Another alternative is use @PostConstruct method annotation.
@SessionScoped
public class UserBean implements Serializable {
@Inject UserService service;
private User user;
public UserBean() {
}
@PostConstruct
void init(){
this.user = service.findUser("kaas");
}
}
Read docs