Spring 高级 AutowiredAnnotationBeanPostProcessor 运行分析

1、AutowiredAnnotationBeanPostProcessor 运行分析

AutowiredAnnotationBeanPostProcessor 的作用:AutowiredAnnotationBeanPostProcessor 解析 @Autowired@Value----执行时机 在依赖注入阶段解析@Autowired@Value

一、processor.postProcessProperties(null,bean1,"bean1")解析 @Autowired@Value

复制代码
package com.mangoubiubiu.show.a04;


import org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.context.annotation.ContextAnnotationAutowireCandidateResolver;

public class DigInAutowired {

    public static void main(String[] args) {

        DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
        beanFactory.registerSingleton("bean2",new Bean2());//创建过程,依赖注入,初始化
        beanFactory.registerSingleton("bean3",new Bean3());

        beanFactory.setAutowireCandidateResolver(new ContextAnnotationAutowireCandidateResolver());//value

        //1、查找哪些属性、方法加了@Autowired,这称之为InjectionMetadata
        AutowiredAnnotationBeanPostProcessor processor = new AutowiredAnnotationBeanPostProcessor();
        //设置bean 工厂 让bean工厂去找Bean
        processor.setBeanFactory(beanFactory);

        //自己创建一个bean1 自己创建的并没有依赖注入
        Bean1 bean1 = new Bean1();

        System.out.println(bean1);

        processor.postProcessProperties(null,bean1,"bean1");

        System.out.println(bean1);


    }
}
复制代码
复制代码
package com.mangoubiubiu.show.a04;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.annotation.Resource;


public class Bean1 {

    private static final Logger log = LoggerFactory.getLogger(Bean1.class);

    private Bean2 bean2;

    @Autowired
    public void setBean2(Bean2 bean2){
        log.info("@Autowired 生效:{}",bean2);
        this.bean2=bean2;
    }


    private Bean3 bean3;

    public void setBean3(Bean3 bean3){
        log.info("@Resource 生效:{}",bean3);
        this.bean3=bean3;
    }


    private String home;

    @Autowired
    public void setHomeAutowired(@Value("${JAVA_HOME}") String home){
        log.info("Autowired@Value 生效:{}",home);
        this.home = home;
    }

    @Resource
    public void setHomeResource(@Value("${JAVA_HOME}") String home){
        log.info("Resource@Value 生效:{}",home);
        this.home = home;
    }

    @PostConstruct
    public void initPostConstruct(){
        log.info("@PostConstruct 生效");
    }

    @PreDestroy
    public void initPreDestroy(){
        log.info("@PreDestroy 生效");
    }


    @Override
    public String toString() {
        return "Bean1{" +
                "bean2=" + bean2 +
                ", bean3=" + bean3 +
                ", home='" + home + '\'' +
                '}';
    }
}
复制代码

二、AutowiredAnnotationBeanPostProcessor.findAutowiringMetadata 用来获取某个 bean 上加了 @Value @Autowired 的成员变量,方法参数的信息,表示为 InjectionMetadata

 

 

复制代码
package com.mangoubiubiu.show.a04;


import org.springframework.beans.PropertyValues;
import org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor;
import org.springframework.beans.factory.annotation.InjectionMetadata;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.context.annotation.ContextAnnotationAutowireCandidateResolver;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class DigInAutowired {

    public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {

        DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
        beanFactory.registerSingleton("bean2",new Bean2());//创建过程,依赖注入,初始化
        beanFactory.registerSingleton("bean3",new Bean3());

        beanFactory.setAutowireCandidateResolver(new ContextAnnotationAutowireCandidateResolver());//value

        //1、查找哪些属性、方法加了@Autowired,这称之为InjectionMetadata
        AutowiredAnnotationBeanPostProcessor processor = new AutowiredAnnotationBeanPostProcessor();
        //设置bean 工厂 让bean工厂去找Bean
        processor.setBeanFactory(beanFactory);

        //自己创建一个bean1 自己创建的并没有依赖注入
        Bean1 bean1 = new Bean1();

        System.out.println(bean1);

//        processor.postProcessProperties(null,bean1,"bean1");
      Method findAutowiringMetadata =  AutowiredAnnotationBeanPostProcessor.class.getDeclaredMethod("findAutowiringMetadata", String.class, Class.class, PropertyValues.class);
        findAutowiringMetadata.setAccessible(true);
        //获取 Bean1 上加了 @Value @Autowired 的成员变量 , 方法参数
        InjectionMetadata metadata = (InjectionMetadata)findAutowiringMetadata.invoke(processor, "bean1", Bean1.class, null);

        System.out.println(metadata);
        System.out.println(bean1);


    }
}
复制代码

三、metadata.inject(bean1,"bean1",null); 进行依赖注入

复制代码
package com.mangoubiubiu.show.a04;


import org.springframework.beans.PropertyValues;
import org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor;
import org.springframework.beans.factory.annotation.InjectionMetadata;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.context.annotation.ContextAnnotationAutowireCandidateResolver;
import org.springframework.core.env.StandardEnvironment;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class DigInAutowired {

    public static void main(String[] args) throws Throwable {

        DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
        beanFactory.registerSingleton("bean2",new Bean2());//创建过程,依赖注入,初始化
        beanFactory.registerSingleton("bean3",new Bean3());

        beanFactory.setAutowireCandidateResolver(new ContextAnnotationAutowireCandidateResolver());//value
        beanFactory.addEmbeddedValueResolver(new StandardEnvironment()::resolvePlaceholders);
        //1、查找哪些属性、方法加了@Autowired,这称之为InjectionMetadata
        AutowiredAnnotationBeanPostProcessor processor = new AutowiredAnnotationBeanPostProcessor();
        //设置bean 工厂 让bean工厂去找Bean
        processor.setBeanFactory(beanFactory);

        //自己创建一个bean1 自己创建的并没有依赖注入
        Bean1 bean1 = new Bean1();

        System.out.println(bean1);

//        processor.postProcessProperties(null,bean1,"bean1");
        Method findAutowiringMetadata =  AutowiredAnnotationBeanPostProcessor.class.getDeclaredMethod("findAutowiringMetadata", String.class, Class.class, PropertyValues.class);
        findAutowiringMetadata.setAccessible(true);
        //获取 Bean1 上加了 @Value @Autowired 的成员变量 , 方法参数
        InjectionMetadata metadata = (InjectionMetadata)findAutowiringMetadata.invoke(processor, "bean1", Bean1.class, null);
        //进行依赖注入
        metadata.inject(bean1,"bean1",null);
        System.out.println(metadata);
        System.out.println(bean1);
    }
}
复制代码

总结 :AutowiredAnnotationBeanPostProcessor 执行依赖注入的时候,实际上是先调用了postProcessProperties方法,而postProcessProperties方法内部 分为2步去进行依赖注入,第一步是用findAutowiringMetadata方法找到某个类型中标注了@Autowired 方法 成员变量 收集起来,信息叫做InjectionMetadata,找到信息后 InjectionMetadata 执行 inject 才完成反射调用 进行依赖注入。

 

本文作者:KwFruit

本文链接:https://www.cnblogs.com/mangoubiubiu/p/16591665.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   KwFruit  阅读(145)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起