一、SSM框架的联系
SpringMVC是一个表述层框架,可以处理浏览器发送的服务器中的请求,并且将一些数据响应到浏览器
Mybatis是一个持久层框架,帮助我们来连接数据库,访问数据库,操作数据库中的数据
Spring是一个整合型框架,可以使用他的两个核心,叫做IOC和AOP,通过IOC来管理对象,比如Mybatis里面操作数据库的sqlsession对象,可以直接交给spring来进行管理,通过Mybatis操作数据库的过程中,要想实现事务功能,我们也可以使用Spring中AOP的一个重要应用,叫做声明式事务来实现。Mybatis可以交给Spring来进行整合,来进行管理,而Spring和SpringMVC本身就是同源的,都是属于Spring家族的一个框架,两者不整合的话则Spring和SpringMVC来创建同一个IOC容器,而我们当前的IOC容器是需要加载咱们Spring的配置文件来进行创建的,现在需要将所有内容配置到同一个配置文件中,也可以配置到多个里面,不整合主要就是Spring和SpringMVC来创建同一个IOC容器。而整合即为各自来管理各自的组件,比如SpringMVC来创建他自己的IOC容器,来管理他管理的组件,Spring就把其余的一些组件来进行管理,来创建一个新的IOC容器。
SpringMVC的IOC容器是在DispatcherServlet初始化的过程中来创建的
Spring的IOC容器 SpringMVC管理的组件是控制层组件,而其他的组件则要交给Spring来管理,比如业务层组件(service层),而当前SpringMVC的控制层依赖于当前的service层组件,因为在我们的控制层里面需要创建一个service的成员变量,然后进行一个自动装配(获取IOC容器的时候完成的,即当前的controller里面来自动装配service这个组件就是在当前springmvc的IOC容器获取的时候来执行的,而SpringMVC的IOC容器是在Dispatcherservlet进行初始化的时候进行创建的,而Dispatcherservlet的初始化是在服务器进行启动的时候完成的),然后在controller里面使用service对象,所以需要先创建Spring的IOC容器,后创建Springmvc的IOC容器。
服务器的三大组件:servlet(最后执行)、filter(第二执行)、listener(最早被执行)
二、ContextLoaderListener
监听器有三种:(1)ServletContextListener,可监听ServletContext的状态。
(2)HttpSessionListener,监听HttpSession的状态
(3)HttpSessionAttributeListener,监听HttpSession的状态
监听器里面初始化的方法只执行一次。
Spring提供了监听器ContextLoaderListener,实现ServletContextListener接口,可监听ServletContext的状态,里面有两个抽象方法,第一个抽象方法叫ServletContext初始化(该方法中帮助我们完成了一个创建Spring的IOC容器的功能),第二个叫ServletContext销毁。
在web服务器的启动,读取Spring的配置文件,创建Spring的IOC容器,web应用中必须在web.xml中配置。
我们找到他的初始化方法:
此方法里面进行创建了Spring的IOC容器,
创建完毕之后,把他放在最大的域对象里面进行共享:
我们可以在web.xml里面配置监听器:
<listener> <!-- 配置Spring的监听器,在服务器启动时加载spring的配置文件 Spring配置文件默认位置和名称:/WEB-INF/applicationContext.xml 可通过上下文参数自定义Spring配置文件的位置和名称 --> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!--自定义Spring配置文件的位置和名称--> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring.xml</param-value> </context-param>
我们来进行创建一个新的工程,
我们将pom.xml进行创建:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.rgf</groupId> <artifactId>spring_MVC_helloWorld</artifactId> <version>1.0-SNAPSHOT</version> <packaging>war</packaging> <properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <!--SpringMVC--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.3.1</version> </dependency> <!--日志--> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>1.2.3</version> </dependency> <!--ServletAPI--> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <scope>provided</scope> </dependency> <!--Spring和Thymeleaf整合包--> <dependency> <groupId>org.thymeleaf</groupId> <artifactId>thymeleaf-spring5</artifactId> <version>3.0.12.RELEASE</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>tools</artifactId> <version>1.8.0</version> <scope>compile</scope> </dependency> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3.1</version> </dependency> </dependencies> </project>
之后我们进行添加web模块:
之后创建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>SpringMVC</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>SpringMVC</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <listener> <!--在服务器启动时加载Spring的配置文件--> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring.xml</param-value> </context-param> </web-app>
之后进行创建SpringMVC.xml:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!--扫描控制层组件--> <context:component-scan base-package="com.rgf.controller"></context:component-scan> <!--配置Thymeleaf视图解析器--> <bean id="viewResolver" class="org.thymeleaf.spring5.view.ThymeleafViewResolver"> <property name="order" value="1"/> <property name="characterEncoding" value="UTF-8"/> <property name="templateEngine"><!--模板引擎--> <bean class="org.thymeleaf.spring5.SpringTemplateEngine"> <property name="templateResolver"><!--模板解析器--> <bean class="org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver"> <!--/WEB-INF/templates/index.html--> <!--视图前缀+逻辑视图+视图后缀就是我们完整的物理视图,即访问index.html,不用完整路径,即index即可进行访问--> <!--视图前缀--> <property name="prefix" value="/WEB-INF/templates/"/> <!--视图后缀--> <property name="suffix" value=".html"/> <property name="templateMode" value="HTML5"/> <property name="characterEncoding" value="UTF-8" /> </bean> </property> </bean> </property> </bean> <mvc:annotation-driven/> <mvc:view-controller path="/" view-name="index"></mvc:view-controller> </beans>
再进行创建Spring.xml:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"> <!--扫描组件--> <context:component-scan base-package="com.rgf.service.impl"></context:component-scan> </beans>
之后我们进行创建控制层:
package com.rgf.controller; import com.rgf.service.HelloService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; @Controller public class HelloController { @Autowired private HelloService helloService; }
再进行创建service层,首先我们进行创建接口:
package com.rgf.service; public interface HelloService { }
之后我们创建该接口的实现类:
package com.rgf.service.impl; import com.rgf.service.He