Reputation: 3703
I'm trying to use an application scoped bean in JSF2, but for some reason it is always null
in my request scoped bean. Here's the code I'm using:
The application scoped bean:
@ManagedBean(eager=true, name="applicationTracking")
@ApplicationScoped
public class ApplicationTracking implements Serializable {
private static final long serialVersionUID = 4536466449079922778L;
public ApplicationTracking() {
System.out.println("ApplicationTracking constructed");
}
}
The request scoped bean:
@ManagedBean
@RequestScoped
public class SearchResults implements Serializable {
private static final long serialVersionUID = 4331629908101406406L;
@ManagedProperty("#{applicationTracking}")
private ApplicationTracking appTracking;
public ApplicationTracking getAppTracking() {
return appTracking;
}
public void setAppTracking(ApplicationTracking appTrack) {
this.appTracking = appTrack;
}
@PostConstruct
public void init() {
System.out.println("SearchResults.init CALLED, appTracking = " + appTracking);
}
}
According to everything I'm seeing in the forums this should work without any other configurations. When I start the server (Tomcat) I'm seeing the ApplicationTracking
constructor and init
methods getting called.
But in my SearchResults
component the printout in the PostConstruct
is always null:
SearchResults.init CALLED, appTracking = null
What am I missing?
Upvotes: 1
Views: 224
Reputation: 1108557
Provided that you imported those annotations from the right package javax.faces.bean.*
, then this problem will happen if you re-registered the very same managed bean class in faces-config.xml
on a different managed bean name. Get rid of that faces-config.xml
entry. That's the JSF 1.x style of registering managed beans. You don't need it in JSF 2.x. When you do so anyway, then it will override any annotation based registration on the managed bean class, causing them to be ineffective.
Make sure you don't read JSF 1.x targeted resources while learning and working with JSF 2.x. Many things are done differently in JSF 2.x.
Upvotes: 3