Need to configure a Spring AOP Advice within an AspectJ project

I've got a Spring project that uses AspectJ. In 99.9% of the cases, using AspectJ (ajc) to CTW my aspects is working as expected.

However, in one particular situation, I find I am needed to weave my aspect around a Spring aspect. More specifically, I want to wrap a Spring @Transactional.

If I use AspectJ ajc to CTW my advice, it will weave it around any method that is defined as @Transactional. Rather, I want to wrap the Spring @Transactional advice. Basically, I am trying to catch any errors that are thrown within a Transaction and retry the transaction if failed.

My problem is that by already using ajc in the rest of the project, it automatically weaves in my aspect into my classes, rather than allowing Spring's AOP mechanisms to interpret it and weave it at run time.

My project is using the aspectj-maven-plugin . How do I configure my pom/project such that ajc ignores my @Aspect, but Spring still sees it?


My project is using the aspectj-maven-plugin. How do I configure my pom/project such that ajc ignores my @Aspect, but Spring still sees it?

Don't include the Spring aspect library.

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>aspectj-maven-plugin</artifactId>
    <version>1.5</version>
    <configuration>
        <complianceLevel>${project.javaVersion}</complianceLevel>
        <encoding>UTF-8</encoding>
        <aspectLibraries>
            <!-- remove this -->
            <aspectLibrary>
                <groupId>org.springframework</groupId>
                <artifactId>spring-aspects</artifactId>
            </aspectLibrary>
        </aspectLibraries>

Having both compile time weaving and run time proxy-based AOP is probably more trouble than it's worth and is likely to introduce other issues. It shouldn't be a problem to have both your aspect and the standard Sping aspect advise @Transactional methods. If I were you'd I'd stick with compile time weaving and set the precedence on your aspect so that it is run first. Within your aspect, you can catch exceptions and retry as needed.

From the AspectJ docs:

A piece of around advice controls whether advice of lower precedence will run by calling proceed. The call to proceed will run the advice with next precedence, or the computation under the join point if there is no further advice.

A piece of before advice can prevent advice of lower precedence from running by throwing an exception. If it returns normally, however, then the advice of the next precedence, or the computation under the join pint if there is no further advice, will run.

链接地址: http://www.djcxy.com/p/25406.html

上一篇: AspectJ和Spring eclipse的配置

下一篇: 需要在AspectJ项目中配置Spring AOP建议