JSF2 ApplicationScope bean instantiation time?

It seems to me that @ApplicationScoped beans are only triggered the first time a page is accessed using EL.

When I request ApplicationMap , will the @ApplicationScoped bean be created?

 ExternalContext ec = currentInstance.getExternalContext(); result = ec.getApplicationMap().get(beanName); 

How else can I start instantiating a bean application before loading the XHTML page?

+6
source share
1 answer

You can use eager=true in the @ManagedBean .

 @ManagedBean(eager=true) @ApplicationScoped public class Config { // ... } 

Thus, the bean will be auto-acces when launching webapp.

Instead, you can also use Application#evaluateExpressionGet() to programmatically evaluate EL and therefore automatically create a bean. See also an example of this answer .

 FacesContext context = FacesContext.getCurrentInstance(); Confic config = (Config) context.getApplication().evaluateExpressionGet(context, "#{config}", Config.class); // ... 

You can also just enter it as @ManagedProperty bean where you need it.

 @ManagedBean @RequestScoped public class Register { @ManagedProperty("#{config}") private Config config; @PostConstruct public void init() { // ... } // ... } 

JSF will automatically create it before injection into the parent bean. It is available in all methods besides @PostConstruct .

+9
source

Source: https://habr.com/ru/post/894443/


All Articles