当前位置:网站首页>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
边栏推荐
- Nacos配置中心之客户端长轮询
- I was rejected by the leader for a salary increase, and my anger rose by 9.5K after switching jobs. This is my mental journey
- NebulaGraph v3.2.0 Release Note,对查询最短路径的性能等多处优化
- 仪表板展示 | DataEase看中国:数据呈现中国资本市场
- 堪称奔驰“理财产品”,空间媲美宝马X5,采用了非常运动的外观
- 赶紧进来!!!教你C语言实现扫雷小游戏(文章最后有源码!!!)
- 【3D建模制作技巧分享】ZBrush如何设置笔刷快捷键
- @Import注解的作用以及如何使用
- 基于内容的图像检索系统设计与实现--颜色信息--纹理信息--形状信息--PHASH--SHFT特征点的综合检测项目,包含简易版与完整版的源码及数据!
- 【3D建模制作技巧分享】如何使用ZBrush导出效果图
猜你喜欢
随机推荐
赶紧进来!!!教你C语言实现扫雷小游戏(文章最后有源码!!!)
请你说一下final关键字以及static关键字
@Async注解的作用以及如何实现异步监听机制
Shell expect real cases
堪称奔驰“理财产品”,空间媲美宝马X5,采用了非常运动的外观
社区分享|腾讯海外游戏基于JumpServer构建游戏安全运营能力
ffplay视频播放原理分析
Nuclei(二)进阶——深入理解workflows、Matchers和Extractors
【3D建模制作技巧分享】ZBrush模型如何添加不同材质
PAN3020 Sub-1G无线收发芯片
Linear DP (bottom)
Kernel函数解析之kernel_restart
【手撕AHB-APB Bridge】~ AMBA总线 之 AHB
加解密在线工具和进制转化在线工具
【游戏建模模型制作全流程】ZBrush蜥蜴模型雕刻教程
【3D建模制作技巧分享】ZBrush如何重新拓扑
enumerate()函数
特征工程资料汇总
基于内容的图像检索系统设计与实现--颜色信息--纹理信息--形状信息--PHASH--SHFT特征点的综合检测项目,包含简易版与完整版的源码及数据!
The Go Programming Language (Introduction)