I am currently developing two Spring applications that makes use of Spring-AOP. I have an aspect that allows simple performance logging which is defined as such:
@Aspect
final class PerformanceAdvice {
private Logger logger = LoggerFactory.getLogger("perfLogger");
public Object log(final ProceedingJoinPoint call) throws Throwable {
logger.info("Logging statistics.");
}
}
This advice can then be created through Spring AOP configuration with the following XML:
<bean id="performanceAdvice" class="com.acme.PerformanceAdvice" />
<aop:config>
<aop:aspect ref="performanceAdvice">
<aop:around pointcut="execution(* com.acme..*(..))" method="log"/>
</aop:aspect>
</aop:config>
This works fine for classes that are created by Spring such as classes annotated with @Service
. But I'd like this aspect to also advise other aspects in the secondary project. I'm aware Spring doesn't support this as noted in their docs:
In Spring AOP, it is not possible to have aspects themselves be the target of advice from other aspects. The @Aspect annotation on a class marks it as an aspect, and hence excludes it from auto-proxying.
Thus I'm probably in need of something more powerful such as AspectJ
. Or is it possible to make Spring aware of the aspect and still allow advising? From numerous other questions (that aren't directly related to this specific problem) on StackOverflow I have tried making aspects @Configurable
, making them Spring-aware by defining them as a @Component
and played around with various XML and plugin settings such as:
<context:spring-configured />
<context:annotation-config/>
<context:load-time-weaver/>
<aop:aspectj-autoproxy/>
I'm running out of ideas now. Will I be needing to write fully-fledged AspectJ
aspects? If so, can it use the same configuration such as Spring, referencing the existing aspect and defining a new pointcut? This would be useful so I don't have to re-work the PerformanceAdvice
for Project 1
but still reference and use it in Project 2
.
edit regarding this comment: To make myself more clear, I have the following example.
I have a Service in Project 2
.
@Service
public class TargetSpringServiceImpl implements TargetSpringService {
@Override
public String doSomeComplexThings(String parameter) {
return "Complex stuff";
}
}
When this method gets called, I have an aspect that does some validation.
@Aspect
public class ValidationAdvice {
@Autowired
ValidationService validationService
public void validate(JoinPoint jp) throws Throwable {
//Calls the validationService to validate the parameters
}
}
With the following pointcut as execution:
<bean id="validationAdvice" class="com.acme.advice.ValidationAdvice" />
<aop:config>
<aop:aspect ref="validationAdvice">
<aop:before pointcut="execution(* com.acme.service..*(..))" method="validate"/>
</aop:aspect>
</aop:config>
I'd like to have my PerformanceAdvice
's log()
method to be invoked on the ValidationAdvice
's validate()
method. NOT on the doSomeComplexThings()
method of the TargetSpringService
class. As this is just an additional pointcut. The problem lies with advising the method of another aspect.
If i understand you whant to perform action on aspect advices what i would do is something like that (Using aspectj can be change to spring annotation) :
Then i create others aspects extending this aspect.
So (from what I understand) you want to re-use the advise of your first project in your second project. But additionally you also want to add further logic to the advise. This can be done by wrapping the advise of project-1 with a custom implementation in project-2. You can do that with wrapping additional advices (see Advice ordering):
Project 1 requires some minor modifications (alternatively to implementing
Ordered
you could use the@Order
annotation / and of course you could also inject the ordering instead of hardcoding it):Create the custom Advice-advising implemenation in your Project 2
Wire the components together:
Aspect with the
Ordered.HIGHEST_PRECEDENCE
is called first.First of all Spring does not use aspectJ to implement AOP but either JDK or cglib dynamic proxies. The aspectJ style here just refers to the syntax.
Aspectj aspects are static and use bytecode injection applied either at compile or at class-loading time.
Then spring is only able to apply proxy on objects it manages because dynamic proxies are applied during dependency injection. Moreover if you have 2 differents projects, you must ensure they share the same spring context or they will be isolated and applying aspect from 1 project to beans of the 2nd won't work.
And yes you will probably have to use real aspectJ aspects here. For performance logging purpose it's more suitable as there will be about no performance impact.
I have figured out two possible solutions to my problem. One is actually advising the aspect, the other works around the problem but is actually more elegant.
Solution 1: Advise the aspect
In
AspectJ
it's possible to weave just about anything. With the help of aMETA-INF/aop.xml
file as stated in the AspectJ LTW documentation, I could reference the aspect and define a new pointcut in the following way.Changes to project 1
The
PerformanceAdvice
To allow
AspectJ
to define a new pointcut, the advice has to beabstract
and have an abstractpointcut
method that can be hooked into.Changes to project 2
The
META-INF/aop.xml
The
aop.xml
file defines a new aspect calledConcretePerformanceAdvice
. It extends of theAbstractPerformanceAdvice
as well but defines a new pointcut. Then, inAspectJ
it IS possible (unlike in Spring-AOP) to define a pointcut to another aspect.The
pom.xml
Weaving the aspect requires some instrumentation. This requires both a dependency and a plugin to execute it. As for the dependency, it is the following:
At the moment, during testing, I do the instrumentation through the
surefire-plugin
. That requires the following bit:The Spring context
To enable the load-time weaving, it's also necessary to activate the weaving. So the following has to be added to the Spring context.
Solution 2: Delegate to a Spring bean
Spring-AOP
does not allow aspects to advise other aspects. But it does allow advice to be run on a Spring@Component
of course. So the other solution would be to move the validation done in the advice, to another Spring bean. This Spring bean is then autowired into the advice and executed, but thePerformanceAdvice
has its pointcut on the validation component and not on the validation aspect. So it would look like the following:Changes to project 1
None!
Changes to project 2
The advice autowires the Spring
@Component
and delegates its logic to the component.Then in the Spring context it's possible to define the pointcut on the
@Component
while theValidationAdvice
autowires the@Component
.