当前位置:网站首页>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
注解(待生产验证)。
边栏推荐
- shell specification and variables
- 一种灵活的智能合约协作方式
- AQS
- LeetCode952三部曲之一:解题思路和初级解法(137ms,超39%)
- 0DFS Medium LeetCode6134. Find the closest node to the given two nodes
- Upload markdown documents to blog garden
- 罗克韦尔AB PLC RSLogix5000中的比较指令使用方法介绍
- Based on php online learning platform management system acquisition (php graduation design)
- Based on php animation peripheral mall management system (php graduation design)
- 365 days challenge LeetCode1000 questions - Day 046 Generate a string with odd number of each character + add two numbers + valid parentheses
猜你喜欢
随机推荐
WEB渗透之SQL 注入
Based on php online music website management system acquisition (php graduation design)
Dichotomy Medium LeetCode6133. Maximum Number of Groups
基于php湘西旅游网站管理系统获取(php毕业设计)
Spark practice questions + answers
使用 Zokrates 在 BSV 上创建您的第一个 zkSNARK 证明
模拟数据之mockjs
小程序--分包
SAP ABAP OData 服务如何支持删除(Delete)操作试读版
找工作必备!如何让面试官对你刮目相看,建议收藏尝试!!
线上故障排查方案
线程池分析
企业公众号文章写作方向:如何写出读者认可的优质内容
自建 Prometheus 采集腾讯云容器服务监控数据最佳实践
MySQL相关知识
Anacoda的用途
[ASM] Bytecode Operation MethodWriter
递归(各经典例题分析)
【C语言实现】整数排序-四种方法,你都会了吗、
使用分类权重解决数据不平衡的问题