Spring IOC基于注解启动分析

点击上方 "程序员小乐" ,关注公众号

8点20分,第一时间与你相约

每日英文

Don’t worry, someday all the pain will pass and you will become stronger and happier than before.

不要着急,所有的伤痛终将结束,你将比过去更加坚强,幸福。

每日掏心话

宁可装傻,也不要自作聪明。使人成熟的是经历,而不是岁月。我相信,梦里能到达的地方,总有一天,脚步也能到达。


来自:神易风 | 责编:乐乐

链接:dwz.cn/F5J76iuu

640?wx_fmt=jpeg

图片来自网络

往日回顾:从四分钟到两秒——谈谈客户端性能优化的一些最佳实践

   正文   

Spring 基于注解启动

主要有两个Class实现注解启动

  • AnnotationConfigApplicationContext

  • AnnotationConfigWebApplicationContext

我们以AnnotationConfigApplicationContext 为研究对象

640?wx_fmt=otherAnnotationConfigApplicationContext.png

引入Spring 最小依赖

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>

编写器启动代码

 public static void main(String[] args) {
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
        applicationContext.register(BeanConfig.class);
        applicationContext.refresh();
        Date date = applicationContext.getBean("date",Date.class);
        System.out.println(date);
    }

AnnotationConfigApplicationContext 构造函数

    public AnnotationConfigApplicationContext() {
                //负责注册Class ,读取器
        this.reader = new AnnotatedBeanDefinitionReader(this);
               //负责扫描指定类路径下的Class,注册bean
        this.scanner = new ClassPathBeanDefinitionScanner(this);
    }

AnnotatedBeanDefinitionReader 构造方法

    public AnnotatedBeanDefinitionReader(BeanDefinitionRegistry registry) {
        this(registry, getOrCreateEnvironment(registry));
    }

    public AnnotatedBeanDefinitionReader(BeanDefinitionRegistry registry, Environment environment) {
        Assert.notNull(registry, "BeanDefinitionRegistry must not be null");
        Assert.notNull(environment, "Environment must not be null");
        this.registry = registry;
               //初始化ConditionEvaluator
        this.conditionEvaluator = new ConditionEvaluator(registry, environment, null);

        /** 在給定的注册表中註冊所有相关的post processors
         *  判断容器是否已经存在给定注册表的bean,如果没有注册bean,并将bean放入容器中
         *  把所有的处理处理器列出来
         * ConfigurationClassPostProcessor 內部管理的配置注解处理器
         * AutowiredAnnotationBeanPostProcessor  内部管理@Autowired 的处理器
         * RequiredAnnotationBeanPostProcessor  @Required的处理器
         * CommonAnnotationBeanPostProcessor  JSR-250注解处理器 ,先判断是否支持jsr,如果支持注册
         * PersistenceAnnotationBeanPostProcessor JPA管理 先使用类加载器查找是否存在,如果有这个包则注册
         * EventListenerMethodProcessor  @EventListener的处理器
         * DefaultEventListenerFactory 管理EventListenerFactory处理器
         */
        AnnotationConfigUtils.registerAnnotationConfigProcessors(this.registry);
    }

ConditionEvaluator 这个对象干什么,点击进去

    public ConditionEvaluator(@Nullable BeanDefinitionRegistry registry,
            @Nullable Environment environment, @Nullable ResourceLoader resourceLoader) {

        this.context = new ConditionContextImpl(registry, environment, resourceLoader); 
    }
        //ConditionContextImpl 实现了ConditionContext接口,ConditionEvaluator静态内部类
        public ConditionContextImpl(@Nullable BeanDefinitionRegistry registry,
                @Nullable Environment environment, @Nullable ResourceLoader resourceLoader) {

            this.registry = registry;
            this.beanFactory = deduceBeanFactory(registry);
            this.environment = (environment != null ? environment : deduceEnvironment(registry));
            this.resourceLoader = (resourceLoader != null ? resourceLoader : deduceResourceLoader(registry));
            this.classLoader = deduceClassLoader(resourceLoader, this.beanFactory);
        }

可以知道ConditionEvaluator使用外部传参的方法初始化了Spring容器顶级对象

ClassPathBeanDefinitionScanner构造函数

    public ClassPathBeanDefinitionScanner(BeanDefinitionRegistry registry) {
        this(registry, true);
    }

    public ClassPathBeanDefinitionScanner(BeanDefinitionRegistry registry, boolean useDefaultFilters) {
        this(registry, useDefaultFilters, getOrCreateEnvironment(registry));
    }

    public ClassPathBeanDefinitionScanner(BeanDefinitionRegistry registry, boolean useDefaultFilters,
            Environment environment) {
        this(registry, useDefaultFilters, environment,
                (registry instanceof ResourceLoader ? (ResourceLoader) registry : null));
    }

    public ClassPathBeanDefinitionScanner(BeanDefinitionRegistry registry, boolean useDefaultFilters,
            Environment environment, @Nullable ResourceLoader resourceLoader) {
        Assert.notNull(registry, "BeanDefinitionRegistry must not be null");
        this.registry = registry;
        if (useDefaultFilters) {
            registerDefaultFilters();
        }
        setEnvironment(environment);
        setResourceLoader(resourceLoader);
    }

    protected void registerDefaultFilters() {
        this.includeFilters.add(new AnnotationTypeFilter(Component.class));
        ClassLoader cl = ClassPathScanningCandidateComponentProvider.class.getClassLoader();
        try {
            this.includeFilters.add(new AnnotationTypeFilter(
                    ((Class<? extends Annotation>) ClassUtils.forName("javax.annotation.ManagedBean", cl)), false));
            logger.debug("JSR-250 'javax.annotation.ManagedBean' found and supported for component scanning");
        }
        catch (ClassNotFoundException ex) {
        }
        try {
            this.includeFilters.add(new AnnotationTypeFilter(
                    ((Class<? extends Annotation>) ClassUtils.forName("javax.inject.Named", cl)), false));
            logger.debug("JSR-330 'javax.inject.Named' annotation found and supported for component scanning");
        }
        catch (ClassNotFoundException ex) {
            // JSR-330 API not available - simply skip.
        }
    }

