zoukankan      html  css  js  c++  java
  • @Configuration注解作用和在Spring中的处理

    # 作用

    首先编写两个类作为测试

    package com.fh.cglib;
    
    public class AopService {
    
        public void query(){
            System.out.println("query");
        }
    }
    package com.fh.cglib;
    
    public class AopService1 {
    
        public AopService1(){
            System.out.println("init aopService1...");
        }
    }

    然后在写个测试用的配置类

    package com.fh.cglib;
    
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.stereotype.Component;
    
    @Component
    @Configuration
    public class AopConfig {
    
        @Bean
        public AopService getAopService(){
            getAopService1();
            return new AopService();
        }
    
        @Bean
        public AopService1 getAopService1(){
            return new AopService1();
        }
    }

    然后将配置类上的@Configuration去掉和添加分别启动容器,你就会发现AopServoce1的对象初始化调用了两次,对,这就是这个注解的作用

    # 在Spring中的处理

    AbstractApplicationContext#refresh
       AbstractApplicationContext#invokeBeanFactoryPostProcessors
          PostProcessorRegistrationDelegate#invokeBeanFactoryPostProcessors
            PostProcessorRegistrationDelegate#invokeBeanDefinitionRegistryPostProcessors
              ConfigurationClassPostProcessor#postProcessBeanDefinitionRegistry
                 ConfigurationClassPostProcessor#processConfigBeanDefinitions
                   ConfigurationClassUtils#checkConfigurationClassCandidate

    根据这个包结构,我们查看到类对注解的处理操作,可以发现是向beanDef中添加了一个属性值

    AbstractApplicationContext#refresh
       AbstractApplicationContext#invokeBeanFactoryPostProcessors
          AbstractApplicationContext#invokeBeanFactoryPostProcessors
             ConfigurationClassPostProcessor#postProcessBeanFactory
                ConfigurationClassPostProcessor#enhanceConfigurationClasses

    然后我们根据这个包结构找到了对上面设置的属性做的处理,发现如果是属性设置成full的话,会通过cglib设置一个代理类对象

     根据代码中代理对象的设置,发现cglib动态代理添加了一个

    EnhancedConfiguration接口,这个接口的父接口为我们提供了BeanFactory属性,还添加了一个方法拦截数组,在方法拦截中做了一些处理,没有直接调用具体的方法,而是通过判断当前方法和正在执行的方法是否为同一个方法,是的话就执行被代理对象的方法。
    不是的话,直接从BeanFactory中获取,然后返回即可(具体处理没有这么简单)

     方法的判断

     对象的获取


    后续有啥问题再做修改和补充
  • 相关阅读:
    expect 函数体 花括号
    bash 连接字符串
    Ubuntu下搭建Python开发环境
    expect
    >&2
    expect语法基础: while、for 循环、if 语句的用法示例
    bash exit
    python开发工具
    eclipse中安装adt出现了duplicate location错误怎样解决
    shell source
  • 原文地址:https://www.cnblogs.com/nihaofenghao/p/12612437.html
Copyright © 2011-2022 走看看