当前位置:网站首页>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
注解(待生产验证)。
边栏推荐
猜你喜欢
[Niu Ke brush questions-SQL big factory interview questions] NO4. Travel scene (a taxi)
FusionGAN:A generative adversarial network for infrared and visible image fusion article study notes
19 Lectures on Disassembly of Multi-merchant Mall System Functions - Invoice Management on the Platform
Recycling rental system 100% open source without encryption Mall + recycling + rental
Upload markdown documents to blog garden
Today's sleep quality record 74 points
NFT的10种实际用途(NFT系统开发)
shell specification and variables
Getting Started Database Days4
小程序中的多表联合查询
随机推荐
0DFS Medium LeetCode6134. Find the closest node to the given two nodes
ImportError: `save_weights` requires h5py. Problem solved
AIDL communication
【C语言】猜数字小游戏
Shell programming conditional statement
ImportError: `save_weights` requires h5py.问题解决
熟悉的朋友
[@synthesize in Objective-C]
number of solutions to solve a multivariate multi-degree equation
找工作必备!如何让面试官对你刮目相看,建议收藏尝试!!
(*゚ヮ゚)*【精品C语言整理】*(゚ヮ゚*)女盆友缠着你让你教她写代码怎么办?安排,三万字博文带你走遍C语言,从此不再害怕编程
VGUgarbage collector(垃圾回收器)的实现原理
C语言必杀技3行代码把运行速度提升4倍
dvwa 通关记录1 - 暴力破解 Brute Force
漫长的投资生涯
今日睡眠质量记录74分
2022 版 MySQL 巅峰教程,收藏好,慢慢看
不卷了!入职字节跳动一周就果断跑了。
使用分类权重解决数据不平衡的问题
感觉自己好傻