1

So my question revolves around the use of Spring AOP with XML based schema versus using it with AspectJ. From looking around online I've been trying to figure out which approach to take for AOP. One particular scenario has me slightly confused;

Suppose I have a number of classes with n number of methods, and I want to apply advice from my aspect class to certain methods/join points but not all, I can see when using AspectJ that this would be fairly straightforward - I just apply my aspect annotation to the methods that should use the advice. However, from what I've seen of the xml based approach, I would have to create a pointcut for each of these methods (assuming they can't be covered by one expression i.e. each method has a distinct name) and (if I was using the proxy based approach) a proxy class for each target/class. The AspectJ approach seems much tidier in this sense.

So is my understanding of the two methods correct or have I missed out on some part of Spring AOP that can achieve a neater solution for the xml approach?

Sorry for the long-winded explanation, but I wanted to make the scenario as clear as possible...

1 Answer 1

2

It sounds like you are trying to decide between Spring AOP and AspectJ, but you're assuming that Spring AOP requires XML-based configuration. It doesn't. You can use AspectJ annotations for both Spring AOP and AspectJ:

package com.example.app;

import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;

@Aspect
public class NotificationAspect {
    @Autowired private NotificationGateway notificationGateway;

    @Pointcut("execution(* com.example.app.ItemDeleter.delete(com.example.app.Item))")
    private void deleteItemOps() { }

    @AfterReturning(pointcut = "deleteItemOps() && args(item)")
    public void notifyDelete(Item item) {
        notificationGateway.notify(item, ConfigManagementEvent.OP_DELETE);
    }
}

So if you're trying to compare Spring AOP and AspectJ, it's more sensible to compare AspectJ to annotation-based Spring AOP.

Spring AOP is generally simpler (you don't need the AspectJ compiler); hence the reference docs recommend Spring AOP over AspectJ unless you need more exotic pointcuts.

UPDATE: Responding to the OP's comment below, we can use XML configuration to advise specific methods:

<aop:config>
    <aop:pointcut
        id="deleteItemOps"
        expression="execution(* com.example.app.ItemDeleter.delete(com.example.app.Item))" />
    <aop:advisor
        advice-ref="notificationAdvice"
        pointcut-ref="deleteItemOps() && args(item)" />
</aop:config>

If you want to embed the pointcut right in the <aop:advisor>, you can do that too:

<aop:config>
    <aop:advisor
        advice-ref="notificationAdvice"
        pointcut="execution(* com.example.app.ItemDeleter.delete(com.example.app.Item)) && args(item)" />
</aop:config>

(I haven't checked the && args(item) part of the XML configuration, but I think that's OK for the example I gave. If it doesn't work, try removing it and feel free to edit the answer accordingly.)

4
  • Yeah I guess I was asking about using spring aop with the xml config versus using spring aop with the aspectj annotations, using it with the aspectj annotations seems to allow us to apply our aspects to individual methods/joinpoints while using the xml config means we will apply it to a class or a group of methods included in the pointcut expression - spring with aspectj does seem to provide more flexibility in this regard?
    – Shane
    Oct 12, 2014 at 0:53
  • Yeah, I thought that might have been your intent. Consider updating the title to reflect that. At any rate, I updated the response.
    – user41871
    Oct 12, 2014 at 1:06
  • Thanks, will do, I suspect that the above solution will require a different advisor tag for each pointcut/method where the advice would be applied?
    – Shane
    Oct 12, 2014 at 1:20
  • Yes, that's correct. It allows you to bind the advice to the joinpoint (method).
    – user41871
    Oct 12, 2014 at 1:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.