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