Spring與Web環境整合

憨豆只幹扎瓦發表於2022-05-09

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);

 

相關文章