Reputation: 23
What does Spring Boot do in the event that there is code to use both Spring AOP with run time weaving/use of proxies and compile time weaving in the same project. For example, say you have an aspect that looks like this:
@Aspect
@Component
public class TestAspect {
// ...
}
where the existence of @Component would imply that we are using Spring AOP and proxies, and you have this in your pom.xml
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.11</version>
<executions>
<execution>
<goals>
<goal>compile</goal> <!-- use this goal to weave all your main classes -->
<goal>test-compile</goal> <!-- use this goal to weave all your test classes -->
</goals>
</execution>
</executions>
</plugin>
which would imply we are using compile time weaving.
Which one takes precedence? Is compile time weaving used, or proxies?
Upvotes: 2
Views: 2772
Reputation: 27
I've recently ran into a situation where Spring AOP seems to be disabled when AspectJ (P)CTW kicks in, but not application wise but only class wise. After some debugging in spring code, I figured out that the above observation is conditionally true
In our case, we registered the ajc aspects as beans by using aspectOf()
, so that the aspects can also have spring bean dependency injected. This makes the aspects visible to spring.
(But we forgot to configure to weave our code with this aspect, so our aspect was actually working under spring proxy aop not ajc weaving)
Spring AOP register all the @Aspect
beans as Advisor
and look it up when creating beans when creating proxy beans.
(Proxy beans, thus upon creation, have determined eligible aspects)
When testing whether an advisor is eligible for a target bean,
AspectJExpressionPointcut.matches(Class<?> targetClass)
is called down the call stack(possibly for InstantiationModelAwarePointcutAdvisorImpl
ish only).
And the aspectOf() created aspects' relevant advisor fails the test at this line (this.aspectCompiledByAjc && compiledByAjc(targetClass))
the former is true since we used aspectOf()
, the latter actually checks the class definition for signs of ajc compilation with some adhoc logics.
To conclude, Advisor
s with AspectJExpressionPointcut
type ClassFilter
cannot be applied to already ajc weaved class
(the advisor for @Transactional
uses TransactionAttributeSourcePointcut
and doesn't check if ajc compilation happened, so spring proxy based @Transactional works even on ajc compiled classes)
Upvotes: 0
Reputation: 67457
Hm, why didn't you just try? Wouldn't that have been faster than asking? OK, here are my thoughts:
aspectjrt.jar
on the classpath. If that is the case, the application should run normally, with or without Spring.<aspectLibraries>
configuration.Bottom line: You cannot mix Spring AOP and AspectJ LTW, but you should be able to use Spring AOP + AspectJ CTW. You just should have good reasons to do so and understand what you are doing.
Upvotes: 3