0

I was task to log how long a certain method from a maven dependency would take. This is the Aspect class.

@Aspect
@Component
@Order(1)
public class CloseableHttpClientAsp {
    private Long startMs;
    private Long endMs;

    @Pointcut(value = "execution(* org.apache.http.impl.client.CloseableHttpClient.execute(..))")
    void executePointcut() {
    }

    @Before(value = "executePointcut")
    public void doBefore(JoinPoint point) throws Throwable {
        startMs = new Date().getTime();
    }

    @After(value = "executePointcut")
    public void doAfter(JoinPoint point) throws Throwable {
        endMs = new Date().getTime();
        Long ellapseMs = endMs - startMs;
        // Other logging happens here
    }
}

The class CloseableHttpClient is from org.apache.httpcomponents:httpclient:4.5.13. It never enters doBefore and doAfter. I tried to replace the expression on the @Pointcut annotation to a class from my project just to check if it will work, and it does. Does that mean I cannot add Aspect to classes that are not from my own project? Is there anyway to accomplish what I need to?

Gideon
  • 1,469
  • 2
  • 26
  • 57
  • Gideon, you need to use native AspectJ when targeting non-Spring components. The answer I linked to when closing this question as a duplicate explains the difference between Spring AOP and AspectJ and also links to documentation regarding how to use AspectJ from within Spring. – kriegaex Nov 09 '22 at 14:08

0 Answers0