• spring-boot autoConfiguration


    一, 第一个待注入类

    public class CacheService {
    }
    
    public class LoggerService {
    }
    

      

    方法一, 实现接口ImportSelectort

    public class CacheImportSelector implements ImportSelector {
        @Override
        public String[] selectImports(AnnotationMetadata annotationMetadata) {
            return new String[]{CacheService.class.getName()};
        }
    }

    方法二, 实现接口ImportBeanDefinitionRegistrar,

    public class LoggerServiceSelector implements ImportBeanDefinitionRegistrar {
        @Override
        public void registerBeanDefinitions(AnnotationMetadata annotationMetadata, BeanDefinitionRegistry beanDefinitionRegistry) {
            RootBeanDefinition rootBeanDefinition = new RootBeanDefinition(LoggerService.class);
            String strBeanname = StringUtils.uncapitalize(LoggerService.class.getName());
            beanDefinitionRegistry.registerBeanDefinition(strBeanname, rootBeanDefinition);
        }
    }

    自定义Enable注解, 将CacheService, LoggerService加载到Spring-boot项目中

    @Target({ElementType.TYPE})
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    @Component
    @Import({CacheImportSelector.class, LoggerServiceSelector.class})
    public @interface EnableCacheService {
    }
    
    
    
    //启动Spring-boot
    
    @EnableCacheService
    @SpringBootApplication
    public class SpringBootDemoApplication {
        public static void main(String[] args) {
            ConfigurableApplicationContext context = SpringApplication.run(SpringBootDemoApplication.class, args);
            CacheService cacheService = context.getBean(CacheService.class);
            System.out.println(cacheService.toString());
            LoggerService loggerService = context.getBean(LoggerService.class);
            System.out.println(loggerService);
        }
    }
  • 相关阅读:
    python生成器和使用gevent操作协程
    python飞机大战
    python控制鼠标键盘+监听键盘
    python生成彩色二维码
    springboot+springcloud+maven相关父子项目创建
    c++求最大公约数
    java8 LocalDateTime
    nginx 代理wss
    vue 全局使用axios
    Vue3.0核心源码解读| 组件渲染:vnode 到真实 DOM 是如何转变的?
  • 原文地址:https://www.cnblogs.com/snow-man/p/11158743.html
Copyright © 2020-2023  润新知