スプリングwebプロジェクトでは、プロジェクトが起動完了したかどうかを判断します.
3462 ワード
この記事は鬼見ネットで同時に発表されています.https://faceghost.com/article/483341
概説:springロードが完了したら、いくつかの初期化作業をすることがあります.キャッシュ、DBなどをロードして、ここでServlet Contect Listenerインターフェースを実現して、判断に来ます.
特に注意するのは、私たちがweb.xmlにこのlistenerを配置するには、springの下に必ず配置します.
web.xml
class>org.springframework.web.context.ContextLoaderListener class>
class>com.xx.listener.WebAppContentListener class>
WebAppContentListener.javapackage com.xx.listener;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
public class WebAppContentListener implements ServletContextListener {
private static final Logger log = Logger.getLogger(WebAppContentListener.class);
public static ApplicationContext WEB_APP_CONTEXT = null;
@Override
public void contextInitialized(ServletContextEvent sce) {
WEB_APP_CONTEXT = WebApplicationContextUtils.getWebApplicationContext(sce.getServletContext());
if(WEB_APP_CONTEXT != null) {
log.info("started ...");/**
* 。
*
* ApplicationContext spring bean
*
* e.g.
*
* WEB_APP_CONTEXT.getBean(args)
*
*/
}
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
}
}