Spring Web实现原理
Spring WebSpring Web的实现1、JavaEE项目中:(1)spring创建容器:ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml")(2)项目通过main方法运行。2、Web项目中:(1)项目是在服务器上运行的,如:tomcat(2)tomcat启动后,项
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、依赖
<!--servlet依赖-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
<!--jsp依赖-->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2.1-b03</version>
<scope>provided</scope>
</dependency>
<!--监听器依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<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配置文件
<?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">
<servlet>
<servlet-name>GoodsServlet</servlet-name>
<servlet-class>com.spring.controller.GoodsServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>GoodsServlet</servlet-name>
<url-pattern>/buy</url-pattern>
</servlet-mapping>
<!--注册监听器 ContextLoaderListener
监听器对象被创建后,会读取/WEB-INF/spring.xml
为什么读取文件:因为在监听器中要创建ApplicationContext对象,需要加载配置文件。
/WEB-INF/spring.xml 就是监听器默认读取的spring配置文件路径
可以修改spring文件的默认位置,使用context-param重指定配置文件的位置
监听器:目的是创建容器对象,创建了容器对象,就能把spring配置文件中的所有对象都创建好。
-->
<!--注册监听器 ContextLoaderListener-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--重新指定spring配置文件的位置-->
<context-param>
<!--contextConfigLocation:表示配置文件的路径-->
<param-name>contextConfigLocation</param-name>
<!--自定义配置文件的路径-->
<param-value>classpath:spring.xml</param-value>
</context-param>
</web-app>
7、启动tomcat
服务器即可
更多推荐
所有评论(0)