Here's some cool hax for ya, in case other solutions aren't working for you due to various circumstances...
Let's say I have a class Alpha
that I want initialized first:
public class Alpha {
@PostConstruct
public void init() {
}
}
I can put the following method in Alpha
:
@ManagedBean(name = "Alpha", eager = true)
public class Alpha {
public static void requireAlpha() {
FacesContext context = FacesContext.getCurrentInstance();
Object alpha = context.getApplication().evaluateExpressionGet(context, "#{Alpha}", Object.class);
System.out.println("Alpha required: " + alpha.toString());
}
@PostConstruct
public void init() {
}
}
Then, in any classes that are initializing too early, simply call:
Alpha.requireAlpha();
// ...
// (Code that requires Alpha to be initialized first.)
And if there's a ChildAlpha
class that extends Alpha
that you really want to be initialized (rather than the parent), make sure to use "ChildAlpha" instead, in both the name=""
and the EL Expression ("#{}"
).
See here for more infos: Get JSF managed bean by name in any Servlet related class