平台:
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?如果是这样,为什么?
提前感谢任何帮助解决这个问题。