卤蛋的SSM整合完整流程讲解
SpringMVC 是 Spring 家族中的一个框架,可以选择整合,也可以选择不整合,不整合就是指 Spring 和 SpringMVC 创建了同一个 IOC 容器,整合是指 Spring 和 SpringMVC 各自创建自己的 IOC 容器,管理各自的组件。(建议整合)
SpringMVC 是管理表示层(Web)的组件,其他组件是交给 Spring 去管理,表示层依赖于Service组件,这里需要进行自动装配对该组件进行注入,即需要从IOC容器中获取,上面解释了SpringMVC这个子容器可以访问Spring这个父容器的Bean。
而对Service组件进行装配是在表示层的,应该从 SpringMVC 容器中获取,那么Spring 创建 IOC 容器要先于SpringMVC 的创建时间,才能完成自动装配。
而小编在学Spring的时候,不管是使用注解式注入,还是使用配置式注入,都得通过一行代码去将对象交给 IOC 容器管理(针对singleton范围的)。也就是说Spring创建 IOC 是有代码的。
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(??);
或
ApplicationContext applicationContext = new AnnotationConfigApplicationContext(??);
Tomcat服务器提供了三大组件:监听器,过滤器,Servlet。这三大组件的执行顺序为:监听器,过滤器,Servlet。
SpringMVC 中 IOC 容器的创建是在其核心组件DispatcherServlet初始化的时候创建的,根据自下往上的排除法,可以猜测 Spring 要先创建IOC容器的话,很可能就发生在过滤器或者监听器这俩大组件中(因为是Spring提供的整合,所以也排除在DispatcherServlet生命周期中创建)。
过滤器中有俩次过滤,一次是对请求的过滤,执行doFilter方法之前,一次是响应的过滤,执行doFilter方法之后。如果把对Spring IOC容器创建的代码放在过滤器中,缺点很多:
所以可以考虑使用监听器进行实现。
监听器的种类挺多,但根据 Tomcat 运行Servlet的执行顺序,符合要求的只有 ServletContextListener 一个。
为什么这么说?ServletContextListener,顾名思义是去监听ServletContext对象的,它作为WebApp中的上下文对象,其中的初始化参数可以被这整个应用去使用。之所以说它符合要求,首先是一个webapp就一个ServletContext对象,还有就是和它的以下运行流程有关:
contextInitialized
方法,下面是ServletContextListener接口中的方法(监听器可以通过其事件参数,通过event.getServletContext()
获取ServletContext对象,然后获取其初始化参数,然后进行操作)public interface ServletContextListener extends EventListener {void contextInitialized(ServletContextEvent var1);void contextDestroyed(ServletContextEvent var1);
}
下面再看看 ContextLoaderListener
监听器的实现,一切都变得清晰起来了。
public class ContextLoaderListener extends ContextLoader implements ServletContextListener {public ContextLoaderListener() {}public ContextLoaderListener(WebApplicationContext context) {super(context);}public void contextInitialized(ServletContextEvent event) {this.initWebApplicationContext(event.getServletContext());}public void contextDestroyed(ServletContextEvent event) {this.closeWebApplicationContext(event.getServletContext());ContextCleanupListener.cleanupAttributes(event.getServletContext());}
}
当 Tomcat 去解析 web.xml ,启动项目然后创建 ServletContext 对象进行初始化后,就会去执行这个 initWebApplicationContext 方法,见方法名知其意,这就是先去创建 Spring IOC 容器的实现。
随后为什么要在web.xml 中配置以下内容也很清晰了:
org.springframework.web.context.ContextLoaderListener contextConfigLocation classpath:spring-service.xml
1)建库建表(这个由于是整合练习,可以按照自己的需求进行建库建表)
2)新建maven项目,选择webapp模块
3)修改目录,新建目录,如下所示:
4)修改 pom.xml文件(添加以下依赖,下面只是提供了整合依赖,如果需要其他具体实现,比如aspects,jdbc事务,jsp依赖,分页操作…都得另外配置依赖)
junit junit 4.13 test org.springframework spring-test 5.2.5.RELEASE org.springframework spring-context ${spring.version} org.springframework spring-webmvc ${spring.version} org.mybatis mybatis 3.5.11 com.alibaba druid 1.2.16 mysql mysql-connector-java 8.0.31
、org.apache.tomcat tomcat-servlet-api 10.1.0 org.mybatis mybatis-spring 2.0.6 com.fasterxml.jackson.core jackson-databind 2.14.2
5)添加jdbc.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssmuser?useUnicode=true&characterEncoding=utf8
jdbc.username=root
jdbc.password=123456
6)添加mybatis-config.xml文件,其他一些mybatis主要配置可以在spring配置SqlSessionFactoryBean时进行配置。
7)添加spring-mapper.xml文件
8)添加spring-service.xml文件,因为没有使用事务,这个不添加也没关系,但是这个包扫描就得添加到上面去。
9)添加springmvc.xml文件
10)在web.xml文件中注册springmvc框架(添加DispatcherServlet),注册spring框架(全局配置,监听器)
dispatcherServlet org.springframework.web.servlet.DispatcherServlet contextConfigLocation classpath:springmvc.xml dispatcherServlet / org.springframework.web.context.ContextLoaderListener contextConfigLocation classpath:spring-service.xml
11)整合配置结束,开始写Java程序,由于所建的数据库,所建的表不一致,下面我就直接给出小编所写类,类中所用注解,测试。
以上测试可以证明sm整合成功。
以上测试说明ss整合成功,即ssm整合成功。