Spring mvc @Transactional annotation configuration steps and @Transactional things are invalid Note

  • spring-context.xml file configuration
<?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:jdbc="http://www.springframework.org/schema/jdbc"
       Xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
       Xmlns:util="http://www.springframework.org/schema/util" xmlns:cache="http://www.springframework.org/schema/cache"
       Xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation="
Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
Http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
Http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.1.xsd
Http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.1.xsd
Http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
Http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd
Http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.1.xsd
       Http://www.springframework.org/schema/cache
       Http://www.springframework.org/schema/cache/spring-cache.xsd"
       Default-lazy-init="true">


    <!-- Automatically register beans with Annotation, do not scan @Controller annotations -->
    <context:component-scan base-package="com.test.project"><!-- base-package If multiple, separate with "," -->
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>


    <!-- Define transaction -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>

    <!-- Configure the Annotation driver, scan the @Transactional annotated class definition transaction -->
    <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>
    
</beans>
  • spring-mvc.xml Configuration
<?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/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
Http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
Http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">

    <!-- Auto-scan uses Annotation to automatically register beans, only scan @Controller -->
    <context:component-scan base-package="com.test.myproject" use-default-filters="false">
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>

</beans>

use-default-filters="false" must be set to false, otherwise the default value of the

use-default-filters property is true, that is, the package is scanned using the default Filter. The default Filter scans the classes marked with @Service, @Controller and @Repository. We want SpringMVC to only control the jump logic of the site, so we only want the SpringMVC configuration to scan the @Controllerce annotation class. You don't want it to scan the classes of the remaining annotation annotations, so set use-default-filters to false and use the context:include-filter subtag to set it to only scan classes with the @Controller annotation annotation. Spring is different. We want Spring to scan only the classes with @Controller annotation annotations and scan other annotation annotation classes. Based on the default Filter, we set context:exclude- Filter tag excludes Controller