当前位置:网站首页>@Import注解的作用以及如何使用
@Import注解的作用以及如何使用
2022-08-04 23:08:00 【步尔斯特】
@Import注解提供了@Bean注解的功能,在Spring 4.2之后,@Import可以直接指定实体类,加载这个类定义到context中。
@Import的优先于本身的的类定义加载。
这里直讲@Import比较常用的用法
直接指定类
新建类ImportTest1
public class ImportTest1 {
public void run() {
System.out.println("======ImportTest1=======");
}
}
新建配置类ImportTest2,并标注注解@Import,注解内部指定想要加载的类,这里使其交给Spring 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方法,指定想要加载的全类名。
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));
}
}
自定义注解整合ImportSelector接口
自定义注解
@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
边栏推荐
猜你喜欢
MySQL基础篇【子查询】
【内存操作函数内功修炼】memcpy + memmove + memcmp + memset(四)
如何根据地址获取函数名
使用OpenCV实现一个文档自动扫描仪
【字符串函数内功修炼】strncpy + strncat + strncmp(二)
未上市就“一举成名”,空间媲美途昂,安全、舒适一个不落
从“草原牛”到“数字牛”:蒙牛的数字化转型之道
基于内容的图像检索系统设计与实现--颜色信息--纹理信息--形状信息--PHASH--SHFT特征点的综合检测项目,包含简易版与完整版的源码及数据!
轮播图动态渲染
If you can't get your heart, use "distributed lock" to lock your people
随机推荐
3D建模师为了让甲方爸爸过稿,还可以这么做,就是在赚血汗钱啊
【字符串函数内功修炼】strcpy + strcat + strcmp(一)
[Paper Notes KDD2021] MixGCF: An Improved Training Method for Graph Neural Network-based Recommender Systems
temp7777
Go 编程语言(简介)
【3D建模制作技巧分享】ZBrush纹理贴图怎么导入
Service Mesh落地路径
直播带货为农产品开拓销售渠道
truffle
Both synchronized and ReentrantLock are smooth, because they are reentrant locks, and a thread will not deadlock if it takes the lock multiple times. We need reentrant locks
2022年全网最全接口自动化测试框架搭建,没有之一
测试技术:关于上下文驱动测试的总结
Redisson
The market value of 360 has evaporated by 390 billion in four years. Can government and enterprise security save lives?
对“为什么一些程序员很傲慢”的解读
MySQL基础篇【聚合函数】
Acwing3593. 统计单词
Pytest学习-Fixture
One trick to cure pycharm DEBUG error UnicodeDecodeError: 'utf-8' codec can't decode
npm基本操作及命令详解