当前位置:网站首页>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注解(待生产验证)。
边栏推荐
猜你喜欢

Upload markdown documents to blog garden

2022-08-01 第八组 曹雨 泛型 枚举

Kubernetes第零篇:认识kubernetes

Based on php online examination management system acquisition (php graduation design)

Based on php film and television information website management system acquisition (php graduation design)

_ _ determinant of a matrix is higher algebra eigenvalue of the product, the characteristic value of matrix trace is combined

求解多元多次方程解的个数

ImportError: `save_weights` requires h5py.问题解决

基于php酒店在线预定管理系统获取(php毕业设计)

宝塔应用使用心得
随机推荐
365 days challenge LeetCode1000 questions - Day 046 Generate a string with odd number of each character + add two numbers + valid parentheses
Based on php online examination management system acquisition (php graduation design)
[Mobile Web] Mobile terminal adaptation
shell编程规范与变量
微软校园大使喊你来秋招啦!
今年的很美味
[ASM] Bytecode Operation MethodWriter
罗克韦尔AB PLC RSLogix5000中的比较指令使用方法介绍
Shell programming conditional statement
【建议收藏】ヾ(^▽^*)))全网最全输入输出格式符整理
企业公众号文章写作方向:如何写出读者认可的优质内容
Spark cluster construction
不卷了!入职字节跳动一周就果断跑了。
Getting Started Database Days4
The difference between groupByKey and reduceBykey
基于php旅游网站管理系统获取(php毕业设计)
今日睡眠质量记录74分
基于php在线考试管理系统获取(php毕业设计)
Based on php online music website management system acquisition (php graduation design)
Implementation principle of VGUgarbage collector (garbage collector)