博文引用:
SSM框架是由Spring、SpringMVC、MyBatis三个开源框架组成的,在本人当下的理解中主要用于开发web项目。要使用SSM框架首先需要导入相应的包(具体的包请自行检索),接着开始编写配置文件。
编写配置文件的过程稍微有点复杂,但是配置好之后以后就可以重复利用。主要的配置文件有:web.xml(自动生成,在WEB-INF下),applicationContext.xml(spring配置文件,文件名可能不同,以自己的文件名为准),springMVC.xml,Mapper.xml文件(操作数据库的配置文件)。
执行一个使用SSM框架搭建的javaWEB工程时,服务器加载顺序如下:
1.读取web.xml配置文件
<context-param>
<!-- 根据指定的路径区读取spring配置文件。 -->
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
注意: 此时服务器会去加载spring配置文件,暂停本段配置之后的代码执行;
2、加载spring配置文件,applicationContext.xml中的主要配置如下:
(1)、只要导入了相应的包,这一段配置不变
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
">
(2)、自动扫描base-package对应的路径下面的java文件,如果扫描到文件中带有@Service,@Component,@Repository,@Controller等这些注解的类,则把这些类注册为bean
<context:component-scan base-package="com.sun.*"></context:component-scan>
(3)、配置数据源,操作数据库的配置
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/test?serverTimezone=CST" />
<property name="username" value="root" />
<property name="password" value="123456" />
<property name="defaultAutoCommit" value="false" />
</bean>
(4)、sqlSessionFactory配置
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 注入数据库连接池 -->
<property name="dataSource" ref="dataSource"/>
<!-- 配置MyBatis全局配置文件:mybatis-config.XML -->
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<!-- 扫描entity包 使用别名 -->
<property name="typeAliasesPackage" value="com.sun.seckill.entity"/>
<!-- 扫描sql配置文件:mapper需要的xml文件 -->
<property name="mapperLocations" value="classpath:mapper/*.xml"/>
</bean>
(5)、配置扫描dao接口包,动态实现dao接口,并注入到spring容器中
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 注入sqlSessionFactory -->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
<!-- 给出扫描dao接口包 自动生成sqlSessionFactory去执行sql语句-->
<property name="basePackage" value="com.sun.seckill.dao"/>
</bean>
(6)、事务配置,即事务管理器作用是对一系列的数据库操作进行统一的自动的提交和回滚操作
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" ></property>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
(7)、AOP配置,执行日志文件等操作
<aop:config>
<aop:pointcut expression="execution(* com.sun.service.*.*(..))" id="text" />
<aop:advisor advice-ref="xx" pointcut-ref="text" />
</aop:config>
至此spring配置文件加载完成,服务器回到web.xml文件中进行后续的执行。
3.回到web.xml配置文件
继续按顺序从上到下加载。加载监听器;加载过滤器;加载前端控制器(即servlet或者springMVC),前端控制器中的1配置决定了前端控制器是否在容器启动时就加载,若其值大于等于0则在容器启动时加载,小于零或不设置时则不在容器启动时加载。若要在容器(服务器Tomcat)启动时就加载前端控制器,则此时暂停web.xml的加载,先去加载springMVC。
4.加载springMVC配置文件
在web.xml配置文件加载前端控制器时根据其中的相关配置加载springMVC配置。加载控制器(第二步中未加载),自动实例化相关类以便之后直接使用不用new(controller和servse),加载视图解析器,完成springMVC配置文件的加载。
5.回到web.xml文件
继续进行后续加载,若无后续则完成整个项目的加载。