当前位置:网站首页>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注解(待生产验证)。
边栏推荐
- File operations of WEB penetration
- 漫长的投资生涯
- 高等代数_证明_矩阵的行列式为特征值之积, 矩阵的迹为特征值之和
- 10 Practical Uses of NFTs (NFT System Development)
- 基于php影视资讯网站管理系统获取(php毕业设计)
- 365 days challenge LeetCode1000 questions - Day 046 Generate a string with odd number of each character + add two numbers + valid parentheses
- Spark练习题+答案
- 高等代数_证明_矩阵的任意特征值的代数重数大于等于其几何重数
- shell规范与变量
- 线程池分析
猜你喜欢

WEB渗透之SQL 注入

2022 edition of MySQL tutorial, top collection good, take your time

【C语言实现】两种计算平均成绩题型,博主精心整理,值得一读

AIDL communication

网络水军第一课:手写自动弹幕

基于php在线考试管理系统获取(php毕业设计)

Raspberry Pi information display small screen, display time, IP address, CPU information, memory information (C language), four-wire i2c communication, 0.96-inch oled screen

Mini Program--Independent Subcontracting & Subcontracting Pre-download

Based on php hotel online reservation management system acquisition (php graduation project)

Homework 8.1 Orphans and Zombies
随机推荐
_ _ determinant of a matrix is higher algebra eigenvalue of the product, the characteristic value of matrix trace is combined
Based on php film and television information website management system acquisition (php graduation design)
ImportError: `save_weights` requires h5py.问题解决
【C语言实现】求两个整数的较大值
Spark shuffle调优
VGUgarbage collector(垃圾回收器)的实现原理
HCIP---Multiple Spanning Tree Protocol related knowledge points
selenium无头,防检测
dvwa 通关记录1 - 暴力破解 Brute Force
User Experience | How to Measure User Experience?
ModuleNotFoundError: No module named 'yaml'
feel so stupid
Based on php tourism website management system acquisition (php graduation design)
ImportError: `save_weights` requires h5py. Problem solved
[Niu Ke brush questions-SQL big factory interview questions] NO4. Travel scene (a taxi)
高等代数_证明_矩阵的行列式为特征值之积, 矩阵的迹为特征值之和
Advanced Algebra_Proof_The algebraic multiplicity of any eigenvalue of a matrix is greater than or equal to its geometric multiplicity
小程序中的多表联合查询
[@synthesize in Objective-C]
Spark shuffle tuning