如何使用PostConstructApplicationEvent初始化应用程序范围的Managed Bean?

时间:2012-12-14 19:31:57

标签: events jsf-2 event-handling el systemeventlistener

平台:
JSF(Mojarra)2.1.14

我正在尝试使用PostConstructApplicationEvent初始化应用程序范围的托管bean,这是因为我有一个重要的约束,即不使用@PostConstruct注释,因为所需的构建工具使用Java 5进行编译。为此我实现了SystemEventListener这个类中的接口

public class ApplicationListener implements SystemEventListener {

    private final static Logger LOGGER = Logger
            .getLogger(ApplicationListener.class.getName());

    public boolean isListenerForSource(Object app) {
        boolean isApp = (app instanceof Application);  
        return isApp;
    }

    public void processEvent(SystemEvent event) throws AbortProcessingException {
         try {
        if (event instanceof PostConstructApplicationEvent) {
             Application app = ((PostConstructApplicationEvent) event).getApplication();
             LOGGER.info("PostConstructApplicationEvent is Called");
             FacesContext context = FacesContext.getCurrentInstance();
            MyBean myBean = ((PostConstructApplicationEvent) event).getApplication().evaluateExpressionGet(context,"#{myBean}", MyBean.class);
             myBean.init();
         } catch (Exception e) {
             FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error while initiliazing app bean", e.getMessage());
             LOGGER.error("Error while initiliazing app bean", e);
         }
        }
    }
}  

问题是我得到了这些例外:

2012-12-14 12:19:48,822 INFO [ApplicationListener] - PostConstructApplicationEvent is Called
14/12/2012 12:19:48 PM com.sun.faces.config.ConfigureListener contextInitialized
GRAVE: Critical error during deployment: 
com.sun.faces.config.ConfigurationException: CONFIGURATION FAILED! null
    at com.sun.faces.config.ConfigManager.initialize(ConfigManager.java:376)
    at com.sun.faces.config.ConfigureListener.contextInitialized(ConfigureListener.java:223)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4791)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5285)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
    at java.util.concurrent.FutureTask.run(FutureTask.java:138)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    at java.lang.Thread.run(Thread.java:619)
Caused by: java.lang.NullPointerException
    at com.sun.faces.application.ApplicationImpl.evaluateExpressionGet(ApplicationImpl.java:426)
    at com.myproject.event.ApplicationListener.processEvent(ApplicationListener.java:39)
    at javax.faces.event.SystemEvent.processListener(SystemEvent.java:106)
    at com.sun.faces.application.ApplicationImpl.processListeners(ApplicationImpl.java:2169)
    at com.sun.faces.application.ApplicationImpl.invokeListenersFor(ApplicationImpl.java:2145)
    at com.sun.faces.application.ApplicationImpl.publishEvent(ApplicationImpl.java:303)
    at com.sun.faces.config.ConfigManager.publishPostConfigEvent(ConfigManager.java:601)
    at com.sun.faces.config.ConfigManager.initialize(ConfigManager.java:370)
    ... 11 more
14/12/2012 12:19:48 PM org.apache.catalina.core.StandardContext listenerStart
GRAVE: Excepción enviando evento inicializado de contexto a instancia de escuchador de clase com.sun.faces.config.ConfigureListener
java.lang.RuntimeException: com.sun.faces.config.ConfigurationException: CONFIGURATION FAILED! null
    at com.sun.faces.config.ConfigureListener.contextInitialized(ConfigureListener.java:290)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4791)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5285)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
    at java.util.concurrent.FutureTask.run(FutureTask.java:138)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    at java.lang.Thread.run(Thread.java:619)
Caused by: com.sun.faces.config.ConfigurationException: CONFIGURATION FAILED! null
    at com.sun.faces.config.ConfigManager.initialize(ConfigManager.java:376)
    at com.sun.faces.config.ConfigureListener.contextInitialized(ConfigureListener.java:223)
    ... 10 more
Caused by: java.lang.NullPointerException
    at com.sun.faces.application.ApplicationImpl.evaluateExpressionGet(ApplicationImpl.java:426)
    at com.myproject.event.ApplicationListener.processEvent(ApplicationListener.java:39)
    at javax.faces.event.SystemEvent.processListener(SystemEvent.java:106)
    at com.sun.faces.application.ApplicationImpl.processListeners(ApplicationImpl.java:2169)
    at com.sun.faces.application.ApplicationImpl.invokeListenersFor(ApplicationImpl.java:2145)
    at com.sun.faces.application.ApplicationImpl.publishEvent(ApplicationImpl.java:303)
    at com.sun.faces.config.ConfigManager.publishPostConfigEvent(ConfigManager.java:601)
    at com.sun.faces.config.ConfigManager.initialize(ConfigManager.java:370)
    ... 11 more
14/12/2012 12:19:48 PM org.apache.catalina.core.StandardContext startInternal
GRAVE: Error listenerStart
14/12/2012 12:19:48 PM org.apache.catalina.core.StandardContext startInternal
GRAVE: Falló en arranque del Contexto [/calibre] debido a errores previos
14/12/2012 12:19:48 PM javax.faces.FactoryFinder$FactoryManager getFactory
GRAVE: La aplicación no se ha inicializado correctamente durante el inicio, no se encuentra la fábrica: javax.faces.application.ApplicationFactory. Attempting to find backup.
14/12/2012 12:19:48 PM com.sun.faces.config.ConfigureListener contextDestroyed
GRAVE: Unexpected exception when attempting to tear down the Mojarra runtime
java.lang.IllegalStateException: Could not find backup for factory javax.faces.application.ApplicationFactory. 
    at javax.faces.FactoryFinder$FactoryManager.getFactory(FactoryFinder.java:1010)
    at javax.faces.FactoryFinder.getFactory(FactoryFinder.java:342)
    at com.sun.faces.config.InitFacesContext.getApplication(InitFacesContext.java:136)
    at com.sun.faces.config.ConfigureListener.contextDestroyed(ConfigureListener.java:328)
    at org.apache.catalina.core.StandardContext.listenerStop(StandardContext.java:4831)
    at org.apache.catalina.core.StandardContext.stopInternal(StandardContext.java:5478)
    at org.apache.catalina.util.LifecycleBase.stop(LifecycleBase.java:232)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:160)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
    at java.util.concurrent.FutureTask.run(FutureTask.java:138)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    at java.lang.Thread.run(Thread.java:619)

现在,我已经开始了解问题的原因是ELContext已从InitFacesContext释放,因此在尝试调用getELContext()方法时会抛出NullPointerException。

所以我的问题是,我在这里做错了什么?我们是否应该在PostConstructApplicationEvent期间不以编程方式检索Managed Beans?如果是这样,为什么?

提前感谢任何有助于解决这个问题的帮助。

1 个答案:

答案 0 :(得分:2)

虽然我无法解释您目前正在处理的堆栈跟踪,

  1. J2SE 5支持注释,实际上它们是在5中引入的。

  2. 我可以告诉你JSF通过一个简单的注释参数来满足你的用例(急切加载bean),这将确保你的@ApplicationScoped bean被实例化@ startup。将eager=true添加到@ManagedBean批注中,如

    中所示
      @ManagedBean(eager=true)
    
  3. 完成工作