当前位置:网站首页>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
边栏推荐
猜你喜欢
随机推荐
对“为什么一些程序员很傲慢”的解读
PID Controller Improvement Notes No. 7: Improve the anti-overshoot setting of the PID controller
Since a new byte of 20K came out, I have seen what the ceiling is
【字符串函数内功修炼】strcpy + strcat + strcmp(一)
【转载】kill掉垃圾进程(在资源管理器占用的情况下)
[QNX Hypervisor 2.2用户手册]10.5 vdev ioapic
【3D建模制作技巧分享】如何使用ZBrush导出效果图
深度|医疗行业勒索病毒防治解决方案
【内存操作函数内功修炼】memcpy + memmove + memcmp + memset(四)
3D建模师为了让甲方爸爸过稿,还可以这么做,就是在赚血汗钱啊
话题 | 雾计算和边缘计算有什么区别?
Kernel函数解析之kernel_restart
PAN3020 Sub-1G无线收发芯片
MySQL基础篇【子查询】
[QNX Hypervisor 2.2用户手册]10.4 vdev hpet
SRv6网络的安全解决方案
使用代理对象执行实现类目标方法异常
小黑leetcode冲浪:94. 二叉树的中序遍历
[Cultivation of internal skills of string functions] strcpy + strcat + strcmp (1)
OPENCV学习DAY8









