will not be managed by Spring 和was not registered for synchronization because synchronization is n

   在SpringMVC框架,事物管理杜絕出現 will not be managed by Spring  和was not registered for synchronization because synchronization is not active

否則Mybatis的事物管理中對事物的控制會出問題。

    web.xml的加載順序爲:注意web.xml的執行順序  context-param -> listener -> filter -> servlet
   如果在SpringMCV中使用註解方式,建議清晰定義註解的作用,避免各個層級之間應爲初級程序員造成的註解混亂問題;

應該保證各個配置文件功能單一,避免架構混亂

直接上代碼:、

 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_3_0.xsd"
    id="WebApp_ID" version="3.0">
    <display-name>SingLoginA</display-name>


    <!-- 編碼過濾器 -->
    <filter>
        <filter-name>encodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <async-supported>true</async-supported>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>encodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>


    <!-- 防止Spring內存溢出監聽器 -->
    <listener>
        <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
    </listener>
    <!-- 使Spring支持request與session的scope,如:<bean id="loginAction" scope="request"/> -->
    <listener>
        <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
    </listener>
    <!-- 日誌 -->
    <context-param>
        <param-name>log4jConfigLocation</param-name>
        <param-value>WEB-INF/classes/log4j.properties</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
    </listener>
    <!-- Spring MVC servlet -->
    <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:spring-mvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
        <async-supported>true</async-supported>
    </servlet>
    <servlet-mapping>
        <servlet-name>SpringMVC</servlet-name>
        <!-- 此處可以可以配置成*.do,對應struts的後綴習慣 -->
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    <welcome-file-list>
        <welcome-file>/index.jsp</welcome-file>
    </welcome-file-list>
    <!-- 配置SESSION超時,單位是分鐘 -->
    <session-config>
        <session-timeout>15</session-timeout>
    </session-config>
    <!-- Spring監聽器 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
                 classpath*:spring-mybatis.xml,
                 classpath*:servlet-init.xml,
                <!-- classpath*:spring-mybatis.xml,, -->
        </param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <!-- 阿里巴巴數據庫連接池 -->
    <filter>
        <filter-name>druidWebStatFilter</filter-name>
        <filter-class>com.alibaba.druid.support.http.WebStatFilter</filter-class>
        <init-param>
            <param-name>exclusions</param-name>
            <param-value>*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>druidWebStatFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <servlet>
        <servlet-name>druidStatView</servlet-name>
        <servlet-class>com.alibaba.druid.support.http.StatViewServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>druidStatView</servlet-name>
        <url-pattern>/druid/*</url-pattern>
    </servlet-mapping>
    <!-- 系統啓動時候加載的服務 -->
    <servlet>
        <description></description>
        <servlet-name>borrowServlet</servlet-name>
        <servlet-class>com.sing.common.util.sevlet.InitService</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>test</param-value>
        </init-param>
        <load-on-startup>10</load-on-startup>
    </servlet>
</web-app>


##########################

Spring-init.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:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:util="http://www.springframework.org/schema/util" 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-3.0.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
  <!--
  properties文件引入方式
  <context:property-placeholder location="classpath*:dataSourceConfig.properties" />
   或則
   -->
  <bean id="propertyConfigurer"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath:/dataSourceConfig.properties</value>
            </list>
        </property>
    </bean>
   
    <!-- 注意web.xml的執行順序  context-param -> listener -> filter -> servlet  
      避免在SpringMVC中啓用 service註解,否則Spring管理不了數據庫事物
         <context:component-scan base-package="com.sing.service" />
         如下爲線上指出註解用於何處切com.sing.service 允許用Service
         -->  

<context:component-scan base-package="com.sing.service" >
   <context:include-filter type="annotation" expression="org.springframework.stereotype.Service"/>
   <!-- 攔截所有的在service層使用 @service註解的類  主要是爲了層級更加清晰以及避免註解亂用-->
   <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>


    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
        init-method="init" destroy-method="close">
        <property name="url" value="${url}" />
        <property name="username" value="${username}" />
        <property name="password" value="${password}" />
        <property name="initialSize" value="${initialSize}" />
        <property name="minIdle" value="${minIdle}" />
        <property name="maxActive" value="${maxActive}" />
        <property name="maxWait" value="${maxWait}" />
        <!-- timeBetweenEvictionRunsMillis毫秒秒檢查一次連接池中空閒的連接,把空閒時間超過minEvictableIdleTimeMillis毫秒的連接斷開,直到連接池中的連接數到minIdle爲止  -->
        <property name="timeBetweenEvictionRunsMillis" value="${BEtime}" />
        <property name="minEvictableIdleTimeMillis" value="${minEtime}" />
        <property name="validationQuery" value="SELECT 'x'" />
        <property name="testWhileIdle" value="true" />
        <property name="testOnBorrow" value="false" />
        <property name="testOnReturn" value="false" />
        <property name="poolPreparedStatements" value="${poolPreparedStatements}" />
        <property name="maxPoolPreparedStatementPerConnectionSize" value="${maxPoolPreparedStatementPerConnectionSize}" />
        <property name="removeAbandoned" value="${removeAbandoned}" /> <!-- 打開removeAbandoned功能 -->
        <property name="removeAbandonedTimeout" value="${removeAbandonedTimeout}" /> <!-- 1800秒,也就是30分鐘 -->
        <property name="logAbandoned" value="${logAbandoned}" /> <!-- 關閉abanded連接時輸出錯誤日誌 -->
        <property name="filters" value="mergeStat" />
        <property name="connectionProperties" value="druid.stat.slowSqlMillis=5000" />
    </bean>
    <!--做雙數據源-->
    <bean id="readDataSource" class="com.alibaba.druid.pool.DruidDataSource"
        init-method="init" destroy-method="close">
        <property name="url" value="${readUrl}" />
        <property name="username" value="${readUsername}" />
        <property name="password" value="${readPassword}" />
        <property name="initialSize" value="${initialSize}" />
        <property name="minIdle" value="${minIdle}" />
        <property name="maxActive" value="${maxActive}" />
        <property name="maxWait" value="${maxWait}" />
        <!-- timeBetweenEvictionRunsMillis毫秒秒檢查一次連接池中空閒的連接,把空閒時間超過minEvictableIdleTimeMillis毫秒的連接斷開,直到連接池中的連接數到minIdle爲止  -->
        <property name="timeBetweenEvictionRunsMillis" value="${BEtime}" />
        <property name="minEvictableIdleTimeMillis" value="${minEtime}" />
        <property name="validationQuery" value="SELECT 'x'" />
        <property name="testWhileIdle" value="true" />
        <property name="testOnBorrow" value="false" />
        <property name="testOnReturn" value="false" />
        <property name="poolPreparedStatements" value="${poolPreparedStatements}" />
        <property name="maxPoolPreparedStatementPerConnectionSize" value="${maxPoolPreparedStatementPerConnectionSize}" />
        <property name="removeAbandoned" value="${removeAbandoned}" /> <!-- 打開removeAbandoned功能 -->
        <property name="removeAbandonedTimeout" value="${removeAbandonedTimeout}" /> <!-- 1800秒,也就是30分鐘 -->
        <property name="logAbandoned" value="${logAbandoned}" /> <!-- 關閉abanded連接時輸出錯誤日誌 -->
        <property name="filters" value="mergeStat" />
        <!-- <property name="filters" value="stat" /> -->
        <property name="connectionProperties" value="druid.stat.slowSqlMillis=5000" />
        <!-- <property name="useGloalDataSourceStat" value="true" /> -->
    </bean>

 <!--
   <bean id="multipleDataSource" class="org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource">
        <property name="defaultTargetDataSource" ref="dataSource"/>
        <property name="targetDataSources">
            <map>
                <entry key="dataSource" value-ref="dataSource"/>
                <entry key="readDataSource" value-ref="readDataSource"/>
            </map>
        </property>
    </bean>
 -->
    
    <bean id="druid-stat-interceptor"
        class="com.alibaba.druid.support.spring.stat.DruidStatInterceptor" />
    <bean id="druid-stat-pointcut" class="org.springframework.aop.support.JdkRegexpMethodPointcut"
        scope="prototype">
        <property name="patterns">
            <list>
                <value>com.cn.sing.service.*</value>
            </list>
        </property>
    </bean>
    <aop:config>
        <aop:advisor advice-ref="druid-stat-interceptor"
            pointcut-ref="druid-stat-pointcut" />
    </aop:config>
    <bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>
   <!-- 避免 will not be managed by Spring
   和was not registered for synchronization because synchronization is not active
       一定要是事物被Spring管理,否則事物不起作用
   -->
    <!--註解式事物會在上下文中尋找使用@Transactional的註解的類和方法,啓用事物
      申明式事物會在攔截execution(* com.sing.service..*.*(..))的切面中尋找對應的
      其中:第一個*代表所有的返回值類型
                       第二個*代表所有的類
                       第三個*代表類所有方法 最後一個..代表所有的參數。
      攔截方法使用事物,其中事物的加載方式根據 事物在配置文件的位置有關係,
      例如,聲明式事物在註解式事物之前,則會以聲明式事物的攔截事物爲主,其攔截不到的時候
      纔會自動去匹配註解式事物,
      如果想使用兩種事物方式兼容,建議註解式事物在聲明式事物之前
     -->
       <!-- 註解式事物 -->
     <tx:annotation-driven transaction-manager="transactionManager" />
     <!-- 聲明式事物-->
    <aop:config>
        <aop:advisor pointcut="execution(* com.sing.service..*.*(..))" advice-ref="txAdvice" />
    </aop:config>
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="load*" read-only="true" />
            <tx:method name="select*"  />
            <tx:method name="*" propagation="REQUIRED" rollback-for="Exception" />
        </tx:attributes>
    </tx:advice>

     
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="mapperLocations" value="classpath*:com/sing/dao/mapper/*.xml" />
    </bean>   

    

    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.sing.dao" />
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
    </bean>

</beans>

Spring-mybatis.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:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:util="http://www.springframework.org/schema/util" 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-3.0.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
  <!--
  properties文件引入方式
  <context:property-placeholder location="classpath*:dataSourceConfig.properties" />
   或則
   -->
  <bean id="propertyConfigurer"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath:/dataSourceConfig.properties</value>
            </list>
        </property>
    </bean>
   
    <!-- 注意web.xml的執行順序  context-param -> listener -> filter -> servlet  
      避免在SpringMVC中啓用 service註解,否則Spring管理不了數據庫事物
         <context:component-scan base-package="com.sing.service" />
         如下爲線上指出註解用於何處切com.sing.service 允許用Service
         -->  
<context:component-scan base-package="com.sing.service" >
   <context:include-filter type="annotation" expression="org.springframework.stereotype.Service"/>
   <!-- 攔截所有的在service層使用 @service註解的類  主要是爲了層級更加清晰以及避免註解亂用-->
   <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>


    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
        init-method="init" destroy-method="close">
        <property name="url" value="${url}" />
        <property name="username" value="${username}" />
        <property name="password" value="${password}" />
        <property name="initialSize" value="${initialSize}" />
        <property name="minIdle" value="${minIdle}" />
        <property name="maxActive" value="${maxActive}" />
        <property name="maxWait" value="${maxWait}" />
        <!-- timeBetweenEvictionRunsMillis毫秒秒檢查一次連接池中空閒的連接,把空閒時間超過minEvictableIdleTimeMillis毫秒的連接斷開,直到連接池中的連接數到minIdle爲止  -->
        <property name="timeBetweenEvictionRunsMillis" value="${BEtime}" />
        <property name="minEvictableIdleTimeMillis" value="${minEtime}" />
        <property name="validationQuery" value="SELECT 'x'" />
        <property name="testWhileIdle" value="true" />
        <property name="testOnBorrow" value="false" />
        <property name="testOnReturn" value="false" />
        <property name="poolPreparedStatements" value="${poolPreparedStatements}" />
        <property name="maxPoolPreparedStatementPerConnectionSize" value="${maxPoolPreparedStatementPerConnectionSize}" />
        <property name="removeAbandoned" value="${removeAbandoned}" /> <!-- 打開removeAbandoned功能 -->
        <property name="removeAbandonedTimeout" value="${removeAbandonedTimeout}" /> <!-- 1800秒,也就是30分鐘 -->
        <property name="logAbandoned" value="${logAbandoned}" /> <!-- 關閉abanded連接時輸出錯誤日誌 -->
        <property name="filters" value="mergeStat" />
        <property name="connectionProperties" value="druid.stat.slowSqlMillis=5000" />
    </bean>
    
    <bean id="readDataSource" class="com.alibaba.druid.pool.DruidDataSource"
        init-method="init" destroy-method="close">
        <property name="url" value="${readUrl}" />
        <property name="username" value="${readUsername}" />
        <property name="password" value="${readPassword}" />
        <property name="initialSize" value="${initialSize}" />
        <property name="minIdle" value="${minIdle}" />
        <property name="maxActive" value="${maxActive}" />
        <property name="maxWait" value="${maxWait}" />
        <!-- timeBetweenEvictionRunsMillis毫秒秒檢查一次連接池中空閒的連接,把空閒時間超過minEvictableIdleTimeMillis毫秒的連接斷開,直到連接池中的連接數到minIdle爲止  -->
        <property name="timeBetweenEvictionRunsMillis" value="${BEtime}" />
        <property name="minEvictableIdleTimeMillis" value="${minEtime}" />
        <property name="validationQuery" value="SELECT 'x'" />
        <property name="testWhileIdle" value="true" />
        <property name="testOnBorrow" value="false" />
        <property name="testOnReturn" value="false" />
        <property name="poolPreparedStatements" value="${poolPreparedStatements}" />
        <property name="maxPoolPreparedStatementPerConnectionSize" value="${maxPoolPreparedStatementPerConnectionSize}" />
        <property name="removeAbandoned" value="${removeAbandoned}" /> <!-- 打開removeAbandoned功能 -->
        <property name="removeAbandonedTimeout" value="${removeAbandonedTimeout}" /> <!-- 1800秒,也就是30分鐘 -->
        <property name="logAbandoned" value="${logAbandoned}" /> <!-- 關閉abanded連接時輸出錯誤日誌 -->
        <property name="filters" value="mergeStat" />
        <!-- <property name="filters" value="stat" /> -->
        <property name="connectionProperties" value="druid.stat.slowSqlMillis=5000" />
        <!-- <property name="useGloalDataSourceStat" value="true" /> -->
    </bean>

 <!--
   <bean id="multipleDataSource" class="org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource">
        <property name="defaultTargetDataSource" ref="dataSource"/>
        <property name="targetDataSources">
            <map>
                <entry key="dataSource" value-ref="dataSource"/>
                <entry key="readDataSource" value-ref="readDataSource"/>
            </map>
        </property>
    </bean>
 -->
    
    <bean id="druid-stat-interceptor"
        class="com.alibaba.druid.support.spring.stat.DruidStatInterceptor" />
    <bean id="druid-stat-pointcut" class="org.springframework.aop.support.JdkRegexpMethodPointcut"
        scope="prototype">
        <property name="patterns">
            <list>
                <value>com.cn.sing.service.*</value>
            </list>
        </property>
    </bean>
    <aop:config>
        <aop:advisor advice-ref="druid-stat-interceptor"
            pointcut-ref="druid-stat-pointcut" />
    </aop:config>
    <bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>
   <!-- 避免 will not be managed by Spring
   和was not registered for synchronization because synchronization is not active
       一定要是事物被Spring管理,否則事物不起作用
   -->
    <!--註解式事物會在上下文中尋找使用@Transactional的註解的類和方法,啓用事物
      申明式事物會在攔截execution(* com.sing.service..*.*(..))的切面中尋找對應的
      其中:第一個*代表所有的返回值類型
                       第二個*代表所有的類
                       第三個*代表類所有方法 最後一個..代表所有的參數。
      攔截方法使用事物,其中事物的加載方式根據 事物在配置文件的位置有關係,
      例如,聲明式事物在註解式事物之前,則會以聲明式事物的攔截事物爲主,其攔截不到的時候
      纔會自動去匹配註解式事物,
      如果想使用兩種事物方式兼容,建議註解式事物在聲明式事物之前
     -->
       <!-- 註解式事物 -->
     <tx:annotation-driven transaction-manager="transactionManager" />
     <!-- 聲明式事物-->
    <aop:config>
        <aop:advisor pointcut="execution(* com.sing.service..*.*(..))" advice-ref="txAdvice" />
    </aop:config>
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="load*" read-only="true" />
            <tx:method name="select*"  />
            <tx:method name="*" propagation="REQUIRED" rollback-for="Exception" />
        </tx:attributes>
    </tx:advice>

     
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="mapperLocations" value="classpath*:com/sing/dao/mapper/*.xml" />
    </bean>   

    

    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.sing.dao" />
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
    </bean>

</beans>


Spring.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:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans  
                        http://www.springframework.org/schema/beans/spring-beans-3.1.xsd  
                        http://www.springframework.org/schema/context  
                        http://www.springframework.org/schema/context/spring-context-3.1.xsd  
                        http://www.springframework.org/schema/mvc  
                        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
                        http://www.springframework.org/schema/tx
                        http://www.springframework.org/schema/tx/spring-tx.xsd">
    
    <!-- 自動掃描該包,使SpringMVC認爲包下用了@controller註解的類是控制器  
    <context:component-scan base-package="com.sing.controller" />
     -->
    <context:component-scan base-package="com.sing.controller" >
         <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
         <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service"/>
  </context:component-scan>
    
    <!--  
    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                 <value>classpath*:*.properties</value>
            </list>
        </property>
        <property name="ignoreUnresolvablePlaceholders" value="true" />
    </bean>
    -->
    <!--避免IE執行AJAX時,返回JSON出現下載文件 -->
    <bean id="mappingJacksonHttpMessageConverter"
        class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
        <property name="supportedMediaTypes">
            <list>
                <value>text/html;charset=UTF-8</value>
            </list>
        </property>
    </bean>
    <!-- 啓動SpringMVC的註解功能,完成請求和註解POJO的映射 -->
    <bean
        class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
        <property name="messageConverters">
            <list>
                <ref bean="mappingJacksonHttpMessageConverter" />    <!-- JSON轉換器 -->
            </list>
        </property>
    </bean>
    
    <!-- MVC註解驅動 -->
    <mvc:annotation-driven />
    <!-- 指定靜態資源目錄 -->
    <mvc:default-servlet-handler />
    <mvc:resources mapping="/baseJs/**" location="/WEB-INF/page/*.js"/>
    
    <!--  自定義攔截器:繼承HandlerInterceptor實現攔截 -->
    <mvc:interceptors>
        <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" />
        <mvc:interceptor>
            <mvc:mapping path="/**" />
            <bean class="com.sing.common.util.interceptor.SessionAllInterceptor">
                <property name="excludeUrls">
                    <list>
                    <value>/haocai/assets/subregister</value>
                    </list>
                </property>
            </bean>
        </mvc:interceptor>
    <!--
        <mvc:interceptor>
            <mvc:mapping path="/**" />
            <bean class="com.sing.common.util.interceptor.SessionAllInterceptor">
            </bean>
        </mvc:interceptor> -->
    </mvc:interceptors>
    <!-- 自定義異常攔截器 -->
    <bean id="exceptionResolver" class="com.sing.common.util.interceptor.ExceptionInterceptor"/>
    <!-- 定義跳轉的文件的前後綴 ,視圖模式配置-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!-- 這裏的配置我的理解是自動給後面action的方法return的字符串加上前綴和後綴,變成一個 可用的url地址 -->
        <property name="prefix" value="/" />
        <property name="suffix" value=".jsp" />
    </bean>
    
    <!-- 配置文件上傳,如果沒有使用文件上傳可以不用配置,當然如果不配,那麼配置文件中也不必引入上傳組件包 -->
    <bean id="multipartResolver"  
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver">  
        <!-- 默認編碼 -->
        <property name="defaultEncoding" value="utf-8" />  
        <!-- 文件大小最大值 -->
        <property name="maxUploadSize" value="10485760000" />  
        <!-- 內存中的最大值 -->
        <property name="maxInMemorySize" value="40960" />  
    </bean>
</beans>






發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章