I have a problem where injection fails on first try but not on second. My app is laid out like this (this is a Java EE application using Maven in Eclipse):
@Stateful
@Named("myBean")
@SessionScoped
public class MyBean implements Serializable {
@Inject
private User user;
...
@PostConstruct
public String init() {
.. do some general tasks, no issues here.
}
public String initApplication() { // this gets called on page load
String userId = user.getId();
...
}
.... in another class...
@Produces
@SessionScoped
@Named("user")
public User produceUser() {
// code to create user is here
}
Symptom: I start the browser, clear the cache, start the application. I get a null on user the @Produces method is never called. I call the same URL again to start the application, and then it works.