1、整合SSM
a、导入之前整合的Spring+MyBatis
b、配置监听器启动Spring的IoC容器(或者直接在springmvc.xml引入spring.xml)
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:beans.xml</param-value> //spring配置beans.xml
</context-param>
注意:MyBatis没有Spring事务管理的问题:(直接在springmvc.xml引入spring.xml不会有事务问题)
第一种情况:
两个容器产生两套控制器和业务类,请求调用的是springmvc容器的而事务是控制在spring容器上!
解决:MVC的springmvc.xml配置文件中只扫描包含controller的注解,Spring的beans.xml文件中扫描包时,排除controller的注解,如下所示:
SpringMVC的xml配置:
<context:component-scan base-package="com.cssl.controller"/>
Spring的xml配置:(不扫描带有@Controller注解的类,因为这些类在springmvc.xml中扫描)
<context:component-scan base-package="com.cssl">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
只用一个配置文件:(相当直接在springmvc.xml引入spring.xml)
第二种情况:MySQL数据没用InnoDB引擎,修改数据库引擎即可
2、Web获取当前的SpringIoC容器
方法一:(通过ServletContext加载spring容器,SpringBoot适用)
ApplicationContext ctx=WebApplicationContextUtils.getWebApplicationContext(application);
方法二:(获取当前的spring容器,任何java类中适用)
WebApplicationContext act=ContextLoader.getCurrentWebApplicationContext();
3、防止Spring内存溢出监听器
<listener>
<listener-class>
org.springframework.web.util.IntrospectorCleanupListener
</listener-class>
</listener>