Spring集成Web環境
分析
應用上下文對象是通過new ClasspathXmlApplicationContext(spring配置文件)
方式獲取的,但是每次從容器中獲得Bean時都要編寫new ClasspathXmlApplicationContext(spring配置文件)
,這樣的弊端是配置文件會加載多次,應用上下文對象創建多次。
在Web項目中,可以使用ServletContextListener
監聽Web應用的啟動,我們可以在Web應用啟動時,就加載Spring的配置文件,創建應用上下文對象ApplicationContext,再將其存儲到最大的域servletContext域中,這樣就可以在任意位置從域中獲。得應用上下文對象ApplicationContext對象了。
實現
Spring提供了一個監聽器ContextLoaderListener就是對上述功能的封裝,該監聽器內部加載Spring配置文件,創建應用上下文對象,並存儲到ServletContext域中,提供了一個客戶端工具WebApplicationContextUtils供使用者獲得應用上下文對象。
操作
- 導入spring-web坐標
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.0.5.RELEASE</version>
</dependency
- 在web.xml中配置ContextLoaderListener監聽器
<!-- 指定配置文件路徑,該標簽盡量放在前面!!!-->
<!-- 全局初始化參數-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!--配置監聽器-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
- 通過工具獲得應用上下文對象
package com.itheima.listener;
import com.itheima.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
public class ContextLoaderListener implements ServletContextListener {
public void contextInitialized(ServletContextEvent servletContextEvent) {
ServletContext servletContext = servletContextEvent.getServletContext();
//讀取web.xml中的全局參數
String contextConfigLocation = servletContext.getInitParameter("contextConfigLocation");
ApplicationContext app = new ClassPathXmlApplicationContext(contextConfigLocation);
//將Spring的應用上下文對象存儲到ServletContext域中
servletContext.setAttribute("app",app);
System.out.println("spring容器創建完畢....");
}
public void contextDestroyed(ServletContextEvent servletContextEvent) {
}
}
package com.itheima.listener;
import org.springframework.context.ApplicationContext;
import javax.servlet.ServletContext;
// 工具類,返回創建好的spring容器
public class WebApplicationContextUtils {
public static ApplicationContext getWebApplicationContext(ServletContext servletContext){
return (ApplicationContext) servletContext.getAttribute("app");
}
}
package com.itheima.web;
import com.itheima.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class UserServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// //ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
ServletContext servletContext = this.getServletContext();
// ApplicationContext app = (ApplicationContext) servletContext.getAttribute("app");
// ApplicationContext app = WebApplicationContextUtils.getWebApplicationContext(servletContext);
// 通過spring-web獲取應用上下文對象
ApplicationContext webApplicationContext = WebApplicationContextUtils.getWebApplicationContext(servletContext);
// 示例獲取創建好的Bean
UserService userService = webApplicationContext.getBean(UserService.class);
userService.save();
}
}