Spring Web实现原理

Spring Web

  • 需求
  • Spring Web的实现: spring的ContextLoaderListener(关键代码)

1、JavaEE项目中:
(1)spring创建容器:ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
(2)项目通过main方法运行。
2、Web项目中:
(1)项目是在服务器上运行的,如:tomcat
(2)tomcat启动后,项目一直运行。

需求

JavaSE项目中使用ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");这种方式在web方法中随着请求次数增大的时候,创建容器和对象的压力会增大。而Web项目中容器对象只需要创建一次就能全局使用,把容器对象放入全局作用于ServletContext中。
实现:使用监听器,当全局作用域被创建时,创建容器对象存入ServletContext中。
监听器的作用
(1)创建容器对象,执行ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
监听器:目的是创建容器对象,创建了容器对象,就能把spring配置文件中的所有对象都创建好。
(2)把容器对象存放到ServletContext,ServletContext.setAttribute(key,context)。
监听器可以自己创建,也可以使用框架中提供的 ContextLoaderListener

Spring Web的实现: spring的ContextLoaderListener(关键代码)

1、新建maven的web项目
2、依赖

		 
        <dependency>
            <groupId>javax.servletgroupId>
            <artifactId>javax.servlet-apiartifactId>
            <version>4.0.1version>
            <scope>providedscope>
        dependency>
        
        <dependency>
            <groupId>javax.servlet.jspgroupId>
            <artifactId>jsp-apiartifactId>
            <version>2.2.1-b03version>
            <scope>providedscope>
        dependency>
        
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-webartifactId>
            <version>${spring.version}version>
        dependency>

3、dao、service层代码参考:https://blog.csdn.net/qq_36763419/article/details/113938719
4、spring配置文件,不变动。可参考:同上

5、控制器(servlet)

package com.spring.controller;

import com.spring.service.BuyGoodsService;
import com.spring.service.BuyGoodsServiceImpl;
import com.spring.utils.SpringApplicationContextUtils;
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 GoodsServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int gid = Integer.valueOf(request.getParameter("goodId"));
        int nums = Integer.valueOf(request.getParameter("nums"));

        /*创建spring的容器对象*/
//        String config = "classpath:spring.xml";
//        ApplicationContext context = new ClassPathXmlApplicationContext(config);
//        System.out.println("容器对象的信息===" + context);
//        BuyGoodsService service = (BuyGoodsService) context.getBean("buyService");

        /*手动工具类获取容器对象*/
        WebApplicationContext context = null;
//        //获取ServletContext中的容器对象,创建好的容器对象
//        String key = WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE;
//        Object attr = getServletContext().getAttribute(key);
//        if (attr !=null){
//            context = (WebApplicationContext) attr;
//        }

        /*使用框架中的方法获取容器对象*/
        ServletContext servletContext = getServletContext();
        context = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
        BuyGoodsService service = (BuyGoodsService) context.getBean("buyService");
//        BuyGoodsService service = SpringApplicationContextUtils.getContext(servletContext,"buyService");
        service.buy(gid, nums);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


    }
}

6、web.xml配置文件


<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">
    <servlet>
        <servlet-name>GoodsServletservlet-name>
        <servlet-class>com.spring.controller.GoodsServletservlet-class>
    servlet>
    <servlet-mapping>
        <servlet-name>GoodsServletservlet-name>
        <url-pattern>/buyurl-pattern>
    servlet-mapping>

    
    
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
    listener>
    
    <context-param>
        
        <param-name>contextConfigLocationparam-name>
        
        <param-value>classpath:spring.xmlparam-value>
    context-param>
web-app>

7、启动tomcat服务器即可

你可能感兴趣的:(Spring(mvc,boot,持久层框架),servlet,spring)