1
votes

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

1 Answers

2
votes

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.)