1、定义一个监听器[MyContextListener],此类里最主要获取springContext和context
package my.request; import javax.servlet.ServletContext; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; import org.springframework.web.context.WebApplicationContext; import org.springframework.web.context.support.WebApplicationContextUtils; /** * @author chaox * @description 通过监听器获取springContext和context,此时普通类只需要提供get和set方法来获取springContext和context */ public class MyContextListener implements ServletContextListener { private ServletContext context = null; // 获取spring注入的bean对象 private WebApplicationContext springContext = null; @Override public void contextDestroyed(ServletContextEvent event) { // Output a simple message to the server‘s console System.out.println("The Simple Web App. Has Been Removed"); this.context = null; } // 这个方法在Web应用服务做好接受请求的时候被调用。 @Override public void contextInitialized(ServletContextEvent event) { this.context = event.getServletContext(); if (context == null) { System.out.println("获取ServletContext上下文失败!"); return; } ApplicationContextHelper.setServletContext(context); // context.setAttribute("xxxKey", "applicationScope"); // context.getAttribute("xxxKey"); springContext = WebApplicationContextUtils.getWebApplicationContext(this.context); if (springContext != null) { // 获取制定的Sprign的beanId=xxxManager的对象 // xxxManager = (XXXManager) springContext.getBean("xxxManager"); ApplicationContextHelper.setContext(springContext); } else { System.out.println("获取应用程序上下文失败!"); return; } //Output a simple message to the server‘s console System.out.println("The Simple Web App. Is Ready"); } public ServletContext getContext() { return context; } public void setContext(ServletContext context) { this.context = context; } public WebApplicationContext getSpringContext() { return springContext; } public void setSpringContext(WebApplicationContext springContext) { this.springContext = springContext; } }
2、定义一个普通类[ApplicationContextHelper],此类里最主要提供get和set方法来获取监听器中的springContext和context,从而获取springbean和application范围的对象
package my.request; import javax.servlet.ServletContext; import org.springframework.context.ApplicationContext; public class ApplicationContextHelper { private static ApplicationContext context = null; private static ServletContext servletContext = null; public static ApplicationContext getContext() { return context; } public static void setContext(ApplicationContext context) { ApplicationContextHelper.context = context; } public static ServletContext getServletContext() { return servletContext; } public static void setServletContext(ServletContext servletContext) { ApplicationContextHelper.servletContext = servletContext; } }
3、web.xml配置,需要注意ContextLoaderListener配置的位置需要在自定义的MyContextListener前,否则springContext=null
<listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <!-- 配置获取springContext和context --> <listener> <listener-class>my.request.MyContextListener</listener-class> </listener>
监听器和普通类获取springContext和context,从而获取springbean和application范围的对象
原文:http://www.cnblogs.com/maven-chao/p/4872714.html