Spring 5.x 源码之旅五十解析配置类加载bean定义过程一

先上图,有个概念

在这里插入图片描述
前面没有很细致的讲解析配置类加载bean定义过程,现在可以把细节给补补,顺便可以理解下配置类注解。

例子

我们这次来一次性把Import,ComponentScan,ImportResource,PropertySource等这些注解都给搞明白了,具体什么时候用,有什么用。

MergeConfig配置类

@Configuration
@Import({ImportBean1.class, MyImportSelector.class, MyImportBeanDefinitionRegistrar.class})
@ComponentScan("com.ww.scan")
@ImportResource("classpath:spring.xml")
@PropertySource("classpath:my.properties")
@MyEnableAnnotation()
public class MergeConfig {

    @Autowired
    Environment environment;

    @Bean
    public Bean1 getBean1() {
        Bean1 bean1 = new Bean1();
        bean1.setAge(Integer.valueOf(environment.getProperty("age")));
        bean1.setName(environment.getProperty("name"));
        return bean1;
    }
	//内部类
    private class InnerClass{
        @Bean
        public Bean2 getBean2(){
            return new Bean2();
        }
    }
}

ImportBean1简单的import进来的类

public class ImportBean1 {
}

MyImportSelector可以import其他类进来

这个也可以进行扩展,因为你可以获取有这个import注解的类的注解信息,比如我这里自己定义了一个注解MyEnableAnnotation,如果发现有这个的话,就import一个ImportSelectorBean类的全限定名,等于说可以有一个开关了,只要有这个注解就可以做点事,其实前面说的AOP的注解也类似,只是他用ImportBeanDefinitionRegistrar直接创建处理器了。

public class MyImportSelector implements ImportSelector {
    @Override
    public String[] selectImports(AnnotationMetadata importingClassMetadata) {
        importingClassMetadata.getAnnotationTypes().forEach(System.out::println);
        if(importingClassMetadata.hasAnnotation("com.ww.annotation.MyEnableAnnotation")){
            return new String[]{"com.ww.pojo.ImportSelectorBean"};
        }
        return null;
    }
}

ImportSelectorBean

public class ImportSelectorBean {
}

MyEnableAnnotation

就是个标记注解,没什么属性。

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyEnableAnnotation {
}

MyImportBeanDefinitionRegistrar

也不做什么,就是为了凑齐所有解析情况而已。

public class MyImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar {
    @Override
    public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {

    }
}

扫描的目录

一个有Component注解,一个没有。
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

bean和属性的配置文件

在这里插入图片描述

spring.xml

里面就一个bean定义。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="beanXml1" class="com.ww.pojo.BeanXml1"></bean>
</beans>

my.properties

就属性,给Bean1进行属性赋值的。

age = 1
name = ww
Bean1

就两个属性,主要看属性配置成不成功。

public class Bean1 {
    private int age;
    private String name;

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
Bean2
public class Bean2 {
}

结果

属性配置没问题。
在这里插入图片描述
bean都实例化成单例了:
在这里插入图片描述
下篇我们介绍具体的流程吧。

好了,今天就到这里了,希望对学习理解有帮助,大神看见勿喷,仅为自己的学习理解,能力有限,请多包涵。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值