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);
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/471756.html
標籤:Java
