• 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注解(待生产验证)。
  • 相关阅读:
    vue模板语法下集
    jQuery基础
    MacOS下brew切换为国内源
    文件操作(1)
    Confluence使用教程-不古出品
    音视频开发:ffplay使用sonic实现倍速播放
    投稿期刊被on hold了是怎么回事?SCI期刊状态解读(超全整理,一文解惑)
    【Tomcat服务部署及优化】
    React中的Redux:简介和实例代码
    16、IOC 之 BeanFactory 接口
  • 原文地址:https://blog.csdn.net/sinat_34351851/article/details/126072838