首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我可以创建带注释的Faces应用程序侦听器类吗?

我可以创建带注释的Faces应用程序侦听器类吗?
EN

Stack Overflow用户
提问于 2013-07-20 09:22:41
回答 1查看 301关注 0票数 2

我想创建一个带有注释的Faces应用程序侦听器,我有以下类:

代码语言:javascript
复制
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的配置,该怎么办?

代码语言:javascript
复制
<!-- 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> 
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-07-20 09:44:32

对此没有注解。更重要的是,你本质上是在使用错误的工具。只需使用eagerly initialized application scoped managed bean即可。

代码语言:javascript
复制
@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上下文侦听器:

代码语言:javascript
复制
@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旨在连接到UIComponentRenderer,而不是独立使用。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17757722

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档