• 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;
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    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);
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • spring.cloud.kubernetes.example.account.AccountService 主类
    • spring.cloud.kubernetes.example.account 主包
    • example-service-account 模块名
    • 其他的三个为代码生成的路径

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

  • 相关阅读:
    excel使用小笔记
    1.15.C++项目:仿muduo库实现并发服务器之HttpRequest和HttpResponse模块的设计
    leetcode 241Different Ways to Add Parentheses 为运算表达式设计优先级 解题记录
    Java之通配符
    Redis学习笔记( 入门篇)
    Spark之UDF失效
    Python深度学习032:conda操作虚拟环境env的全部命令
    Istio EnvoyFilter+Lua 简单实现动态路由转发
    阿里云产品试用系列-云服务器 ECS
    【C++】C++多线程库的使用
  • 原文地址:https://blog.csdn.net/u014087707/article/details/128201789