使用AspectJ Compiler解决Spring @Transactional方法自调用失效

If we want to use the @Transactional annotation on private methods, we cannot use Spring’s default proxy mode - it only works for public methods that are called from client code.

For self-invoked methods we need to use AspectJ.

An Example

This may be the case e.g. when we want to save incoming data and send a letter using that data within a transaction. If saving the data fails we do not want to send the letter. If sending the letter fails we do not want to loose the data that we received. Hence, we want to save the data in a separate transaction, by annotating our saveData(..) method with @Transactional(propagation = Propagation.REQUIRES_NEW).

Configuring Spring To Use AspectJ

On a @Configuration class we just need to set the AspectJ advice mode for the transaction management configuration:

1
2
3
4
5
6
@Configuration
@ComponentScan
@EnableTransactionManagement(mode = AdviceMode.ASPECTJ)
class ApplicationConfiguration {
    // ...
}

Configuring Maven To Run The AspectJ Compiler

Next we need to use the AspectJ compiler, so let’s add its needed dependencies:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<dependencies>
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjrt</artifactId>
        <version>1.7.4</version>
    </dependency>

    <!-- In this example we use JPA for persistence. -->
    <dependency>
        <groupId>org.hibernate.javax.persistence</groupId>
        <artifactId>hibernate-jpa-2.1-api</artifactId>
        <version>1.0.0.Final</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aspects</artifactId>
        <version>4.0.6.RELEASE</version>
    </dependency>
</dependencies>

The AspectJ compiler itself can then be enabled with:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<build>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>aspectj-maven-plugin</artifactId>
            <version>1.6</version>
            <executions>
                <execution>
                    <goals>
                        <goal>compile</goal>
                        <!-- <goal>test-compile</goal> -->
                    </goals>
                </execution>
            </executions>
            <configuration>
                <complianceLevel>1.7</complianceLevel>
                <forceAjcCompile>true</forceAjcCompile>
                <weaveDirectories>
                    <weaveDirectory>${project.build.outputDirectory}</weaveDirectory>
                </weaveDirectories>
                <aspectLibraries>
                    <aspectLibrary>
                        <groupId>org.springframework</groupId>
                        <artifactId>spring-aspects</artifactId>
                    </aspectLibrary>
                </aspectLibraries>
            </configuration>
        </plugin>
    </plugin>
</build>

If we do not use <forceAjcCompile> and do not set the <weaveDirectory> AspectJ will not weave the transaction code into our generated Groovy bytecode. Instead, it will warn us with No sources found skipping aspectJ compile when Maven runs. That is as the aspectj-maven-plugin processes the src/main/java directory by default but not src/main/groovy.

How The AspectJ Generated Code Looks Like

Just for completeness, let’s look how AspectJ has wrapped the saveData(..) method with a transaction. Using the Procyon decompiler we can see:

1
2
3
4
@Transactional(propagation = Propagation.REQUIRES_NEW)
public void saveData(final SomeData data) {
    ((AbstractTransactionAspect)AnnotationTransactionAspect.aspectOf()).ajc$around$org_springframework_transaction_aspectj_AbstractTransactionAspect$1$2a73e96c((Object)this, (AroundClosure)new DataService$AjcClosure3(new Object[] { this, data }), DataService.ajc$tjp_1);
}

转载于:https://my.oschina.net/yqz/blog/1603556

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值