zoukankan      html  css  js  c++  java
  • 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);
        }
    }
  • 相关阅读:
    C struct 中字节对齐问题(转)
    蚁群算法,PSO算法以及两种算法可以融合的几种方法
    遗传及蚁群算法
    ListBox FAQ常用问题
    关于C#中ListBox控件重绘Item项
    创业艰难,问题多多
    asp.net客户端脚本验证小技巧
    防止ASP.NET按钮多次提交的办法
    鼠标点到文本框时的提示信息
    枚举的转换
  • 原文地址:https://www.cnblogs.com/snow-man/p/11158743.html
Copyright © 2011-2022 走看看