1. Spring AOP 与 Native Image 的兼容性处理
Spring AOP 与 GraalVM Native Image 的兼容性如何处理,Spring 如何为 AOP 生成与收集代理配置?
- Spring AOP 动态代理在 Native 中的限制
- Spring AOT 对代理的预生成
- 反射/代理配置的自动收集
Spring AOP 依赖动态代理(JDK 动态代理或 CGLIB),而 Native Image 的 AOT 编译无法在运行时动态生成代理类,因此需要 Spring AOT 在构建期预生成并注册代理类,同时把相关接口/类写入 proxy-config.json 与 reflect-config.json。Spring Framework 6 / Spring Boot 3 提供了 GraalVM AOT 支持(Spring AOT 引擎),会在构建期解析 @Bean、AOP 切面等,生成 native 需要的代理与反射元数据。处理时需确保 AOP 依赖的类、接口、切面被正确纳入配置,并避免在运行时动态创建代理实例。
兼容的关键是"把动态代理提前到构建期做"。Spring 的 AOT 引擎从 @Configuration 出发,静态化计算 Bean 定义与代理,配合 reachability metadata 满足 Native 的封闭世界假设。