[Spring] Integration of container and web environment-servletContext domain listener

When learning spring before, we have been simulating the web layer without using real servlets. Next, we will start the tomcat server, import the servlet/jsp coordinates, and enter the real web environment that we are familiar with.
 
The web environment brings the familiar servletContext/Session/Request domain-think about it, appin which domain should the Spring container ( ) be placed?

 
 

Understanding: Spring container and servletContext domain

Every time there is newan app has obvious drawbacks: the configuration file is loaded multiple times, and the container (context object) is created multiple times.

When entering the web environment we are familiar with, we have a new idea to solve this problem:

The servletContext domain is the largest domain and is only loaded once every time a web project is started. Therefore, we use ServletContextListener to monitor the web project. When the web project starts, we create an app and store it in the servletContext domain. Every time you need to use an app, take it out of the domain.

In other words, the Spring container is created only once when the web project is started. newThe problem of using multiple times to create is solved.

 
 

Implementation: Manually integrate the app into the servletContext domain

① Handwritten ContextLoaderListener listener

public class ContextLoaderListener implements ServletContextListener {
    
    
    public void contextInitialized(ServletContextEvent servletContextEvent) {
    
    
    
        // 1.在web项目启动时创建app上下文对象
        ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
        
        // 2.将app上下文对象存储到最大的域(ServletContext域)中
        ServletContext servletContext = servletContextEvent.getServletContext();
        servletContext.setAttribute("app", app);
        
    }

    public void contextDestroyed(ServletContextEvent servletContextEvent) {
    
    

    }
}

② Configure the listener in web.xml

<!-- 配置监听器 -->
<listener>
	<listener-class>com.samarua.listener.ContextLoaderListener</listener-class>
</listener>

③ Use (Remove the app from the domain)

public class UserServlet extends HttpServlet {
    
    

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    
    
    	// 获取servletContext域
        ServletContext servletContext = req.getServletContext();
        // 从域中取出Spring容器
        ApplicationContext app = (ApplicationContext) servletContext.getAttribute("app");
        // 用一下试试 >_<
        UserService userService = app.getBean(UserService.class);
        userService.sayHi();
    }
	
	@Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    
    
        this.doGet(req, resp);
    }

}

 
 

Implementation: Use the listener and tool classes provided by Spring

① Configure in web.xml

<!-- 全局初始化参数(这是因为在Spring写好的监听器中,xml配置文件的名称不是写死的,而是在这里指定) -->
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
</context-param>
    
<!-- 配置监听器(这次,我们使用Spring写好的监听器) -->
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

② Use (take out the app from the WebApplicationContextUtils tool class)

public class UserServlet extends HttpServlet {
    
    

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    
    
		// // 获取servletContext域
        ServletContext servletContext = req.getServletContext();
        // 从工具类中取出app
        ApplicationContext app = WebApplicationContextUtils.getWebApplicationContext(servletContext);
        // 用一下试试 >_<
        UserService userService = app.getBean(UserService.class);
        userService.sayHi();
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    
    
        this.doGet(req, resp);
    }
}

 
 
 
 

 
 
 
 

 
 
 
 

More >_<

Guess you like

Origin blog.csdn.net/m0_46202073/article/details/114239541