Question

I am trying to execute code before a private method using AspectJ's load-time-weaving with Spring-Boot and annotation-based configuration and I'm pulling my hair out trying to figure out why my aspect is not being invoked.

My simple aspect is as follows:

@Aspect
public class LoggingAspect {

  private static Logger log = LoggerFactory.getLogger(LoggingAspect.class);

  @Before("execution(private * com.mycompany.MyServiceWithPrivateMethods.*(..))")
  public void privateAspect() {
    log.warn("#### Private method aspect invoked!");
  }

}

I also have the @EnableLoadTimeWeaving annotation on my Spring configuration class and the following in my META-INF/aop.xml:

<aspectj>    
    <weaver options="-verbose">
        <!-- only weave classes in our application-specific packages -->
        <include within="com.mycompany.*"/>
    </weaver>

    <aspects>
        <!-- weave in just this aspect -->
        <aspect name="com.mycompany.LoggingAspect"/>
    </aspects>    
</aspectj>

Lastly, I am also starting my application with the -javaagent:/path/to/spring-instrument.jar option, per the docs. I also have the required jars on my classpath (spring-aop and aspectjweaver) as also mentioned in the Spring docs.

Is this even possible? There is a 'limitation' listed in the AspectJ docs that states Privileged aspects are not supported by the annotation style. located here, however this older post in the Spring Forum seems to indicate that it is indeed possible.

Was it helpful?

Solution

I would have thought it would work, but I would have also thought you would have to add aspectjweaver.jar as a javaagent. One of those assumptions is wrong?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top