Spring-注解-@Autowired原理

  • @Autowired注解处理器(AutowiredAnnotationBeanPostProcessor)
// 在启动spring IoC时,容器会自动装载AutowiredAnnotationBeanPostProcessor后置处理器
public class AutowiredAnnotationBeanPostProcessor extends InstantiationAwareBeanPostProcessorAdapter
		implements MergedBeanDefinitionPostProcessor, PriorityOrdered, BeanFactoryAware {
	...
}
  • 实现了 BeanFactoryAware 接口
  1. 可以提供beanFactory到当前bean实例(AutowiredAnnotationBeanPostProcessor实例)
  2. 当前bean实例从而可以获取到 BeanFactory 里面所有的 Bean。
public interface BeanFactoryAware extends Aware {

	/**
	 * Callback that supplies the owning factory to a bean instance.
	 * <p>Invoked after the population of normal bean properties
	 * but before an initialization callback such as
	 * {@link InitializingBean#afterPropertiesSet()} or a custom init-method.
	 * @param beanFactory owning BeanFactory (never {@code null}). 拥有的beanFactory
	 * The bean can immediately call methods on the factory.
	 * @throws BeansException in case of initialization errors
	 * @see BeanInitializationException
	 */
	void setBeanFactory(BeanFactory beanFactory) throws BeansException;

}
  • 实现了 MergedBeanDefinitionPostProcessor 接口
  1. 为指定的bean后置处理其合并后的bean定义,修改其bena定义
public interface MergedBeanDefinitionPostProcessor extends BeanPostProcessor {

	/**
	 * Post-process the given merged bean definition for the specified bean.
	 * @param beanDefinition the merged bean definition for the bean
	 * @param beanType the actual type of the managed bean instance
	 * @param beanName the name of the bean
	 */
	void postProcessMergedBeanDefinition(RootBeanDefinition beanDefinition, Class<?> beanType, String beanName);

}
  • 实现了 PriorityOrdered 接口(标记接口)
  1. order值相同时,实现PriorityOrdered接口比实现Ordered接口具有更高优先级
  2. 注:实现相同接口时,值越低优先级越高
public interface PriorityOrdered extends Ordered {

}
  • 继承了 InstantiationAwareBeanPostProcessorAdapter(实例化bean后置处理适配器)
public abstract class InstantiationAwareBeanPostProcessorAdapter implements SmartInstantiationAwareBeanPostProcessor {

	// 预言bean类型
	@Override
	public Class<?> predictBeanType(Class<?> beanClass, String beanName) {
		return null;
	}

	// 应该是获取bean的所有构造器
	@Override
	public Constructor<?>[] determineCandidateConstructors(Class<?> beanClass, String beanName) throws BeansException {
		return null;
	}

	// 获取bean的先前引用
	@Override
	public Object getEarlyBeanReference(Object bean, String beanName) throws BeansException {
		return bean;
	}

	// 实例化之前后置处理
	@Override
	public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException {
		return null;
	}

	// 实例化之后后置处理
	@Override
	public boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException {
		return true;
	}

	// 后置处理属性值
	@Override
	public PropertyValues postProcessPropertyValues(
			PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName) throws BeansException {

		return pvs;
	}

	// 实例化之前后置处理
	@Override
	public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
		return bean;
	}

	// 实例化之后后置处理
	@Override
	public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
		return bean;
	}

}

@Autowired 的使用简化了我们的开发,其原理是使用 AutowiredAnnotationBeanPostProcessor 类来实现,该类实现了 Spring 框架的一些扩展接口,通过实现 BeanFactoryAware 接口使其内部持有了 BeanFactory(可轻松的获取需要依赖的的 Bean);通过实现 MergedBeanDefinitionPostProcessor 扩展接口,在 BeanFactory 里面的每个 Bean 实例化前获取到每个 Bean 里面的 @Autowired 信息并缓存下来;通过实现 Spring 框架的 postProcessPropertyValues 扩展接口在 BeanFactory 里面的每个 Bean 实例后从缓存取出对应的注解信息,获取依赖对象,并通过反射设置到 Bean 属性里面

参考:

  1. Spring 中常用注解原理剖析    https://gitbook.cn/books/5aa148e97871be095135025c/index.html?utm_source=blog2gitchat

猜你喜欢

转载自blog.csdn.net/m0_37524661/article/details/87922037