4

I have a spring boot application with some REST controllers, service classes and helper classes. The controllers and service classes are spring managed while helper classes are not spring managed and mostly contain static methods.

The AspectJ configuration is present in java configuration as follows

@Configuration
@EnableLoadTimeWeaving(aspectjWeaving = AspectJWeaving.ENABLED)
public class AspectConfig {

    @Bean
    public LoggingAspect loggingAspect() {
        return new LoggingAspect();
    }
}

The corresponding LoggingAspect class is as follows,

@Aspect
public class LoggingAspect {

    @Before("allMethodsPointcut()")
    public void logBeforeMethod(JoinPoint joinPoint) {
        System.out.println("Entering Method - " + joinPoint.getSignature().getDeclaringType() + "::" + joinPoint.getSignature().getName());
    }

    @After("allMethodsPointcut()")
    public void logAfterMethod(JoinPoint joinPoint) {
        System.out.println("Exiting Method - " + joinPoint.getSignature().getDeclaringType() + "::" + joinPoint.getSignature().getName());
    }

    @Pointcut("execution(* com.test.controller..*(..)) || execution(* com.test.service..*(..)) || execution(* com.test.helper..*(..))")
    public void allMethodsPointcut() {
    }
}
  • When the controller is called, the Aspect enabled logging works for the controller and service functions but not for the helper functions.
  • If we autowire the helper class in the controller, the non-static helper methods start showing the aspectj logs. However, the static helper methods still do not show the aspectj logs

Questions, 1. How can we configure the aspectj advice for classes which are not spring managed i.e. without @Bean, @Autowired, @Component etc. 2. How can we configure aspectj advice for static methods (I am using @EnableLoadTimeWeaving but maybe i am missing something) 3. AspectJ configuration should be java based if possible

Kindly let me know if more details are required

Avi
  • 1,070
  • 1
  • 15
  • 28

1 Answers1

3

Use -javaagent:/path/to/aspectjweaver-<version>.jar as a startup argument to your JVM to enable load-time weaving. Remove @EnableAspectJAutoProxy from your spring configuration so that spring doesn't try to use it's own Spring AOP framework instead of pure AspectJ. Optionally, create META-INF/aop.xml. Add @EnableSpringConfigured if you want to apply spring configuration to beans not managed by spring (@Configurable POJOs).

Nándor Előd Fekete
  • 6,988
  • 1
  • 22
  • 47
  • 1
    I have read similar comments on stack overflow, but I am wondering why It is not working with EnableLoadTimeWeaving included in the configuration. I have NOT included EnableAspectJAutoProxy so spring should not be interfering with the weaving process. Also the deployment server may not be under my control, hence skeptical about adding command line VM arguments. For the last part, do you mean every class that is not spring managed should be annotated with 'Configurable'? – Avi Dec 30 '16 at 12:57
  • 1
    `@EnableLoadTimeWeaving` works through classloader magic. Doing this kind of thing through classloading is always risky, so I try to avoid it whenever possible. That's why I never use Spring's `@EnableLoadTimeWeaving`. Look at `LoadTimeWeaver` class' hierarchy to see what environments are supported. Then, pick your environment and see the the specific implementation class' documentation for any additional configuration you might have to provide. Of course the best would be to get rid of all this stuff and just go with compile time weaving. Or just take control of your servers and use the agent – Nándor Előd Fekete Dec 30 '16 at 13:28
  • For example, if you're using Tomcat versions [6..8) you'll need to specify the classloader explicitely: `` in a `META-INF/context.xml` file in your war. – Nándor Előd Fekete Dec 30 '16 at 13:39
  • `@Configurable` annotation is needed if you want your non-spring managed beans getting configured at construction time by spring. You don't need it to apply weaving to classes. You'll need it though if these non-spring managed classes have `@Autowired` fields or any other spring bean postprocessing needs to be applied to them. – Nándor Előd Fekete Dec 30 '16 at 13:42