0

I'm trying to intercept all methods from the classes that extends certain class. I defined my aspect like this:

@Aspect
public class LogMethodAspect implements Serializable {

@Around("execution(* org.custom.es.GenericComponent+.*(..))") {
    public Object measureExecutionTime(ProceedingJoinPoint joinPoint) throws Throwable {
        long startTime = System.currentTimeMillis();
        Object result = joinPoint.proceed();
        System.out.println(joinPoint+ " -> " + (System.currentTimeMillis() - startTime) + " ms");
        return result;
    }
}

The aspect declaration in Spring:

<bean id="logMethodAspect" class="org.custom.es.LogMethodAspect" />

The Superclass

package org.custom.es;

public abstract class GenericComponent {

    //some code
}

An exampe of a subclass

package org.implementation.es;

public class InformationComponent extends GenericComponent {

    //some code
}

However, it never intercepts it. What I figured is:

  • The bean is being created.
  • If I change the value of the @Around annotation to an annotated-based declaration (like @annotation(myanotation), and use that annotation on a subclass, it works properly, but I can't go an annotate every subclass (more could be created in the future).

I tried using the option given in this question How to specify single pointcut for all classes that extend a specific class but it doesn't work either.

Thanks for any help

Mauricio
  • 186
  • 1
  • 11
  • From the details shared it is not clear if you have enabled aspectj autoproxy ` ` , could you please confirm the same ? – R.G Mar 27 '20 at 02:17
  • R.G is right. Furthermore, we also cannot know if your target classes are correctly declared as beans or components and picked up by component scan because both variants depend on your configuration. Spring AOP only works for Spring beans, not for simple POJOs. What you said about that it works with the marker annotation solution suggests that you configured the beans correctly and the auto-proxy setting might be missing. Your pointcut looks okay, by the way. – kriegaex Mar 27 '20 at 04:22

0 Answers0