3

I need to capture private inner calling methods.

So, I need to use aspectj weaving into my spring-boot project:

@Configuration
@EnableAspectJAutoProxy
public class ApiConfiguration implements WebMvcConfigurer { /*...*/ }

I need to capture a @Service private method execution:

package net.space.service;

// imports

@Service    
public class RepositoryService {
    private void privateMethod(String param) {
        /* Do something */
    }

    public void innerCaller() {
        this.privateMethod(null);
    }
}

IMPORTANT: privateMethod is private and is called only by innerCaller.

However, advice is never reached. How could I solve that?

I've also tried with this pointcut:

@Pointcut(value = "execution(* privateMethod(..))")
public void privatePointcut() {
}

and advice:

@AfterReturning("privatePointcut()")
public void groupMetrics(JoinPoint point) throws Throwable {
    // Do something
}

I've also tried with:

@Pointcut(value = "execution(* net.space.service.RepositoryService.privateMethod(..))")
@Pointcut(value = "execution(* RepositoryService.privateMethod(..))")

EDIT

I've also tried to use @EnableLoadTimeWeaving:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'loadTimeWeaver' defined in class path resource [org/springframework/context/annotation/LoadTimeWeavingConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.instrument.classloading.LoadTimeWeaver]: Factory method 'loadTimeWeaver' threw exception; nested exception is java.lang.IllegalStateException: ClassLoader [sun.misc.Launcher$AppClassLoader] does NOT provide an 'addTransformer(ClassFileTransformer)' method. Specify a custom LoadTimeWeaver or start your Java virtual machine with Spring's agent: -javaagent:org.springframework.instrument.jar at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:590) ~[spring-beans-5.0.8.RELEASE.jar:5.0.8.RELEASE]

Jordi
  • 20,868
  • 39
  • 149
  • 333

1 Answers1

0

I got the answer;

Spring AOP is a proxy-based but has limitations regarding non-public methods;

Due to the proxy-based nature of Spring’s AOP framework, protected methods are by definition not intercepted, neither for JDK proxies (where this isn’t applicable) nor for CGLIB proxies (where this is technically possible but not recommendable for AOP purposes). As a consequence, any given pointcut will be matched against public methods only!

If your interception needs include protected/private methods or even constructors, consider the use of Spring-driven native AspectJ weaving instead of Spring’s proxy-based AOP framework. This constitutes a different mode of AOP usage with different characteristics, so be sure to make yourself familiar with weaving first before making a decision.

Therefore you'd need to enable native AspectJ weaving with the following;

The example presented here uses XML style configuration, it is also possible to configure and use @AspectJ with Java Configuration. Specifically the @EnableLoadTimeWeaving annotation can be used as an alternative to (see below for details).

So can you try with;

@Configuration
@EnableLoadTimeWeaving
public class ApiConfiguration implements WebMvcConfigurer { /*...*/ }
buræquete
  • 14,226
  • 4
  • 44
  • 89
  • I've edited post. It's getting me an exception on service kicking off. – Jordi Nov 14 '18 at 13:56
  • @Jordi can [this](https://stackoverflow.com/questions/7296627/springs-loadtimeweaver-agent-not-starting-up) help? – buræquete Nov 14 '18 at 14:29
  • @Jordi sorry for not checking back on this, but I'd love to hear your feedback on this. – buræquete Nov 29 '18 at 07:49
  • Jordi, you have been flooding SO with AOP-related questions lately, some still open, some deleted again by yourself later. So please at least kindly follow up on them if people volunteer to help you, or else you will end up with people no longer answering your questions. – kriegaex Dec 10 '18 at 08:22