Spring源码窥探之:ImportSelector
2024-08-22 13:44:04
1. 编写实现ImportSelector的类
/**
* @author 70KG
* @Title: SelectImportBean
* @Description:
* @date 2018/7/22下午4:59
* @From www.nmyswls.com
*/
public class SelectImportBean implements ImportSelector { @Override
public String[] selectImports(AnnotationMetadata annotationMetadata) {
return new String[]{"com.nmys.story.springCore.springioc.importBean.Fish","com.nmys.story.springCore.springioc.importBean.Pig"};
} }
2. 编写配置类
/**
* @author 70KG
* @Title: Config
* @Description:
* @date 2018/7/22下午4:41
* @From www.nmyswls.com
*/
@Configuration
// 会自动向容器中加入实例化后的bean,默认的id是bean的全类名
@Import(value = {Dog.class, Cat.class, SelectImportBean.class})
public class ImportBeanConfig { }
3. 测试类
public class Test01 { @Test
public void test() { // IOC容器创建(容器类似一个Map)
AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(ImportBeanConfig.class); System.out.println("IOC容器初始化完成。。。。。"); // 从容器中获取所有的bean的ID
String[] definitionNames = ac.getBeanDefinitionNames(); for (String name : definitionNames) {
System.out.println("------->" + name);
} }
}
4. 测试结果
IOC容器初始化完成。。。。。
------->org.springframework.context.annotation.internalConfigurationAnnotationProcessor
------->org.springframework.context.annotation.internalAutowiredAnnotationProcessor
------->org.springframework.context.annotation.internalRequiredAnnotationProcessor
------->org.springframework.context.annotation.internalCommonAnnotationProcessor
------->org.springframework.context.event.internalEventListenerProcessor
------->org.springframework.context.event.internalEventListenerFactory
------->importBeanConfig
------->com.nmys.story.springCore.springioc.importBean.Dog
------->com.nmys.story.springCore.springioc.importBean.Cat
------->com.nmys.story.springCore.springioc.importBean.Fish
------->com.nmys.story.springCore.springioc.importBean.Pig
5. ImportSelector用于向容器中导入指定的类。
最新文章
- MHA高可用+VIP 集群故障转移(已测试成功)
- Linux系统上安装mysql数据库
- ubuntu亮度无法自动调节终极解决方案
- Android Log日志文件的分析、查看
- .net简单页面后台绑定下拉框,按钮,分页 前台aspx页面
- 【拓扑排序】【HDU3231】【Box Relations】
- jQuery tmpl index
- 前端开发在IOS端遇到的一个诡异问题(Delegate 失效)
- IE console.log 调试状态
- ThreadPoolExecutor参数
- 2015年蓝桥杯省赛A组c++第5题(回溯算法填空)
- 2018/04/25 PHP7的编译安装
- Java之24种设计模式-UML-模型图解读
- Web前端js下载流文件
- stdafx
- R语言字符串替换
- 使用 requests 发送 POST 请求
- UVA 10462 Is There A Second Way Left?(次小生成树&;Prim&;Kruskal)题解
- Tomcat服务器启动失败:Could not publish server configuration for Tomcat v8.0 Server at localhost. Multiple Contexts have a path of
- JS计算字符长度、字节数 -- 转