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

2023-12-01 23:18

本文主要是介绍will not be managed by Spring 和was not registered for synchronization because synchronization is not,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

原文地址:http://blog.csdn.net/xufan007/article/details/52025201

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

这篇关于will not be managed by Spring 和was not registered for synchronization because synchronization is not的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/443069

相关文章

Java实现优雅日期处理的方案详解

《Java实现优雅日期处理的方案详解》在我们的日常工作中,需要经常处理各种格式,各种类似的的日期或者时间,下面我们就来看看如何使用java处理这样的日期问题吧,感兴趣的小伙伴可以跟随小编一起学习一下... 目录前言一、日期的坑1.1 日期格式化陷阱1.2 时区转换二、优雅方案的进阶之路2.1 线程安全重构2

Java中的JSONObject详解

《Java中的JSONObject详解》:本文主要介绍Java中的JSONObject详解,需要的朋友可以参考下... Java中的jsONObject详解一、引言在Java开发中,处理JSON数据是一种常见的需求。JSONObject是处理JSON对象的一个非常有用的类,它提供了一系列的API来操作J

SpringBoot多数据源配置完整指南

《SpringBoot多数据源配置完整指南》在复杂的企业应用中,经常需要连接多个数据库,SpringBoot提供了灵活的多数据源配置方式,以下是详细的实现方案,需要的朋友可以参考下... 目录一、基础多数据源配置1. 添加依赖2. 配置多个数据源3. 配置数据源Bean二、JPA多数据源配置1. 配置主数据

将Java程序打包成EXE文件的实现方式

《将Java程序打包成EXE文件的实现方式》:本文主要介绍将Java程序打包成EXE文件的实现方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录如何将Java程序编程打包成EXE文件1.准备Java程序2.生成JAR包3.选择并安装打包工具4.配置Launch4

SpringBoot内嵌Tomcat临时目录问题及解决

《SpringBoot内嵌Tomcat临时目录问题及解决》:本文主要介绍SpringBoot内嵌Tomcat临时目录问题及解决,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,... 目录SprinjavascriptgBoot内嵌Tomcat临时目录问题1.背景2.方案3.代码中配置t

SpringBoot使用GZIP压缩反回数据问题

《SpringBoot使用GZIP压缩反回数据问题》:本文主要介绍SpringBoot使用GZIP压缩反回数据问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录SpringBoot使用GZIP压缩反回数据1、初识gzip2、gzip是什么,可以干什么?3、Spr

Java程序进程起来了但是不打印日志的原因分析

《Java程序进程起来了但是不打印日志的原因分析》:本文主要介绍Java程序进程起来了但是不打印日志的原因分析,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Java程序进程起来了但是不打印日志的原因1、日志配置问题2、日志文件权限问题3、日志文件路径问题4、程序

Spring 基于XML配置 bean管理 Bean-IOC的方法

《Spring基于XML配置bean管理Bean-IOC的方法》:本文主要介绍Spring基于XML配置bean管理Bean-IOC的方法,本文给大家介绍的非常详细,对大家的学习或工作具有一... 目录一. spring学习的核心内容二. 基于 XML 配置 bean1. 通过类型来获取 bean2. 通过

Spring Boot 集成 Quartz并使用Cron 表达式实现定时任务

《SpringBoot集成Quartz并使用Cron表达式实现定时任务》本篇文章介绍了如何在SpringBoot中集成Quartz进行定时任务调度,并通过Cron表达式控制任务... 目录前言1. 添加 Quartz 依赖2. 创建 Quartz 任务3. 配置 Quartz 任务调度4. 启动 Sprin

springboot上传zip包并解压至服务器nginx目录方式

《springboot上传zip包并解压至服务器nginx目录方式》:本文主要介绍springboot上传zip包并解压至服务器nginx目录方式,具有很好的参考价值,希望对大家有所帮助,如有错误... 目录springboot上传zip包并解压至服务器nginx目录1.首先需要引入zip相关jar包2.然