如何使用 PostConstructApplicationEvent 初始化应用程序范围的受管 Bean



Platform:
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 期间检索以编程方式管理的 Bean?如果是这样,为什么?

提前感谢您对这个问题的任何帮助。

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

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

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

      @ManagedBean(eager=true)
    

完成工作

相关内容

  • 没有找到相关文章

最新更新