利用ServletContextListener 获取spring上下文
?
import javax.servlet.ServletContextEvent;import javax.servlet.ServletContextListener;import org.springframework.context.ApplicationContext;import org.springframework.web.context.support.WebApplicationContextUtils;public class SpringContextListener implements ServletContextListener {private ApplicationContext applicationContext;public void contextDestroyed(ServletContextEvent arg0) {// TODO Auto-generated method stub}public void contextInitialized(ServletContextEvent event) {applicationContext = WebApplicationContextUtils.getWebApplicationContext(event.getServletContext()); //获取spring上下文//applicationContext.getBean("userDao");}}
?
web.xml
?
<listener> <listener-class>com.SpringContextListener</listener-class> </listener>?
?