Spring 6.x 的 AoT 相关支持的注解

RegisterReflectionForBinding

RegisterReflectionForBinding在构建期间.使用RegisterReflectionForBindingProcessor 来注册相关类的元数据. 详情看org.springframework.aot.hint.BindingReflectionHintsRegistrar 类的处理.
不仅会处理当前类. 还会处理其方法和方法参数和返回值的相关类. 仅仅处理与反射相关的操作. 一些动态代理. 序列化. 资源文件.等处理不了.

ImportRuntimeHints

ImportRuntimeHints 可控.更加灵活的处理相关类的元数据. 不仅可以处理反射. 资源. 序列化也可以处理. 指定相关RuntimeHintsRegistrar的子类. for-example:

@SpringBootApplication
@ImportRuntimeHints(AccountService.AccountRuntimeHints.class)
public class AccountService {

    public static void main(String[] args) {

        SpringApplication.run(AccountService.class, args);
    }
   static class AccountRuntimeHints implements RuntimeHintsRegistrar {

        @Override
        public void registerHints(RuntimeHints hints, ClassLoader classLoader) {
            ReflectionHints ref = hints.reflection();
            try {
                for (Class<?> c : getK8sClasses()) {
                    ref.registerType(c, MemberCategory.values());
                }
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }

        private Collection<? extends Class<?>> getK8sClasses() {
            Set<Class<?>> classes = new HashSet<>();
            return classes;
        }
    }
}

Reflective

标注哪些类是需要被反射的. 会自动处理

使用BeanFactoryInitializationAotProcessor

  • 可以在build阶段动态注册某些bean, 例如对Feign的支持. 就是使用这种方式在构建阶段生成Feign的实现类动态注册到spring context中. 参考 注册Feign 实例相关源码:org.springframework.cloud.openfeign.aot.FeignClientBeanFactoryInitializationAotProcessor
使用aot.factories 注册BeanFactoryInitializationAotProcessor

org.springframework.beans.factory.aot.BeanFactoryInitializationAotProcessor=

如何debug Spring的AoT的编译阶段.

   public static void main(String[] args) throws Exception {
        args = new String[]{"spring.cloud.kubernetes.example.account.AccountService",
                "/Users/apple/opensource/spring-cloud-kubernetes-microservices-toolkit/example-service-account/build/generated/aotSources",
                "/Users/apple/opensource/spring-cloud-kubernetes-microservices-toolkit/example-service-account/build/generated/aotResources",
                "/Users/apple/opensource/spring-cloud-kubernetes-microservices-toolkit/example-service-account/build/generated/aotClasses",
                "spring.cloud.kubernetes.example.account",
                "example-service-account"};
        SpringApplicationAotProcessor.main(args);
    }
  • spring.cloud.kubernetes.example.account.AccountService 主类
  • spring.cloud.kubernetes.example.account 主包
  • example-service-account 模块名
  • 其他的三个为代码生成的路径

一个spring-cloud-kubernetes的本地debug的开发工具包.并支持native

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值