2013-11-27 53 views
2

我看到了很多的源代碼,開發人員在ServletContextListener來實現(例如)編碼方式類似於下面是ServletContext的-的removeAttribute中的ServletContextListener,contextDestroyed需要

public class ServletContextListenerImpl implements ServletContextListener { 

    @Override 
public void contextInitialized(ServletContextEvent sce) { 
    sce.getServletContext().setAttribute("attribute1", new Bean1()); 
} 

@Override 
public void contextDestroyed(ServletContextEvent sce) { 
    sce.getServletContext().removeAttribute("attribute1"); //--- LINE_REMOVE 
} 

我的問題是,我們真的需要有行標「LINE_REMOVE 「在上面的例子中?

我想我們不需要它,因爲當contextDestroyed執行時,servletContext即將完全銷燬,所以它的屬性也會被刪除。

感謝您的想法。

回答

4

雖然通常是不需要的,則可能是您正在尋找的代碼也有其監聽範圍內的,移除這些自定義ServletContextAttributeListener屬性和執行相應的一些行動。

+0

謝謝。我接受了你的想法。 – Loc

0

在銷燬ServletContext後不需要刪除屬性。但是,如果你要聽屬性取出事件,您可以使用ServletContextAttributeListener

+0

謝謝。你的想法與@Sotirios Delimanolis的想法很相似。 – Loc

相關問題