How do I use @Aspect in the native-image?

The annotation:

@Target({ElementType.TYPE, ElementType.METHOD, ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
}

The aspect:

@Aspect
@Component
public class MyAspect {
    private static final Logger LOGGER = LoggerFactory.getLogger(MyAspect.class);

    @Pointcut(value = "@annotation(cn.wangliang181230.studynativeimage.MyAnnotation)")
    public void pointCut() {
    }

    @Around("pointCut()")
    public Object beforePointCut(ProceedingJoinPoint joinPoint) throws Throwable {
        MethodSignature signature = (MethodSignature)joinPoint.getSignature();
        Method method = signature.getMethod();

        LOGGER.info("aspect: {}", method.getName());

        return joinPoint.proceed();
    }

}

The controller:

@RestController
public class TestController2 {
    private static final Logger LOGGER = LoggerFactory.getLogger(TestController2.class);

    @MyAnnotation
    @GetMapping("/test")
    public String test() {
        LOGGER.info("test controller");
        return "test";
    }
}

The application:

@SpringBootApplication
@EnableAspectJAutoProxy
public class StudyNativeImageBySpringBoot3Application {

    public static void main(String[] args) {
        SpringApplication.run(StudyNativeImageBySpringBoot3Application.class, args);
    }
}

mvn clean package -Pnative -e

Then start application.exe and visit http://localhost:8080/test, but the aspect did not take effect in the native-image.

Comment From: wilkinsona

We have a smoke test for @Aspect that works. One difference here is your use of an annotation. It may be that there there is insufficient reflection metadata available in the native image for the annotation's presence to be detected by the AOP infrastructure.

Support for @Aspect is a Spring Framework feature so we'll transfer this issue to the Framework team so that they can investigate.

Comment From: wangliang181230

When I change @annotation(cn.wangliang181230.studynativeimage.MyAnnotation) to execution(* cn.wangliang181230.studynativeimage.TestController2.*(..)), and hints.reflection().registerType(TestController2.class);. It still doesn't work.

图片

Comment From: sdeleuze

Aspect could work for simple use cases like in the smoke test, but there are not fully support on native yet, this is already tracked by #28711, so I will close this one as a duplicate

Comment From: wangliang181230

OK,Thank you.