说到Spring、SpringMVC和MyBaits整合(后续简称SSM),之前一直只知道简单的使用各种注解,而不了解其中的加载原理,听了下面的课程后才慢慢理解,原来是相互配合加载来完成的。
SSM整合的基础教程视频(哔哩哔哩,免费的)
整合的架构
在SSM构架中,是以Spring为核心,再配合SpringMVC和MyBatis来处理的。
整合的原理
在说原理之前,先说一下,服务端的加载顺序,此处以tomcat为例。
tomcat在启动时,会加载web.xml文件,在加载web.xml文件时,会一起读取很多配置参数,如<context-param>,<listener>,<servlet>等等,有servlet基础的人都知道,servlet的加载有2种,一种是访问时才加载,一种是服务端启动时就加载(只需要设置<load-on-startup>为1即可)。
SpringMVC的加载
SpringMVC框架核心其实就是一个Servlet,只是它是用一个Servlet来管理所有请求,因此,它的配置就是在web.xml中添加SpringMVC的Servlet分发器。如下所示:
<servlet>
<servlet-name>dispatcherServlet</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>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
由于其中设置了该参数:<load-on-startup>1</load-on-startup>,因此该servlet在服务器启动时就会进行加载和初始化,也就可以实现了后续所有请求都由它来处理和分发。
而这里面其中的“springmvc.xml”就是springmvc的配置文件,它的位置存放在resources目录(如下图),在加载DispatcherServlet时,就会开始读取该配置文件,从而初始化springmvc。
【springmvc.xml】文件的存放位置
【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
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
">
<!--配置controller包扫描器-->
<context:component-scan base-package="com.billy">
<context:include-filter type="annotation"
expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!--配置视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
id="internalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!--配置注解驱动-->
<mvc:annotation-driven/>
<!--过滤静态资源-->
<mvc:resources mapping="/js/**" location="/js/"/>
<mvc:resources mapping="/css/**" location="/css/"/>
<mvc:resources mapping="/images/**" location="/images/"/>
</beans>
Spring的加载
首先Spring需要自己的配置文件,一般都命名为:applicationContext.xml,存放位置如图1所示。
注:配置文件一般大家都统一放在resources文件夹中,方便统一管理。
由于Spring是SSM框架的核心,所以需要服务端启动时就进行加载运行,而能在服务端启动时就加载的文件一般可以利用的只有web.xml文件,所以spring加载设置还是放在了web.xml文件中。
大家都知道,在web.xml中,是可以设置<listener>的,可以设置不同的listener来监听服务端的各种事件,其中就可以监听服务端启动和关闭前后的listener——ServletContextListener。
设置过listener的人可以都知道,ServletContextListener是服务端启动和关闭时所调用的,而spring就是利用该listener来实现自己的加载并拥有和服务端一样的生命周期。因此,只需要设置该listener即可实现spring在服务端启动时就加载。
web.xml配置如下:
<!--服务器启动是的所加载的参数,设置spring xml文件的位置-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!--
服务器启动时添加监听ServletContext,实现spring与ServletContext关联,
这样可以拥有与ServletContext相同的生命周期。
在监听服务器启动时,通过上面设置的spring xml文件路径来获取spring的xml配置,然后加载并初始化
-->
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
首先需要配置<context-param>来设置spring的配置文件的位置,在服务端启动时,spring就可以读取该设置,然后加载该配置文件,实现自己的初始化。
【applicationContext.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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
">
<context:component-scan base-package="com.billy">
<!--设置在扫描包时,排除指定注解的类-->
<context:exclude-filter type="annotation"
expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!--Spring整合MyBatis框架,start-->
<!--1、配置连接池,此处使用c3p0-->
<bean class="com.mchange.v2.c3p0.ComboPooledDataSource" id="dataSource">
<property name="driverClass"
value="com.microsoft.sqlserver.jdbc.SQLServerDriver"/>
<property name="jdbcUrl"
value="jdbc:sqlserver://localhost:1433;DatabaseName=Snx2013"/>
<property name="user" value="sa"/>
<property name="password" value="jgl"/>
</bean>
<!--2、配置SqlSessionFactory,该工厂用于创建dao接口的代理对象,需要引用数据源-->
<bean class="org.mybatis.spring.SqlSessionFactoryBean" id="sqlSessionFactory">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--3、配置dao接口对象所在位置,dao映射位置-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"
id="mapperScannerConfigurer">
<property name="basePackage" value="com.billy.dao"/>
</bean>
<!--Spring整合MyBatis框架,end-->
<!--配置spring框架声明式事务管理,start-->
<!--1、配置事务管理器,关联上面配置的连接池-->
<bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
id="dataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--2、配置事务通知-->
<tx:advice transaction-manager="dataSourceTransactionManager" id="txAdvice">
<tx:attributes>
<!--表示find开头的方法,数据为只读-->
<tx:method name="find*" read-only="true"/>
<!--
表示除了find*开头的,其它的方法的配置。
比如:隔离级别等等,此处只是演示,属性仍然为默认值
-->
<tx:method name="*" isolation="DEFAULT"/>
</tx:attributes>
</tx:advice>
<!--
3、配置AOP增强,用于指定哪些类或方法在调用时,spring是否需要自动加上事务管理。
声明式事务管理建立在AOP之上的。其本质是对方法前后进行拦截,
然后在目标方法开始之前创建或者加入一个事务,
在执行完目标方法之后根据执行情况提交或者回滚事务。
-->
<aop:config>
<aop:advisor advice-ref="txAdvice"
pointcut="execution(* com.billy.service.impl.*ServiceImpl.*(..))"/>
</aop:config>
<!--配置spring框架声明式实物管理,end-->
</beans>
在applicationContext.xml文件中,就整合了MyBaits、数据库连接池和事务管理等配置。
到此,Spring、SpringMVC和MyBaits就整合到一起了,它们主要是利用了服务端启动规则来实现自己的加载,从而实现与服务端相同的生命周期。
总结
在没了解之前,一直都不知道SSM到底是怎么整合的,相互之间是怎么关联的,又是什么时候启动的。
通过这些知识终于了解一些spring的皮毛,其实还真的只是表面的一些皮毛而已,spring框架是何其的庞大,这点东西还只是它的冰山一角,它还有很多知识需要去学习和了解。
注:由于技术和理解能力有限,若上面的说明或注释有不合理和错误的地方,欢迎各位大神指正教导,在下感激不尽。