2012-03-22 70 views
0

我有使用彈簧的網絡應用程序,其中我有locads上下文在其init方法一個servlet:我可以在servlet之後進行過濾器初始化嗎?

private ContextLoader contextLoader; 

public void init() throws ServletException { 
    contextLoader = new ContextLoader(); 
    contextLoader.initWebApplicationContext(getServletContext()); 
} 

此外,我有一個servlet在其中予執行以下操作:

public void init(FilterConfig config) throws ServletException { 
    WebApplicationContext context = 
     WebApplicationContextUtils.getWebApplicationContext(config.getServletContext()); 
    //here I'm using the context 
} 

問題:在servlet初始化之前調用過濾器的init()方法,所以我在過濾器中獲得的上下文爲null。在web.xml中,我的servlet使用load-on-startup = 1進行配置。

有沒有什麼辦法可以讓我的過濾器在servlet初始化後初始化,這樣我就可以在過濾器中使用WebApplicationContext了?

謝謝!

回答

1

試試這個:

  1. 不要配置過濾器在web.xml
  2. 在你的servlet,得到ServletContext中。
  3. 調用ServletContext.addFilter()添加您的過濾器。
相關問題