Spring and MVC integration

Spring and SpringMVC integration

Purpose: The Spring MVC
configuration file is clearly divided to configure the website forwarding logic and website functions (view parser, file upload parser, support for ajax...)
Spring configuration file to configure business-related (transaction control, data source) …)

1. When there are multiple xml files, if you want to work at the same time. (Merge configuration files)

(This method is equivalent to merging, which is equivalent to only one ioc container)

For example: you can add the following code to the springmvc.xml file to make the spring.xml file configuration effective.

(SpringMVC and Spring are divided into containers)

When configuring the front controller in the web.xml file, add multiple context-param values

 //基本配置
 <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. Configuration of SpringMVC and Spring in package scanning

The SpringMVC.xml file only scans the 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>

The Spring.xml file scans out other than 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>

The final web.xml file:

<?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>

Guess you like

Origin blog.csdn.net/weixin_42272869/article/details/112145131