我想创建一个带有注释的Faces应用程序侦听器,我有以下类:
package com.chhibi.listener;
import javax.faces.application.Application;
import javax.faces.event.AbortProcessingException;
import javax.faces.event.PostConstructApplicationEvent;
import javax.faces.event.PreDestroyApplicationEvent;
import javax.faces.event.SystemEvent;
import javax.faces.event.SystemEventListener;
public class FacesAppListener implements SystemEventListener {
@Override
public void processEvent(SystemEvent event) throws AbortProcessingException {
if (event instanceof PostConstructApplicationEvent) {
// other code here
}
if (event instanceof PreDestroyApplicationEvent) {
//other code here
}
}
@Override
public boolean isListenerForSource(Object source) {
// only for Application
return (source instanceof Application);
}
}我想用注解替换faces-config.xml的配置,该怎么办?
<!-- Application is started -->
<system-event-listener>
<system-event-listener-class>
com.chhibi.listenner.FacesAppListener
</system-event-listener-class>
<system-event-class>
javax.faces.event.PostConstructApplicationEvent
</system-event-class>
</system-event-listener>
<!-- Before Application is shut down -->
<system-event-listener>
<system-event-listener-class>
com.chhibi.listenner.FacesAppListener
</system-event-listener-class>
<system-event-class>
javax.faces.event.PreDestroyApplicationEvent
</system-event-class>
</system-event-listener> 发布于 2013-07-20 09:44:32
对此没有注解。更重要的是,你本质上是在使用错误的工具。只需使用eagerly initialized application scoped managed bean即可。
@ManagedBean(eager=true)
@ApplicationScoped
public class MyApplicationBean {
@PostConstruct
public void onPostConstruct() {
// Put code here which should be executed on application's startup.
}
@PreDestroy
public void onPreDestroy() {
// Put code here which should be executed on application's shutdown.
}
}就这样。不需要额外的XML冗长。
或者,如果您对FacesContext提供的JSF工件不感兴趣,那么您也可以使用标准的servlet上下文侦听器:
@WebListener
public class MyApplicationListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent event) {
// Put code here which should be executed on application's startup.
}
@Override
public void contextDestroyed(ServletContextEvent event) {
// Put code here which should be executed on application's shutdown.
}
} 同样在这里,不需要额外的XML。
SystemEventListener旨在连接到UIComponent或Renderer,而不是独立使用。
https://stackoverflow.com/questions/17757722
复制相似问题