The basic construction environment adopted: SpringMVC, Mybatis, MySQL, Tomcat Spring transaction management has broken down the disadvantages of traditional global affairs management and local affairs management, so that a unified transaction management model can be used in any environment. You can write a code once, you can write a code. Then configure different transaction management strategies from your code in different environments. Spring provides two transaction management strategies: one is a statement management strategy, and the other is programming management strategy. Here Since the management strategy uses SpringMVC and Mybatis, it is uniformly labeled to declare the service and Controller
Since the order of loading configuration files at the server starts is web.xml --- root-context.xml (configuration file of Spring) --- Servlet-Context.xml (configuration file of springmvc), due to the root-context.xml configuration In the file, the controller will be scanned first, but at this time, the service has not yet been enhanced by transaction. What will be obtained is the original service (without the handling of transaction, so there is no transaction processing capacity), so we must be on the root-context.xml Do not scan Controller, the configuration is as follows:
<!-Automatically scan the component, here is to remove the Controller below Controler. They are configured in Spring3-Servlet.xml. If it is not removed, it will affect transaction management. -> <Context: Component-SCAN BASE-PACKAGE = "Com.sence"> <Context: Exclude-Filter Type = "Annotation" = "ORG.SpringFrameWork.sterotype.Contr oller " />. < /Context: Component- SCAN> <!-Automatically scan the component, here is to remove the Controller under Controlr. They are configured in Spring3-Servlet.xml. If it is not removed, it will affect transaction management. --> <context:component-scan base-package="com.sence"> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Contr oller" /> </context:component-scan "Scan the Controller in servlet-context.xml while not scanning the Service. The configuration is as follows:
<!-Scan all Controller but do not scan the service-> <Context: component-scan base-package = "com.sence"> <Context: Include-Filter Type = "Annotation" = "OR. g.springframework.stereotype .Controller " /> <context: Exclude-free type =" Annotion "expression =" org.springframework.steeotype.service " /> < /context: component---! controller but not scanning service- -> <context:component-scan base-package="com.sence"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Contro ller" /> <context:exclude-filter type= "Annotation." Expression = "ORG.SpringFramework.sterotype.service" /> < /Context: Component-scan>The following can be configured for the management of the statement, the configuration is as follows:
<!-Transaction Manager, Use DataSourceTransaCTIONAGER-> <Bean ID = "TXMANAGER"> <Property name = "DataSource" Ref = "DataSource" /> < /Bean> <!-Sprin. G Declamed Transaction Management-> < AOP: Config> <AOP: POINTCUT ID = "FooserviceMethods" Exposition = "Execution (*com.sence.*.*.Impl.*.*(..))" <AOP: Advisor Advice-REF = "txadvice "PointCut-Ref =" FooserviceMethods "/> </AOP: Config> <TX: Advice ID =" TXADVICE "Transaction-Manager =" TXMANAGER "> <TX: ATTRIBUTES> <METHOD NAM E = "Find*" Read- Only = "true"/> <tx: method name = "love*" read-only = "true"/> <tx: method name = "*" rollback-for = "customexception"/> </tx: attributes> </tx: Advice> <!-Transaction Manager, Use DataSourceTransaCTIONAGER-> <Bean ID = "TXMANAGER"> <Property name = "DataSource" Ref = "DataSource"/> </> bean> <!- Spring Declarative Transaction Management-> <AOP: Config> <AOP: PointCut ID = "FooserviceMethods" Expression = "Execution (*Com.sence.*.*.*.*(..))" AOP: Advisor advice-ref = "txAdvice" pointcut-ref = "fooserviceMethods"/> </AOP: Config> <tx: advice id = "txadvice" transaction-manager = "txmanager"> <TX: ATTR ibutes> <TX: Method name = "Find*" Read-only = "TRUE"/> <tx: Method name = "Load*" Read-only = "TRUE"/> <tx: Method name = "*" Rollback-FOR = "CustomException"/> </tx: Attributes> </tx: advice>At this point, my configuration was completed, but after my test, when I went to the MYSQL database table in batches, one of the objects were wrong, and the Customexception affairs did not return. This is really a headache, so I continued Find, the steps are as follows: 1. Find whether the declaration of declaration -type transactions is wrong, if the entry point is wrong