Spring與Web環境集成 1. ApplicationContext應用上下文獲取方式 應用上下文對象是通過 new ClassPathXmlApplicationContext(Spring配置文件) 方式獲取的,但是每次從容器中獲取Bean時都要編寫 new ClassPathXmlAppl ...
Spring與Web環境集成
1. ApplicationContext應用上下文獲取方式
應用上下文對象是通過 new ClassPathXmlApplicationContext(Spring配置文件) 方式獲取的,但是每次從容器中獲取Bean時都要編寫 new ClassPathXmlApplicationContext(Spring配置文件),這樣的弊端是配置文件載入多次,應用上下文對象創建多次。
在Web項目中,可以使用ServletContextLIstener監聽Web應用的啟動,我們可以在Web應用啟動時,就載入Spring的配置文件,創建應用上下文對象ApplicationContext,再將其存儲到最大的域servletContext域中,這樣就可以在任意位置獲取應用上下文ApplicationContext對象。
2. 創建案例
1、創建監聽器
package com.ntect.listener; 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 sce) { ServletContext servletContext = sce.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 sce) { } }
2、創建獲取applicationContext對象的工具類
package com.ntect.listener;
import org.springframework.context.ApplicationContext;
import javax.servlet.ServletContext;
public class WebApplicationContextUtils {
public static ApplicationContext getWebApplicationContext(ServletContext servletContext) {
return (ApplicationContext)servletContext.getAttribute("app");
}
}
3、創建servlet的測試類
package com.ntect.web;
import com.ntect.listener.WebApplicationContextUtils;
import com.ntect.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
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);
UserService userService = app.getBean(UserService.class);
userService.save();
}
}
4、配置web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<!-- 全局初始化參數-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>applicationContext.xml</param-value>
</context-param>
<!-- 配置監聽器-->
<listener>
<listener-class>com.ntect.listener.ContextLoaderListener</listener-class>
</listener>
<!-- 配置測試用的servlet-->
<servlet>
<servlet-name>UserServlet</servlet-name>
<servlet-class>com.ntect.web.UserServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>UserServlet</servlet-name>
<url-pattern>/userServlet</url-pattern>
</servlet-mapping>
</web-app>
5、測試
瀏覽器訪問
http://localhost:8080/userServlet
控制台輸出
3. Spring提供獲取應用上下文的工具
上面地分析只是為了方便理解,不用手動實現,Spring提供了一個監聽器ContextLoaderListener就是對上述功能的封裝,該監聽器內部載入Spring配置文件,創建應用上下文對象,並存儲到ServletContext域中,提供了一個客戶端工具WebApplicationContextUtils獲取應用上下文對象。
需要做兩件事:
1、在web.xml中配置ContextLoaderListener監聽器(導入spring-web坐標)
2、使用WebApplicationContextUtils獲得應用上下文對象ApplicationContext
3.1 導入Spring集成web的坐標
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.3.16</version>
</dependency>
3.2 配置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>
3.3 使用WebApplicationContextUtils獲得應用上下文對象ApplicationContext
ApplicationContext app = WebApplicationContextUtils.getWebApplicationContext(servletContext);
UserService userService = app.getBean(UserService.class);