2010-08-22 168 views
7

我在Jboss 4.2.3.GA上運行Stripes Web應用程序,並試圖在啓動JBoss時調用方法。我創建了一個ServletContextListener來,像這樣:爲什麼contextInitialized()被多次調用?

public class TimerContextListener implements ServletContextListener { 

    @Inject 
    private TimerManager timerManager; 

    public void contextInitialized(ServletContextEvent servletcontextevent) { 
     ((Injector) servletcontextevent.getServletContext().getAttribute(GuiceServletContextListener.KEY)).injectMembers(this); 
     timerManager.stopAllTimers(); 
     timerManager.startTimer(); 
    } 

    public void contextDestroyed(ServletContextEvent servletcontextevent) { 

    } 
} 

和我說在web.xml中的條目像這樣:

<listener> 
     <listener-class>com.lawless.web.servletContextListeners.TimerContextListener</listener-class> 
    </listener> 

但contextInitialized()獲取調用3次,當我開始我的服務器。任何想法可能是什麼問題?謝謝。

+1

將基本'System.out.println'你'contextInitialized'的頂部,以確保你所看到的,你認爲你所看到的。順便說一下,我已經看到了這樣一個問題:將Tomcat連接到NetBeans以供開發時使用的已知錯誤會導致[Tomcat雙啓動Web應用程序](https://stackoverflow.com/q/16702011/642706) 。 – 2017-06-01 22:11:52

回答

5

好吧我想通了。它被稱爲3次,因爲我在jboss-web.xml中定義了3個虛擬主機。不知道爲什麼它會導致這種行爲。如果任何人都可以解釋我會欣賞它的原因。

6

每個Web應用程序只有一個ServletContext。在部署應用程序時將創建ServletContext(3個虛擬主機表示部署到3個不同IP地址的主機)。一旦創建了ServletContext,它將被同一應用程序中的所有servlet和JSP文件使用。 ServletContext也被稱爲Web應用程序場景中的應用程序範圍變量。

來源 - http://www.javabeat.net/2009/02/servletcontextlistener-example/

相關問題