【spring】@Autowired bean后置处理器

1.代码示例
package com.learning.bean_processor.autowired_annotation_bean_post_processor;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

public class Student {
	@Autowired
	private Teacher teacher;

	private Dog dog;
	private String home;

	@Autowired
	public void setDog(Dog dog){
		this.dog = dog;
	}

	@Autowired
	public void setHome(@Value("${JAVA_HOME}") String home){
		System.out.println("@Value生效:" + home);
		this.home = home;
	}

	@Override
	public String toString() {
		return "Student{" +
				"teacher=" + teacher +
				", home='" + home + '\'' +
				'}';
	}
}

package com.learning.bean_processor.autowired_annotation_bean_post_processor;

public class Teacher {
}

package com.learning.bean_processor.autowired_annotation_bean_post_processor;

public class Dog {
}
package com.learning.bean_processor.autowired_annotation_bean_post_processor;

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

import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * AutowiredAnnotationBeanPostProcessor运行分析
 */
public class AutowiredAnnotationBeanPostProcessorTest {
	public static void main(String[] args) throws Throwable {
		DefaultListableBeanFactory defaultListableBeanFactory = new DefaultListableBeanFactory();
		defaultListableBeanFactory.registerSingleton("teacher", new Teacher());
		defaultListableBeanFactory.registerSingleton("dog", new Dog());

		// 解析@Value,获取配置值
		defaultListableBeanFactory.setAutowireCandidateResolver(new ContextAnnotationAutowireCandidateResolver());
		// 给${}添加解析器
		defaultListableBeanFactory.addEmbeddedValueResolver(new StandardEnvironment()::resolvePlaceholders);
		// 1.查找哪些属性、方法加了@Autowired,这称之为InjectionMetadata
		AutowiredAnnotationBeanPostProcessor autowiredAnnotationBeanPostProcessor = new AutowiredAnnotationBeanPostProcessor();
		// 设置bean工厂,为了拿到bean的依赖信息
		autowiredAnnotationBeanPostProcessor.setBeanFactory(defaultListableBeanFactory);
		Student student = new Student();
//		System.out.println(student);
//		// 执行依赖注入 @Autowired @Value
//		autowiredAnnotationBeanPostProcessor.postProcessProperties(null, student, "student");
//		System.out.println(student);

		Method findAutowiringMetadata = AutowiredAnnotationBeanPostProcessor.class.getDeclaredMethod("findAutowiringMetadata", String.class, Class.class, PropertyValues.class);
		findAutowiringMetadata.setAccessible(true);
		// 获取Student上加了@Autowired和@Value的方法、参数信息
		InjectionMetadata injectionMetadata = (InjectionMetadata)findAutowiringMetadata.invoke(autowiredAnnotationBeanPostProcessor, "student", Student.class, null);
		System.out.println(injectionMetadata);

		// 2.调用InjectionMetadata 来进行依赖注入,注入时按类型查找值
		injectionMetadata.inject(student, "student", null);

		// 3.如何按类型查找值
		Field teacher = Student.class.getDeclaredField("teacher");
		DependencyDescriptor dependencyDescriptor1 = new DependencyDescriptor(teacher, false);
		Object o1 = defaultListableBeanFactory.doResolveDependency(dependencyDescriptor1, null, null, null);
		System.out.println(o1);

		Method setDog = Student.class.getDeclaredMethod("setDog", Dog.class);
		DependencyDescriptor dependencyDescriptor2 = new DependencyDescriptor(new MethodParameter(setDog, 0), false);
		Object o2 = defaultListableBeanFactory.doResolveDependency(dependencyDescriptor2, null, null, null);
		System.out.println(o2);

		Method setHome = Student.class.getDeclaredMethod("setHome", String.class);
		DependencyDescriptor dependencyDescriptor3 = new DependencyDescriptor(new MethodParameter(setHome, 0), true);
		defaultListableBeanFactory.doResolveDependency(dependencyDescriptor3, null, null, null	);
		System.out.println(student);

	}
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

王佑辉

老板,赏点吧

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值