Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring aop multiple pointcuts & advice but only the last one is working

Tags:

java

spring

aop

I have created two Spring AOP pointcuts that are completely separate and will be woven in for different parts of the system. The pointcuts are used in two different around advices, these around-advices will point to the same Java method.

How the xml file looks:

<aop:config>
    <aop:pointcut expression="execution(......)" id="pointcutOne" />
    <aop:pointcut expression="execution(.....)" id="pointcurTwo" />

    <aop:aspect id="..." ref="springBean">
        <aop:around pointcut-ref="pointcutOne" method="commonMethod" />
        <aop:around pointcut-ref="pointcutTwo" method="commonMethod" />
    </aop:aspect>
</aop:config>

The problem is that only the last pointcut works (if I change the order pointcutOne works because it is the last one). I have gotten it to work by creating one big pointcut, but I would like to have them separate. Any suggestions to why only one of the pointcuts works at a time?

like image 399
Jarle Hansen Avatar asked May 04 '10 07:05

Jarle Hansen


People also ask

What is the difference between JoinPoint and pointcut?

JoinPoint: Joinpoint are points in your program execution where flow of execution got changed like Exception catching, Calling other method. PointCut: PointCut are basically those Joinpoints where you can put your advice(or call aspect). So basically PointCuts are the subset of JoinPoints.

How do I declare a pointcut in Spring AOP?

For using Spring AOP in Spring beans, we need to do the following: Declare AOP namespace like xmlns:aop=“https://www.springframework.org/schema/aop” Add aop:aspectj-autoproxy element to enable Spring AspectJ support with auto proxy at runtime. Configure Aspect classes as other Spring beans.

What is pointcut in AspectJ?

AspectJ provides primitive pointcuts that capture join points at these times. These pointcuts use the dynamic types of their objects to pick out join points. They may also be used to expose the objects used for discrimination. this(Type or Id) target(Type or Id)


1 Answers

Try to have the pointcut and advice inside the <aop:aspect> element. Something like this:

<aop:config>
  <aop:aspect id="aspect1" ref="springBean">
    <aop:pointcut expression="execution(......)" id="pointcutOne" />
    <aop:around pointcut-ref="pointcutOne" method="commonMethod" />
  </aop:aspect>

  <aop:aspect id="aspect2" ref="springBean">
    <aop:pointcut expression="execution(.....)" id="pointcurTwo" />
    <aop:around pointcut-ref="pointcutTwo" method="commonMethod" />
  </aop:aspect>
</aop:config>

I guess your XML configuration resulted in just one proxy object, while it should have been two proxy objects.


Btw: You should consider using @AspectJ syntax instead. It's just Java with pointcuts and advices in annotations. It works well with Spring AOP and offers more functionality than the XML alternative.

All you need in your configuration to enable @AspectJ aspects with Spring AOP:

<aop:aspectj-autoproxy>
     <aop:include name="aspect1" />
     <aop:include name="aspect2" />
</aop:aspectj-autoproxy>

<bean id="aspect1" class="com.demo.Aspect1"/>
<bean id="aspect2" class="com.demo.Aspect2"/>

And the Aspect could be something like this:

@Aspect
public class Aspect1 {

    @Pointcut("execution(* *(..))")
    public void demoPointcut() {}

    @Around("demoPointcut()")
    public void demoAdvice(JoinPoint joinPoint) {}
}

Updated:

Example that uses a pointcut to combine three other pointcuts:

@Pointcut("traceMethodsInDemoPackage() && notInTestClass() " +
    "&& notSetMethodsInTraceDemoPackage()")
public void filteredTraceMethodsInDemoPackage() {}
like image 62
Espen Avatar answered Sep 18 '22 18:09

Espen