当前位置:网站首页>The role of @ Import annotations as well as how to use
The role of @ Import annotations as well as how to use
2022-08-04 23:19:00 【Step Ulster】
@Import注解提供了@Bean注解的功能,在Spring 4.2之后,@Import可以直接指定实体类,加载这个类定义到context中.
@Import的优先于本身的的类定义加载.
Straight talk [email protected]比较常用的用法
直接指定类
新建类ImportTest1
public class ImportTest1 {
public void run() {
System.out.println("======ImportTest1=======");
}
}
新建配置类ImportTest2,并标注注解@Import,The annotation internally specifies the class to be loaded,Here it is handed overSpring IOC容器管理.
@Configuration
@Import(ImportTest1.class)
public class ImportTest2 {
@Bean
public ImportTest2 importTest2(){
return new ImportTest2();
}
}
通过@Autowired就可以直接使用这个类了.
@RestController
public class ProducerController {
@Autowired
private ImportTest1 importTest1;
@GetMapping("/imported")
public String imported() {
importTest1.run();
return String.valueOf(ThreadLocalRandom.current().nextInt(100));
}
}
实现ImportSelector接口
新建类ImportTest1
public class ImportTest1 {
public void run() {
System.out.println("======ImportTest1=======");
}
}
实现ImportSelector接口,实现selectImports方法,Specify the full class name you want to load.
public class ImportSelectorTest implements ImportSelector {
@Override
public String[] selectImports( AnnotationMetadata annotationMetadata) {
return new String[]{
"com.issa.producer.csdn.imported.ImportTest1"};
}
}
在配置类中使用@Import注解指定ImportSelector的实现类.
@Configuration
@Import(ImportSelectorTest.class)
public class ImportTest2 {
public void run() {
System.out.println("======ImportTest2=======");
}
}
通过@Autowired就可以直接使用这个类了.
@RestController
public class ProducerController {
@Autowired
private ImportTest1 importTest1;
@GetMapping("/imported")
public String imported() {
importTest1.run();
return String.valueOf(ThreadLocalRandom.current().nextInt(100));
}
}
Custom annotation integrationImportSelector接口
自定义注解
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Target(ElementType.TYPE)
@Import(ImportTest.class)
public @interface EnableSelector {
@AliasFor("name")
String value() default "";
@AliasFor("value")
String name() default "";
}
注解处理
public class ImportTest implements ImportSelector {
@Override
public String[] selectImports(AnnotationMetadata annotationMetadata) {
Map<String, Object> map = annotationMetadata.getAnnotationAttributes(EnableSelector.class.getName(), true);
assert map != null;
String name = (String) map.get("name");
if (Objects.equals(name, "ImportTest1")) {
return new String[]{
"com.issa.producer.csdn.imported.ImportTest1"};
}
return new String[0];
}
}
注解使用
@Configuration
@EnableSelector("ImportTest1")
public class ImportTest2 {
public void run() {
System.out.println("======ImportTest2=======");
}
}
参考:https://spring.io、https://cloud.tencent.com/developer/article/1811376
边栏推荐
- [Cultivation of internal skills of memory operation functions] memcpy + memmove + memcmp + memset (4)
- TypeScript - the use of closure functions
- Service Mesh落地路径
- 小黑leetcode冲浪:94. 二叉树的中序遍历
- Qt中的常用控件
- 一点点读懂Thremal(二)
- [QNX Hypervisor 2.2用户手册]10.6 vdev mc146818
- 应用联合、体系化推进。集团型化工企业数字化转型路径
- 2022/8/4 树上差分+线段树
- 测试技术:关于上下文驱动测试的总结
猜你喜欢

【3D建模制作技巧分享】zbrush贴图映射小技巧

【内存操作函数内功修炼】memcpy + memmove + memcmp + memset(四)

线上虚拟展馆展示具有哪些优势

3D建模师为了让甲方爸爸过稿,还可以这么做,就是在赚血汗钱啊

【3D建模制作技巧分享】ZBrush纹理贴图怎么导入

功耗控制之DVFS介绍
![[Cultivation of internal skills of string functions] strlen + strstr + strtok + strerror (3)](/img/96/946bbef52bd017ac6142c6b7485a86.png)
[Cultivation of internal skills of string functions] strlen + strstr + strtok + strerror (3)

956. 最高的广告牌

亿流量大考(3):不加机器,如何抗住每天百亿级高并发流量?

【游戏建模模型制作全流程】使用ZBrush制作骷髅王
随机推荐
C5750X7R2E105K230KA(电容器)MSP430F5249IRGCR微控制器资料
【字符串函数内功修炼】strlen + strstr + strtok + strerror(三)
[QNX Hypervisor 2.2用户手册]10.5 vdev ioapic
node中package解析、npm 命令行npm详解,node中的common模块化,npm、nrm两种方式查看源和切换镜像
小黑leetcode之旅:95. 至少有 K 个重复字符的最长子串
xss总结
SRv6网络的安全解决方案
typeScript-闭包函数的使用
小黑leetcode冲浪:94. 二叉树的中序遍历
中国的顶级黑客在国际上是一个什么样的水平?
仪表板展示 | DataEase看中国:数据呈现中国资本市场
Shell expect real cases
The Go Programming Language (Introduction)
话题 | 雾计算和边缘计算有什么区别?
【3D建模制作技巧分享】ZBrush如何使用Z球
go语言的time包介绍
注解@EnableAutoConfiguration的作用以及如何使用
养殖虚拟仿真软件提供高沉浸式的虚拟场景互动操作体验学习
未上市就“一举成名”,空间媲美途昂,安全、舒适一个不落
【游戏建模模型制作全流程】使用ZBrush制作骷髅王