When using the spring framework, we often use this tx:annotation-driven to configure the transaction control of our own projects. Usually, most of the time we use the default mode (do not write or mode="proxy"), proxy It is a proxy mode. Only external method calls will be intercepted by the proxy. Even if the @Transactional annotation is configured for its own method calls, the transaction will not take effect, nor can it be applied to non-public methods. The aspectj mode is different from the proxy mode, the aspectj mode It can be called by its own method, or it can be applied to non-public methods. Here's how to use the mode="aspectj" mode
method/step
First add <tx:annotation-driven transaction-manager="transactionManager" mode="aspectj"/>mode to aspectj mode in spring's configuration file
Add -javaagent:~aspectjweaver-1.8.1.jar to the jvm parameter
If it is under tomcat, it can also be added to catalina.sh, set JAVA_OPTS
When the -javaagent parameter is added, the aspectj mode can be used normally when the service is started. The method mentioned above is the class loader weaving (Load Time Weaving, LTW), and another method is introduced below, weaving at compile time.
在pom.xml文件中加入aspectj-maven-plugin插件配置<plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>aspectj-maven-plugin</artifactId> <version>1.9</version> <configuration> <showWeaveInfo>true</showWeaveInfo> <aspectLibraries> <aspectLibrary> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> </aspectLibrary> </aspectLibraries> <source>${java.version}</source> <target>${java.version}</target> <complianceLevel>${java.version}</complianceLevel> </configuration> <executions> <execution> <goals> <goal>compile</goal> <goal>test-compile</goal> </goals> </execution> </executions></plugin>
这几个配置要加上<source>${java.version}</source><target>${java.version}</target><complianceLevel>${java.version}</complianceLevel>否则可能会报以下错误[ERROR] Syntax error, annotations are only available if source level is 1.5 or greater E:\work\datai\code_study\study-master\study-spring-transaction\src\main\java\cn\sw\study\web\controller\UserController.java:12@Controller^^^^^^^^^^[ERROR] Syntax error, annotations are only available if source level is 1.5 or greater E:\work\datai\code_study\study-master\study-spring-transaction\src\main\java\cn\sw\study\web\controller\UserController.java:13@RequestMapping("/user")[ERROR] Missing message: configure.incompatibleComplianceForSource in: org.aspectj.ajdt.ajc.messages <unknown source file>:<no line information>[ERROR] no sources specified <unknown source file>:<no line information>
After the addition is complete, maven compiles, if there is a java:0::0 can't determine annotations of missing type javax.persistence.Entity error,
则需要添加persistence-api依赖<dependency> <groupId>javax.persistence</groupId> <artifactId>persistence-api</artifactId> <version>1.0</version> <scope>provided</scope></dependency>
After the compilation is completed without errors, you do not need to add -javaagent at this time, start the project again, and you can complete the transaction weaving
Precautions
Aspectj has three periods that can be woven in, compiler, post-compiler, class loader, if you are interested, you can try them all