-2

Annotation

@Target(ElementType.METHOD) // Annotation can be applied to methods
@Retention(RetentionPolicy.RUNTIME) // Annotation will be retained at runtime
public @interface MethodExecutionTime {
} 

Aspect class

@Aspect
@Component
public class MethodExecutionTimeAspect {
    private static final Logger logger = LoggerFactory.getLogger(MethodExecutionTimeAspect.class);
    @Around("@annotation(com.teamsapi.custom_annotation.annotation.MethodExecutionTime)")
    public Object measureExecutionTime(ProceedingJoinPoint joinPoint) throws Throwable {
        long startTime = System.currentTimeMillis();
        Object result = joinPoint.proceed();
        long endTime = System.currentTimeMillis();
        long executionTime = endTime - startTime;
        logger.warn("Method '{}' took {} milliseconds to execute.", joinPoint.getSignature().toShortString(), executionTime);
        return result;
    }
}

config

dependencies {
    implementation 'org.jsoup:jsoup:1.15.3'
    implementation 'org.springframework.boot:spring-boot-starter-data-mongodb'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    implementation 'org.springframework:spring-aop:5.3.9'
    implementation 'org.aspectj:aspectjweaver:1.9.7'
}


plugins {
    id 'java'
    id 'org.springframework.boot' version '3.1.0'
    id 'io.spring.dependency-management' version '1.1.0'
}

this is a method in service class, not working for it

@MethodExecutionTime
    private String responseDataReceived(String url) {
       return "code";
    }

how to execute this code properly tell me any way to implement it properly working fine for some method while not for some method, why it is not working tell me the way to resolve it

  • Is the annotated method in a Component class or otherwise instantiated by Spring? – Jorn Jul 06 '23 at 11:18
  • @service class only – Ansh Gupta Jul 06 '23 at 16:14
  • @Jorn any idea ? – Ansh Gupta Jul 06 '23 at 19:07
  • Remove the `spring-aop` dependency and `aspectj` dependency, add `spring-boot-starter-aop` instead. You are mixing jars from different (and incompatible) versions of Spring . This will never work. Never mix versions of frameworks (regardless of the framework). That being said there is already the `@Timed` annotation from the already supported micrometer project which I would recommend to use instead of re-inventing your own. Next Spring AOP works with proxies meaing only public/protected methods are intercepted and only call into the object not methods on itself (as you are inside the proxy). – M. Deinum Jul 07 '23 at 06:00
  • @M.Deinum changed the dependency, still not working – Ansh Gupta Jul 07 '23 at 10:51
  • And you apparently stopped reading after the first sentence. You skipped the **Next Spring AOP works with proxies meaing only public/protected methods are intercepted and only call into the object not methods on itself (as you are inside the proxy)**. As the method is `private` it is an invocation on the same object. – M. Deinum Jul 09 '23 at 06:46

1 Answers1

0

Inspecting your code, ...

@MethodExecutionTime
private String responseDataReceived(String url) {
  return "code";
}

... I see that the annotated method is private. Spring AOP is proxy-based, and dynamic proxies technically are subclasses. A subclass in Java cannot see or call its parent's private methods, hence the term "private". I.e., you can only intercept public methods (both JDK interface proxies and CGLIB proxies) and protected or class-scopend methods (CGLIB only) using Spring AOP. You would need native AspectJ to intercept private ones, too. But actually, if the method is worth being intercepted, why would it be private, i.e. an implementation detail hidden from the outside world?

P.S.: What is "sustome" supposed to mean?

kriegaex
  • 63,017
  • 15
  • 111
  • 202