当前位置:网站首页>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
边栏推荐
- 容联云发送短信验证码
- 「津津乐道播客」#397 厂长来了:怎样用科技给法律赋能?
- 【游戏建模模型制作全流程】使用ZBrush制作骷髅王
- MySQL的安装与卸载
- C5750X7R2E105K230KA(电容器)MSP430F5249IRGCR微控制器资料
- [Paper Notes KDD2021] MixGCF: An Improved Training Method for Graph Neural Network-based Recommender Systems
- VC bmp文件总结
- [QNX Hypervisor 2.2用户手册]10.4 vdev hpet
- If you can't get your heart, use "distributed lock" to lock your people
- @Import注解的作用以及如何使用
猜你喜欢
Pytest learning - fixtures
【字符串函数内功修炼】strncpy + strncat + strncmp(二)
[Cultivation of internal skills of string functions] strncpy + strncat + strncmp (2)
仪表板展示 | DataEase看中国:数据呈现中国资本市场
2022年全网最全接口自动化测试框架搭建,没有之一
FinClip崁入式搭建生态平台,降低合作门槛
3年,从3K涨薪到20k?真是麻雀啄了牛屁股 — 雀食牛逼呀
为何越来越多人选择进入软件测试行业?深度剖析软件测试的优势...
Day118.尚医通:订单列表、详情、支付
web3.js
随机推荐
【软件测试】常用ADB命令
MySQL的安装与卸载
web3.js
d枚举生成位
If you can't get your heart, use "distributed lock" to lock your people
truffle
temp7777
基于内容的图像检索系统设计与实现--颜色信息--纹理信息--形状信息--PHASH--SHFT特征点的综合检测项目,包含简易版与完整版的源码及数据!
Kernel函数解析之kernel_restart
4-《PyTorch深度学习实践》-反向传播
赶紧进来!!!教你C语言实现扫雷小游戏(文章最后有源码!!!)
Linux系统重启和停止Mysql服务教程
功耗控制之DVFS介绍
956. 最高的广告牌
PAN3020 Sub-1G无线收发芯片
[Cultivation of internal skills of string functions] strncpy + strncat + strncmp (2)
2022/8/3
enumerate()函数
Literature reading ten - Detect Rumors on Twitter by Promoting Information Campaigns with Generative Adversarial Learn
养殖虚拟仿真软件提供高沉浸式的虚拟场景互动操作体验学习