This is my current scenario:
@WebListener
public class WebListenerService implements HttpSessionListener{
.... implement methods
@Produces
@Dependent
public SessionDependentService sessionDependentService(){
}
}
@SessionScoped
@Named
public class AccountController implements Serializable{
//Injected properly and works as expected
@Inject
private SessionDependnetService sessionDependentService;
@Inject
@OnLogin
private Event<Account> accountEvent;
public void onLogin(){
accountEvent.fire(authenticatedAccount);
}
}
@SessionScoped
public class AccountObserver implements Serializable{
//This does not work. It is always null.
@Inject
private SessionDependnetService sessionDependentService;
public void onLoginEvent(@Observes @OnLogin final Account account) {
//When this methods is invoked
//the sessiondependentservice is always null here.
}
}
In the AccountController, the SessionDependentService is correctly injected and is not null, while in the AccountObserver, it is always null.
EDIT: Event using the parameter injection still results to a null value.
public void onLoginEvent(@Observes @OnLogin final Account account, final SessionDependnetService sessionDependentService) {
//When this methods is invoked
//the sessiondependentservice is always null here.
}
Netbeans correctly highlights this as an injection point.
Why is this the case?
I am using wildfly 8 server.
I changed the producer bean from SessionScoped to Stateless bean:
@Stateless
public class WebListenerSessionService {
//Use Instance since http session are dynamic.
@Inject
private Instance<HttpSession> httpSession;
@Produces
@Dependent
public SessionDependentService sessionDependentService(){
//use session to lookup existing service or produce a new one.
}
}
Even though this works fine, there is no where in the CDI spec which says Producer method must be session beans.
to quote:
A producer method must be a default-access, public, protected or private, non-abstract method of a managed bean class or session bean class. A producer method may be either static or non- static. If the bean is a session bean, the producer method must be either a business method of the EJB or a static method of the bean class.
And since @SessionScoped is a managed bean class, why would it an injection into an observer bean not work.