0

I have an aspect written in a project called connector and generated a jar for that project. I have integrated that jar into another spring boot application as a Gradle dependency. This aspect is not working.

JAR FILE CODE

TrackExecutionTime.java

package a.b.aspect;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface TrackExecutionTime {}

ConnectorAOPConfig.java

package a.b.aspect;

@Aspect
@Component
public class ConnectorAOPConfig {

    public ConnectorAOPConfig() {
    }

    @Around("@annotation(a.b.aspect.TrackExecutionTime)")
    public Object logExecutionTime(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("Inside Aspect");
        return joinPoint.proceed();
    }
}

MAIN CODE

AOPConfig.java

package a.c.config;

// Imports here

@Configuration
@EnableAspectJAutoProxy
@ComponentScan(basePackages = {"a.b.aspect"})
public class AOPConfig {
 @Bean
 public ConnectorAOPConfig aopHandler()
 {
        return new ConnectorAOPConfig();
 }
}

Code targeted by aspect:

@TrackExecutionTime
public static ApplicationDTO fetchApplicationByName(String name) {
    //code
    return application;
}

Note: I added that annotation in the connector jar only. Still it is not getting triggered.

kriegaex
  • 63,017
  • 15
  • 111
  • 202
  • what is the package for `ConnectorAOPConfig.java`? – R.G Aug 10 '22 at 08:28
  • @R.G - Same as TrackExecutionTime.java. I have updated the question with package now. Thanks – Prathap Puppala Aug 10 '22 at 11:01
  • Could you confirm if the bean `ConnectorAOPConfig` is registered with the container ? Also how is the annotated method triggered ? Do share the code that calls the target method and the target method that is getting advised. – R.G Aug 10 '22 at 11:08
  • ```@TrackExecutionTime public static ApplicationDTO fetchApplicationByName(String name) { //code return application; }``` – Prathap Puppala Aug 10 '22 at 12:52
  • You cannot advise a `static` method using Spring AOP . Spring AOP is proxy based. You could search on this topic for more details. – R.G Aug 10 '22 at 13:31
  • https://stackoverflow.com/q/55964758/4214241 – R.G Aug 10 '22 at 14:14
  • Does this answer your question? [How to advise static methods using Spring AOP?](https://stackoverflow.com/questions/55964758/how-to-advise-static-methods-using-spring-aop) – kriegaex Aug 20 '22 at 12:27

0 Answers0