【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);

	}
}

你可能感兴趣的:(spring,spring,java)