Spring—整合MVC框架
笔记整理自 b站 孙哥说Spring5
MVC框架整合思想
Ⅰ. 为什么要整合MVC框架
1. MVC框架提供了控制器(Controller)调用Service
DAO => Service
2. 请求响应的处理
3. 接受请求参数 request.getParameter("")
4. 控制程序的运行流程
5. 视图解析(JSP JSON Freemarker Thyemeleaf)
SpringMVC和Spring整合目的:分工明确
- SpringMVC的配置文件就来配置和网站转发逻辑以及网站功能有关的
(视图解析器,拦截器,文件上传解析器)
- Spring的配置文件用来配置和
业务有关的(事务处理 数据源 xxx.... )
整合
SpringMVC和Spring分容器
- Spring的容器除了Controller不扫其他组件全扫
- SpringMVC的容器
只扫描控制器(Controller)
父子容器
如果有两个容器,一个Spring的容器,一个SpringMVC的容器,Spring框架的默认将Spring的容器ApplicationContext作为父容器,SpringMVC的容器作为子容器,子容器(MVC的容器)可以自动装配父容器(Spring的容器)中的Bean,但是父容器不能装配子容器中的Bean
,例如子容器中的Controller可以自动装配父容器中的Service,但是父容器中的Service不能装配子容器中的Controller等。
Ⅱ. Spring可以整合哪些MVC框架
1. Struts1
2. WebWork
3. JSF
4. Struts2
5. SpringMVC
Ⅲ. 搭建Web运行环境
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet.jsp/javax.servlet.jsp-api -->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.1.14.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.1.14.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>5.1.14.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.1.14.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.1.14.RELEASE</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.2</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.18</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.48</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>5.1.14.RELEASE</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.8.8</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.3</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.25</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
Ⅳ. Spring整合MVC框架的核心思路
1️⃣ 准备工厂
1. Web开发过程中如何创建工厂
ApplicationContext ctx = new ClassPathXmlApplicationContext("/applicationContext.xml");
WebXmlApplicationContext()
2. 如何保证工厂唯一同时被共用
被共用:Web request|session|ServletContext(application)
工厂存储在ServletContext这个作用域中 ServletContext.setAttribute("xxx", ctx);
唯一:ServletContext对象 创建的同时 => ApplicationContext ctx = new ClassPathXmlApplicationContext("/applicationContext.xml");
ServletContextListener => ApplicationContext ctx = new ClassPathXmlApplicationContext("/applicationContext.xml");
ServletContextListener 在ServletContext对象创建的同时,被调用(只会被调用一次),
把工厂创建的代码,写在ServletContextListener中,也会保证只调用一次,最终工厂就保证了唯一性
3. 总结
ServletContextListener(唯一)
ApplicationContext ctx = new ClassPathXmlApplicationContext("/applicationContext.xml");
ServletContext.setAttribute("xxx", ctx) (共用)
4. Spring封装了一个ContextLoaderListener
1. 创建工厂
2. 把工厂存在ServletContext中
我们简单看一下ContextLoaderListener类
的源码:
所以Spring提供的ContextLoaderListener类
本质上就是以第
3
3
3 步总结基于ServletContextListener类
实现的。
ContextLoaderListener使用方式
web.xml
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
2️⃣ 代码整合
依赖注入:把Sevice对象注入个控制器对象。
Ⅴ. Spring多配置文件的处理
Spring会根据需要,把配置信息分门别类的放置在多个配置文件中,便于后续的管理及维护。
DAO ------ applicationContext-dao.xml
Service --- applicationContext-service.xml
Action --- applicationContext-action.xml
注意:虽然提供了多个配置文件,但是后续应用的过程中,还要进行整合
-
通配符方式
1. 非web环境 ApplicationContext ctx = new ClassPathXmlApplicationContext("/applicationContext-*.xml"); 2. web环境 <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext-*.xml</param-value> <context-param>
-
import标签
applicationContext.xml 目的 整合其他配置内容 <import resource="applicationContext-dao.xml" /> <import resource="applicationContext-service.xml" /> <import resource="applicationContext-action.xml" /> 1. 非web环境 ApplicationContext ctx = new ClassPathXmlApplicationContext("/applicationContext.xml"); 2. web环境 <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> <context-param>