绕了地球几圈了,其实就是将Spring 顶级接口 Environment,ResourceLoader赋值,使用默认注解过滤器,首先将@Component加入List中,判断当前环境是否支持JSR-250,JSR-330,相应加入过滤器中。也就是这个扫描器默认只扫描@Component或者JSR-250,JSR-330的标记的Class。

applicationContext.register(BeanConfig.class)

    public void register(Class<?>... annotatedClasses) {
        Assert.notEmpty(annotatedClasses, "At least one annotated class must be specified");
        this.reader.register(annotatedClasses); //调用 刚刚初始化读取器
    }
                                            |
============================AnnotatedBeanDefinitionReader 读取器代码======================================================================================================
       public void register(Class<?>... annotatedClasses) {
        for (Class<?> annotatedClass : annotatedClasses) {
            registerBean(annotatedClass);
        }
     }

    public void registerBean(Class<?> annotatedClass) {
        doRegisterBean(annotatedClass, null, null, null);
    }

    /**
    *从给定的bean解析Class给定的注解,执行相应的初始化,保存到Spring容器中
    */
    <T> void doRegisterBean(Class<T> annotatedClass, @Nullable Supplier<T> instanceSupplier, @Nullable String name,
            @Nullable Class<? extends Annotation>[] qualifiers, BeanDefinitionCustomizer... definitionCustomizers) {

        //根据Class的Annotated 得出元数据 AnnotationMetadata
        AnnotatedGenericBeanDefinition abd = new AnnotatedGenericBeanDefinition(annotatedClass);
       /**
        *  判断注册的Class 是否包含@Conditional注解,如果有获取全部value,放入List中
        *  排序后,遍历所有的Conditiion的实现,使用反射获取对象,执行matches方法,
        * 如果发现有返回false,中断循环直接返回true,
        */
        if (this.conditionEvaluator.shouldSkip(abd.getMetadata())) { //如果 @Conditional条件不满足,不进行注册
            return;
        }

        abd.setInstanceSupplier(instanceSupplier);
        //解析Class是否有@Scope,解析@Scope注解返回ScopeMetadata对象,没有直接返回空
        ScopeMetadata scopeMetadata = this.scopeMetadataResolver.resolveScopeMetadata(abd);
        abd.setScope(scopeMetadata.getScopeName());
       //判断注解上Value是否有值,有就使用这个作为BeanName,没有则取类名 
        String beanName = (name != null ? name : this.beanNameGenerator.generateBeanName(abd, this.registry));
        //继续解析AnnotationMetadata的@Lazy,@Primary,@DependsOn,@Role,@Description的注解,放入结果放入对象的属性中
        AnnotationConfigUtils.processCommonDefinitionAnnotations(abd);
        //这个类只是BeanDefinition 包装类
        BeanDefinitionHolder definitionHolder = new BeanDefinitionHolder(abd, beanName);
        //是否需要代理类,如果是则修改内部属性,重新生成BeanDefinition 对象
        definitionHolder = AnnotationConfigUtils.applyScopedProxyMode(scopeMetadata, definitionHolder, this.registry);
      //调用DefaultListableBeanFactory.registerBeanDefinition的方法,做一些安全性校验再,将definitionHolder  放入register容器中
        BeanDefinitionReaderUtils.registerBeanDefinition(definitionHolder, this.registry);
    }

这个方法就是将注册的Bean,解析Class上的注解,初始化注解数据,做相应处理,转化成BeanDefinition ,放入Spring 容器中保存起来。

我们看下BeanDefinition是怎么实现注册到Spring的容器中,主要由DefaultListableBeanFactory.registerBeanDefinition来实现

 
 

将beanDefinition注册到Spring容器中,并没有太多复杂的逻辑,只是做一些安全性的检查。

BeanDefinition

一个BeanDefinition描述了一个bean的实例,包括属性值,构造方法参数值和继承自它的类的更多信息。BeanDefinition仅仅是一个最简单的接口,主要功能是允许BeanFactoryPostProcessor 例如PropertyPlaceHolderConfigure 能够检索并修改属性值和别的bean的元数据(译注)

  • RootBeanDefinition     Spring Factory中的特定bean

  • AnnotatedGenericBeanDefinition      用户自定义bean

Spring 启动流程总结

640?wx_fmt=other

AnnotationConfigApplicationContext 初始化.png

这些BeanDefinition只是放入到Spirng 容器中,并没有进行任何初始化对象的操作,真正的IOC操作都在refresh(),这个方法有空再进行分析。

欢迎在留言区留下你的观点,一起讨论提高。如果今天的文章让你有新的启发,学习能力的提升上有新的认识,欢迎转发分享给更多人。

欢迎各位读者加入程序员小乐技术群,在公众号后台回复“加群”或者“学习”即可。

640?wx_fmt=png

猜你还想看

阿里、腾讯、百度、华为、京东最新面试题汇集

分享8年开发经验,浅谈个人发展经历,明确自己发展方向

坠入谷底的百度,能否实现绝地反弹?

HashMap 如何解决冲突?扩容机制?

百度地震了,也许早晚的事

面试官问:一个 Java 字符串中到底有多少个字符?...

640?wx_fmt=png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值