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