2

注釈付きの Faces アプリケーション Listener を作成したいのですが、次のクラスがあります。

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> 
4

1 に答える 1

2

そのための注釈はありません。さらに、本質的に目的に対して間違ったツールを使用しています。代わりに、熱心に初期化された アプリケーション スコープの マネージド 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 の冗長性を追加する必要はありません。

または、 で利用可能な JSF アーティファクトに関心がない場合は、FacesContext代わりに標準のサーブレット コンテキスト リスナーを使用することもできます。

@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

于 2013-07-20T01:44:31.763 に答える