Spring与SpringMVC整合
目的:分工明确
SpringMVC的配置文件就来配置和网站转发逻辑以及网站功能相关的(视图解析器,文件上传解析器,支持ajax…)
Spring的配置文件来配置和业务有关的(事务控制、数据源 …)
1、当有多个xml文件的时候,若想同时起作用。(合并配置文件)
(这种方式相当于进行了合并,等于只要一个ioc容器)
例如:在springmvc.xml文件中可以加上以下代码,就可以让spring.xml文件配置生效。
(springMVC和Spring分容器)
在web.xml文件配置前端控制器的时候,加上多个 context-param 的value
//基本配置
<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*:/applicationContext-mvc.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>
//多个xml文件新增
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath*:/applicationContext.xml
classpath*:/applicationContext-shiro.xml
</param-value>
</context-param>
2、SpringMVC与Spring在包扫描的配置
SpringMVC.xml文件只扫描Controller
<?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/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
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-4.0.xsd">
<!-- SpringMVC只扫描控制器 ,禁用默认规则-->
<context:component-scan base-package="com.example" use-default-filters="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!-- 扫静态资源 -->
<mvc:default-servlet-handler/>
<!-- 扫动态资源 -->
<mvc:annotation-driven></mvc:annotation-driven>
<!-<mvc:resources location="/resources/" mapping="/resources/**"/> -->
<!-- 视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<!-- 文件上传解析器 -->
<!-- id必须是multipartResolver -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="utf-8"></property>
<!-- 最大上传20M -->
<property name="maxUploadSize" value="#{1024*1024*20}"></property>
</bean>
</beans>
Spring.xml文件扫描出了Controller以外的
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mybatis-spring="http://mybatis.org/schema/mybatis-spring"
xsi:schemaLocation="http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring-1.2.xsd
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-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
<!-- Spring不扫描Controller扫描其他所有.不需要禁用默认规则 -->
<context:component-scan base-package="com.example">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!-- 1、导入外部配置文件 -->
<!-- <context:property-placeholder location="classpath:datasource.properties"/> -->
<!-- 2、配数据源 -->
<bean id= "dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!-- <property name="user" value="${username}"></property>
<property name="password" value="${password}"></property>
<property name="driverClass" value="${driver}"></property>
<property name="jdbcUrl" value="${jdbcurl}"></property>
<property name="maxPoolSize" value="${maxPoolSize}"></property>
<property name="minPoolSize" value="${minPoolSize}"></property> -->
<property name="user" value="root"></property>
<property name="password" value="123456"></property>
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/mybatis"></property>
<property name="maxPoolSize" value="20"></property>
<property name="minPoolSize" value="5"></property>
</bean>
<!-- 3、配置jdbcTemplate操作数据库,这里交给mybatis配-->
<!-- 配置mybatis操作数据库 -->
<bean class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 可以根据配置文件得到sqlSessionFactory -->
<!--configLocation:指定mybatis配置文件位置 -->
<property name="configLocation" value="classpath:mybatis/mybatis_config.xml"></property>
<!--dataSource:指定mybatis配置文件用哪个dataSource -->
<property name="dataSource" ref="dataSource"></property>
<!-- mapperLocations:指定xml文件映射位置,mapper包下的所有xml-->
<property name="mapperLocations" value="classpath:mapper/*.xml"></property>
</bean>
<!-- 我们要把每一个接口的实现加入到ioc容器中 -->
<!-- <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
指定dao接口所在的包
这样的话,对于dao的对象才能用@Autowired自动注入
<property name="basePackage" value="com.example.dao"></property>
</bean> -->
<mybatis-spring:scan base-package="com.example.dao"/>
<!-- 4、配置事务控制。配置事务管理器,让其控制数据源里的连接的关闭与提交 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 是哪个数据源 -->
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 5、基于xml配置。哪些方法切入宿务写切入点表达式 -->
<aop:config>
<!-- 配置切入点表达式 -->
<!-- com.example.service:包里的任意类的任意方法的任意参数,任意返回值 -->
<!-- id为这个切面的唯一标识 -->
<aop:pointcut expression="execution(* com.example.service.*.*(..))" id="txPoint"/>
<!-- advice-ref:引用哪个事务增强 -->
<aop:advisor advice-ref="myAdvice" pointcut-ref="txPoint"/>
</aop:config>
<!-- 6、配置事务增强、事务属性、事务建议
transaction-manager="transactionManager"
:指定是哪个事务管理器
-->
<tx:advice id="myAdvice" transaction-manager="transactionManager">
<!-- 配置事务属性 -->
<tx:attributes>
<!-- 所有方法都是事务方法 -->
<tx:method name="*" rollback-for="java.lang.Exception"/>
</tx:attributes>
</tx:advice>
</beans>
最终的web.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>Restaurant</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- 配置配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 增加前端控制器 -->
<servlet>
<servlet-name>springDispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/applicationContext-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springDispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 配置过滤器 -->
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--支持rest风格的过滤器 -->
<filter>
<filter-name>HiddenHttpMethodFilter</filter-name>
<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>HiddenHttpMethodFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>