当前位置:网站首页>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
边栏推荐
- 对“为什么一些程序员很傲慢”的解读
- 【字符串函数内功修炼】strcpy + strcat + strcmp(一)
- 基于内容的图像检索系统设计与实现--颜色信息--纹理信息--形状信息--PHASH--SHFT特征点的综合检测项目,包含简易版与完整版的源码及数据!
- d枚举生成位
- Service Mesh落地路径
- 逆序对的数量
- 【字符串函数内功修炼】strlen + strstr + strtok + strerror(三)
- Will we still need browsers in the future?(feat. Maple words Maple language)
- 话题 | 雾计算和边缘计算有什么区别?
- Service Mesh落地路径
猜你喜欢
随机推荐
MySQL的安装与卸载
d枚举生成位
Linear DP (bottom)
【3D建模制作技巧分享】zbrush贴图映射小技巧
Based on the results of the facts
地面高度检测/平面提取与检测(Fast Plane Extraction in Organized Point Clouds Using Agglomerative Hierarchical Clu)
容联云发送短信验证码
kernel hung_task死锁检测机制原理实现
吐槽 | 参加IT培训的正确姿势
The Go Programming Language (Introduction)
xss总结
未上市就“一举成名”,空间媲美途昂,安全、舒适一个不落
web3.js
文献阅读十——Detect Rumors on Twitter by Promoting Information Campaigns with Generative Adversarial Learn
[Cultivation of internal skills of string functions] strlen + strstr + strtok + strerror (3)
typeScript-部分应用函数
一点点读懂regulator(二)
年薪50W+的测试工程师都在用这个:Jmeter 脚本开发之——扩展函数
Service Mesh落地路径
被领导拒绝涨薪申请,跳槽后怒涨9.5K,这是我的心路历程
![[Cultivation of internal skills of string functions] strlen + strstr + strtok + strerror (3)](/img/96/946bbef52bd017ac6142c6b7485a86.png)








