• 1. @Component注解的原理剖析


    1. 动机与目标

    1. 通过Java注解方式配置IOC容器随着Springboot的使用已经逐渐形成了风靡,在日常开发中,启动时可能报错提示找不到beanDifinition,对于此类问题,了解@Component的机制对于排查问题很有帮助;
    2. 为了应对面试;

    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...");
    	}
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    被Component注解修饰的类

    @Component
    public class TargetComponent {
    
    	public TargetComponent() {
    		System.out.println("call TargetComponent no-arg construct...");
    	}
    
    	public void sayHello() {
    		System.out.println("hello...");
    	}
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    为了能够说明@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 ...");
    	}
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    被@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");
    	}
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    驱动类(测试类)

    /**
     * @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();
    	}
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    3. 图示

    ACA: AnnotationConfigApplicationContext

    MainConfig ACA AnnotatedBeanDefinitionReader ClassPathBeanDefinitionScanner ConfigurationClassPostProcessor 创建IOC this() new new register() register doRegisterBean register mainConfig invokeBeanFactoryPostProcessors() processConfigBeanDefinitions() parse() doProcessConfigurationClass() doScan(basePackages) findCandidateComponents & registerBeanDefinition 从配置的包中找到 被@Component修饰的类 还应该有被@Service等修饰的类 MainConfig ACA AnnotatedBeanDefinitionReader ClassPathBeanDefinitionScanner ConfigurationClassPostProcessor

    4. 使用心得

    1. 对于某些第三方工具包里面的类可能会被@Component注解修饰,在使用这些类的时候,由于项目的包扫描@ComponentScan一般不会配置扫描第三方工具包,也不会这么做,因为无法评估工具包下的bean都是安全且合乎要求的。这个时候可以通过写一个被@Component修饰的类,里面可以写一个被@bean修饰的方法,return 新的工具类,还有一个方式在配置类或者组件类上使用@Import注解(待生产验证)。
  • 相关阅读:
    Linux 中 find 命令的 30 个实际例子
    「专题速递」RTC云网端联合优化、弱网对抗策略、QUIC协议的能力和实践
    csdn软件测试入门的测试基本流程
    Machine Learning(一)KNN算法
    uniapp模仿下拉框实现文字联想功能 - uniapp输入联想(官方样式-附源码)
    跨平台编译QWT、安装QWT(Windows、Linux、MacOS环境下编译与安装)
    牛客小白月赛#59(A~F)
    学习记忆——数学篇——代数——记忆宫殿——卧室
    从0开始学JAVA【3】
    揭秘Spring框架:模块装配的奥秘与实战技巧 【Spring|Java】
  • 原文地址:https://blog.csdn.net/sinat_34351851/article/details/126072838