当前位置:网站首页>1. @Component注解的原理剖析
1. @Component注解的原理剖析
2022-08-01 21:55:00 【virgil.wang】
1. 动机与目标
- 通过Java注解方式配置
IOC
容器随着Springboot
的使用已经逐渐形成了风靡,在日常开发中,启动时可能报错提示找不到beanDifinition,对于此类问题,了解@Component
的机制对于排查问题很有帮助; - 为了应对面试;
2. 测试用例
spring framework version: 5.1.x
配置类
/** * @author: virgil * @date: 2022/7/24 12:04 AM **/
@ComponentScan(basePackages = "edu.analysis.spring.component")
@Configuration
public class MainConfig {
public MainConfig() {
System.out.println("call MainConfig no-arg construct...");
}
}
被Component注解修饰的类
@Component
public class TargetComponent {
public TargetComponent() {
System.out.println("call TargetComponent no-arg construct...");
}
public void sayHello() {
System.out.println("hello...");
}
}
为了能够说明@ComponentScan的机制和@Component、@Service、@Repository的关系,再补充两个测试类。
被@Service修饰的类
/** * @author: virgil * @date: 2022/7/25 11:31 PM **/
@Service
public class TargetService {
public TargetService() {
System.out.println("call TargetService no-args construct");
}
public void sayService() {
System.out.println("I am targetService ...");
}
}
被@Repository修饰的类
/** * @author: virgil * @date: 2022/7/25 11:32 PM **/
@Repository
public class TargetRepository {
public TargetRepository() {
System.out.println("call TargetRepository no-args constructs");
}
public void sayRepository() {
System.out.println("I am repository");
}
}
驱动类(测试类)
/** * @author: virgil * @date: 2022/7/24 12:12 AM **/
public class MainTest {
@Test
public void componentTest() {
ApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfig.class);
TargetComponent targetComponent = applicationContext.getBean("targetComponent", TargetComponent.class);
targetComponent.sayHello();
}
}
3. 图示
ACA: AnnotationConfigApplicationContext
4. 使用心得
- 对于某些第三方工具包里面的类可能会被
@Component
注解修饰,在使用这些类的时候,由于项目的包扫描@ComponentScan
一般不会配置扫描第三方工具包,也不会这么做,因为无法评估工具包下的bean
都是安全且合乎要求的。这个时候可以通过写一个被@Component
修饰的类,里面可以写一个被@bean
修饰的方法,return
新的工具类,还有一个方式在配置类或者组件类上使用@Import
注解(待生产验证)。
边栏推荐
- ModuleNotFoundError: No module named ‘yaml‘
- HCIP---Multiple Spanning Tree Protocol related knowledge points
- Spark cluster construction
- Spark shuffle tuning
- shell programming conventions and variables
- Spark practice questions + answers
- ARFoundation Getting Started Tutorial U2-AR Scene Screenshot Screenshot
- [@synthesize in Objective-C]
- 线程池分析
- 游戏元宇宙发展趋势展望分析
猜你喜欢
ModuleNotFoundError: No module named ‘yaml‘
一种灵活的智能合约协作方式
小程序中的多表联合查询
Delicious this year
blender3.2.1 unit setting
Analysis of the development trend of game metaverse
小程序--分包
用户体验 | 如何度量用户体验?
_ _ determinant of a matrix is higher algebra eigenvalue of the product, the characteristic value of matrix trace is combined
高等代数_证明_矩阵的行列式为特征值之积, 矩阵的迹为特征值之和
随机推荐
selenium无头,防检测
[@synthesize in Objective-C]
Safe fifth after-school exercise
[ASM] Bytecode Operation MethodWriter
第3讲:MySQL数据库中常见的几种表字段数据类型
【建议收藏】ヾ(^▽^*)))全网最全输入输出格式符整理
Homework 8.1 Orphans and Zombies
Small program -- subcontracting
Flink集群搭建
Unity Shader 常规光照模型代码整理
基于php在线学习平台管理系统获取(php毕业设计)
No more rolls!After joining ByteDance for a week, he ran decisively.
ModuleNotFoundError: No module named 'yaml'
ImportError: `save_weights` requires h5py.问题解决
Dichotomy Medium LeetCode6133. Maximum Number of Groups
【C语言实现】求两个整数的较大值
The difference between groupByKey and reduceBykey
入门数据库Days4
MySQL相关知识
Spark练习题+答案