在我看来,@ ApplicationScoped bean仅在第一次使用EL访问页面时启动.
当我查询ApplicationMap时,是否会创建@ApplicationScoped bean?
- ExternalContext ec = currentInstance.getExternalContext(); result =
- ec.getApplicationMap().get(beanName);
如何在加载XHTML页面之前触发应用程序作用域bean的实例化?
解决方法
@H_404_11@ 您可以在@ManagedBean
声明中使用eager = true.
- @ManagedBean(eager=true)
- @ApplicationScoped
- public class Config {
- // ...
- }
这样bean将在webapp的启动时自动处理.
除此之外,您还可以使用Application#evaluateExpressionGet()
以编程方式评估EL,以便在必要时自动创建bean.另请参阅this answer上的示例.
- FacesContext context = FacesContext.getCurrentInstance();
- Confic config = (Config) context.getApplication().evaluateExpressionGet(context,"#{config}",Config.class);
- // ...
您也可以将它作为您需要它的bean的@ManagedProperty
注入.
- @ManagedBean
- @RequestScoped
- public class Register {
- @ManagedProperty("#{config}")
- private Config config;
- @PostConstruct
- public void init() {
- // ...
- }
- // ...
- }