@Autowired
@Autowired注解的源码:
@Target({ElementType.CONSTRUCTOR, ElementType.METHOD, ElementType.PARAMETER, ElementType.FIELD, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Autowired {
boolean required() default true;
}
从源码看出,它可以标注在构造函数、属性、方法上,用来实现依赖注入。
原理:
@Autowired注解位于org.springframework.beans.factory.annotation包下面,在这个包下面有一个关键的类:AutowiredAnnotationBeanPostProcessor(后置处理器),Spring对**@Autowired注解的实现逻辑就在AutowiredAnnotationBeanPostProcessor**实现的。
当代码执行到AbstractAutowireCapableBeanFactory的doCreateBean方法,会有这面一段代码
synchronized(mbd.postProcessingLock) {
if (!mbd.postProcessed) {
try {
this.applyMergedBeanDefinitionPostProcessors(mbd, beanType, beanName);
} catch (Throwable var17) {
throw new BeanCreationException(mbd.getResourceDescription(), beanName, "Post-processing of merged bean definition failed", var17);
}
mbd.postProcessed = true;
}
}
这里允许后置处理来修改bean definition,当beanName为controller时,进入applyMergedBeanDefinitionPostProcessors方法,
protected void applyMergedBeanDefinitionPostProcessors(RootBeanDefinition mbd, Class<?> beanType, String beanName) {
// 当bp为CommonAnnotationBeanPostProcessor时,会进行@Resource的注入
// 当bp为AutowiredAnnotationBeanPostProcessor时,会进行@Autowired的注入
for (BeanPostProcessor bp : getBeanPostProcessors()) {
if (bp instanceof MergedBeanDefinitionPostProcessor) {
MergedBeanDefinitionPostProcessor bdp = (MergedBeanDefinitionPostProcessor) bp;
bdp.postProcessMergedBeanDefinition(mbd, beanType, beanName);
}
}
}
当bp为AutowiredAnnotationBeanPostProcessor时,进入AutowiredAnnotationBeanPostProcessor类的postProcessMergedBeanDefinition方法,在findAutowiringMetadata方法里,会build metadata数据,以上这个metadata会返回targetClass为controller,injectedElement为service的autowired数据,这样需要autowired的数据被获取到。
当beanName为controller时,会进入postProcessProperties方法。
@Override
public PropertyValues postProcessProperties(PropertyValues pvs, Object bean, String beanName) {
InjectionMetadata metadata = findAutowiringMetadata(beanName, bean.getClass(), pvs);
try {
metadata.inject(bean, beanName, pvs);
}
catch (BeanCreationException ex) {
throw ex;
}
catch (Throwable ex) {
throw new BeanCreationException(beanName, "Injection of autowired dependencies failed", ex);
}
return pvs;
}
在此方法中,获取注入的元数据,这里获取metadata是从缓存获取的,获取metadata后,便开始反射执行对应的bean和properties。
进入metadata.inject(bean, beanName, pvs)方法,执行element.inject(target, beanName, pvs)方法,
public void inject(Object target, @Nullable String beanName, @Nullable PropertyValues pvs) throws Throwable {
Collection<InjectedElement> checkedElements = this.checkedElements;
Collection<InjectedElement> elementsToIterate =
(checkedElements != null ? checkedElements : this.injectedElements);
if (!elementsToIterate.isEmpty()) {
for (InjectedElement element : elementsToIterate) {
if (logger.isTraceEnabled()) {
logger.trace("Processing injected element of bean '" + beanName + "': " + element);
}
// 注入到目标bean
element.inject(target, beanName, pvs);
}
}
}
进入element的inject方法(AutowiredAnnotationBeanPostProcessor的inject方法),获取到需要注入的数据,最后通过ReflectionUtils.makeAccessible(field);field.set(bean, value);进行了真正的反射注入操作。