A)ServletContext servletContext = request.getSession().getServletContext(); mysql
ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(servletContext); web
B)String contextpath = "org.springframework.web.context.WebApplicationContext.ROOT";spring
WebApplicationContext context = request.getSession().getServletContext().getAttribute(contextpath); sql
2、不提倡的方法:(这种写法不单单耗内存,占资源,并且若是数据库链接太多,很容易形成系统运行的缓慢甚至stop!)数据库
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");app