Why doesn’t my aspect get applied? Notice the aspect is in the same package as the aspected method’s class so it should work fine
import lib.*;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class MyClass {
public static void main(String args[]) {
var context = new AnnotationConfigApplicationContext(Config.class);
SomeBean someBean = context.getBean(SomeBean.class);
String returnedVal = someBean.someAspectedMethod();
System.out.println(returnedVal);
System.out.println(returnedVal.getClass());
}
}
package lib;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
@Configuration
@ComponentScan
@EnableAspectJAutoProxy
public class Config {
}
package lib;
import org.springframework.stereotype.Component;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
@Aspect
@Component
public class SomeAspect {
@Around("execution(String lib.SomeBean.someAspectedMethod())")
public Object methodAspect(ProceedingJoinPoint joinPoint) {
System.out.println("Running methodAspect()..."); // never gets printed
Object object = joinPoint.proceed();
return object;
}
}
package lib;
import org.springframework.stereotype.Component;
@Component
public class SomeBean {
public String someAspectedMethod() {
return "some string";
}
}
Console output:
some string
class java.lang.String