0

I have the following issue with AspectJ. My java web application includes multiple dependencies which uses aspects (contain their own META-INF/aop.xml inside jars). Here is an example of aop.xml:

<aspectj>
       <weaver>
              <include within="package..*"/>
       </weaver>
       <aspects>
              <aspect name="package.LoggingAspect" />
       </aspects>
</aspectj>

And here is the code of the aspect:

@Aspect
public class LoggingAspect {

    @Around("executeMethod() && @annotation(someAnnotation)")
    public Object execute(ProceedingJoinPoint joinPoint, SomeAnnotation a) throws Throwable {
        //some logic here
    }

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

In my application I need to override the logic under the given advice. How can I achive this? I tried to provide my own aspect with the same advice (same conditions), but in this case both aspects are applied. Is there any way to exclude unnecessary aspect/advice or override it?

UPD. As temporary solution I copied the code of LoggingAspect to my own, changed the logic of the advide and provided aop.xml with the following:

<aspects>
    <aspect name="package.MyLoggingAspect"/>
    <exclude within="package.LoggingAspect"/>
</aspects>
igor
  • 179
  • 1
  • 1
  • 5
  • I am not sure if I understand correctly - do you have an aspect with the same `@Around` advice that you can't modify and you want to change its behaviour? – Ondra K. Dec 04 '18 at 14:52
  • @igor I once did something similar. in your LoginAspect class, keep a list with "implementations", and depending on a parameter you add to your annotation, choose the right implementation to run. – Stultuske Dec 04 '18 at 14:57
  • @OndraK. Yes, you are right. I can't make changes to that libraries but I need to override the behaviour of one single advice. I added temporary solution to my question. – igor Dec 04 '18 at 15:30
  • @igor if you can't change the code, you can't change it's behaviour. it's quite obvious – Stultuske Dec 05 '18 at 06:41
  • You can only extend abstract aspects and e.g. provide concrete pointcuts for abstract ones. An aspect cannot extend another concrete aspect. So your own solution with the adjusted LTW configuration is just fine. The other (not so nice) option would be to make sure that your re-implementation of the aspect under the very same class + package name is found on the classpath before the original one - ugly! – kriegaex Dec 12 '18 at 05:29

0 Answers0