一, 第一个待注入类

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);
}
}

最新文章

  1. EasyUI 中点击取消按钮关闭Dialog(对话框窗口)
  2. 如何同时打开两个excel
  3. YACC和BISON学习心得
  4. HDU 1257
  5. HTTP 错误 500.22 - Internal Server Error
  6. 在HTML中调用iOS
  7. 从零开始学JAVA(03)-用Eclipse生成HelloWorld的Jar文件(简单不带包)
  8. javascript 实现htmlEncode htmlDecode
  9. poj 3304 Segments(计算几何基础)
  10. poj3294 Life Forms(后缀数组)
  11. [详细实例]MicroPython拼插编程实战:DIY一台会思考的壁障车
  12. Cola Cloud 基于 Spring Boot, Spring Cloud 构建微服务架构企业级开发平台
  13. PHP+Hadoop+Hive+Thrift+Mysql实现数据统计分析
  14. 支付宝APP支付,提示代码 ALIN10070
  15. PHP进阶。
  16. struts2 正确配置通配符方式访问,报错解决
  17. Ubuntu 13.10 安装 Oracle11gR2
  18. js的字符串charAt()方法
  19. /proc/sys 子目录的作用
  20. SpringAOP简介

热门文章

  1. seo成功案例的背后秘密
  2. 热门话题,2020年了,web前端还好找工作吗?
  3. 由浅入深理解 IOC 和 DI
  4. PHP - 读取EXCEL内容 存入数据库
  5. 总结删除文件或文件夹的7种方法-JAVA IO基础总结第4篇
  6. .net core 返回业务错误(不抛异常)
  7. Kubernetes K8S之资源控制器Daemonset详解
  8. 前端防止xxs注入
  9. java的方法详解和总结
  10. POJ - 3851-Wormholes(SPFA判负环)