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.
Some code snippets:
index.xhtml
<h:body>
Hello from Facelets
#{userBean.name}
</h:body>
userbean.java
package beans;
import Domain.User;
import java.io.Serializable;
import javax.enterprise.context.SessionScoped;
import javax.inject.Inject;
import javax.inject.Named;
import service.UserService;
@Named
@SessionScoped
public class UserBean implements Serializable{
@Inject UserService service;
private User user;
public UserBean(){
this.user = service.find_user("foo");
}
public String getName(){
return "bar";
}
}
UserService.java
package service;
import Domain.User;
import javax.ejb.Stateless;
import javax.inject.Named;
@Named
@Stateless
public class UserService {
public UserService(){}
public User find_user(String name){
return new User();
}
}
@Inject can apply to at most one constructor per class. @Inject is optional for public, no-argument constructors when no other constructors are present. This enables injectors to invoke default constructors.
@Inject() is a manual mechanism for letting Angular know that a parameter must be injected. It can be used like so: import { Component, Inject } from '@angular/core'; import { ChatWidget } from '../components/chat-widget';
The @Injectable() decorator defines a class as a service in Angular and allows Angular to inject it into a component as a dependency. Likewise, the @Injectable() decorator indicates that a component, class, pipe, or NgModule has a dependency on a service.
Guice forbids null by default So if something tries to supply null for an object, Guice will refuse to inject it and throw a NULL_INJECTED_INTO_NON_NULLABLE ProvisionException error instead. If null is permissible by your class, you can annotate the field or parameter with @Nullable .
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With