一、目的
本篇文章你可以学到
1、Spring如何找到带有@Autowired注解的属性字段和带有@Autowired注解的方法,
2、Spring如何通过@Autowired给属性赋值,如何调用@Autowired注解的方法
二、源码流程
1、先看下创建doCreateBean的方法
Object beanInstance = doCreateBean(beanName, mbdToUse, args);
在创建Bean的时候,会给Bean里面的属性完成赋值。
protected Object doCreateBean(final String beanName, final RootBeanDefinition mbd, final Object[] args) throws BeanCreationException {
......
//执行后置处理,这里会有部分后置处理器会得到执行
synchronized (mbd.postProcessingLock) {
applyMergedBeanDefinitionPostProcessors(mbd, beanType, beanName);
}
......
}
2、AutowiredAnnotationBeanPostProcessor
//拿到所有BeanPostProcessors,只处理MergedBeanDefinitionPostProcessor类型的
//这里引入一个很重要的后处理器AutowiredAnnotationBeanPostProcessor
protected void applyMergedBeanDefinitionPostProcessors(RootBeanDefinition mbd, Class<?> beanType, String beanName) {
for (BeanPostProcessor bp : getBeanPostProcessors()) {
if (bp instanceof MergedBeanDefinitionPostProcessor) {
MergedBeanDefinitionPostProcessor bdp = (MergedBeanDefinitionPostProcessor) bp;
//这段代码,就是完成找到带有@aotuwired属性和方法操作,并且把属性和方法塞入缓存中,
//下次处理这个类的时候,就从缓存里面取
bdp.postProcessMergedBeanDefinition(mbd, beanType, beanName);
}
}
}
AutowiredAnnotationBeanPostProcessor的功能如下
- 找到带有@Autowired属性和处理@Autowired的方法
- 针对上面找到的带有@Autowired注解属性和方法,如果@Autowired是修饰属性变量就给其赋值,如果@Autowired是修饰方法,则执行该方法。
3、AutowiredAnnotationBeanPostProcessor里面关键代码
private InjectionMetadata buildAutowiringMetadata(final Class<?> clazz) {
。。。。
判断是否是带有@Autowired注解的属性
ReflectionUtils.doWithLocalFields(targetClass, new ReflectionUtils.FieldCallback() {
@Override
public void doWith(Field field) throws IllegalArgumentException, IllegalAccessException {
AnnotationAttributes ann = findAutowiredAnnotation(field);//判断这个类上面是否有@Autowired注解,如果有返回AnnotationAttributes
if (ann != null) {
if (Modifier.isStatic(field.getModifiers())) {//静态方法跳过
return;
}
。。。。。
}
}
});
//判断是否是带有@Autowired注解的Method
ReflectionUtils.doWithLocalMethods(targetClass, new ReflectionUtils.MethodCallback() {
@Override
public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException {
Method bridgedMethod = BridgeMethodResolver.findBridgedMethod(method);
if (!BridgeMethodResolver.isVisibilityBridgeMethodPair(method, bridgedMethod)) {
return;
}
AnnotationAttributes ann = findAutowiredAnnotation(bridgedMethod);
if (ann != null && method.equals(ClassUtils.getMostSpecificMethod(method, clazz))) {
if (Modifier.isStatic(method.getModifiers())) {//跳过静态方法
return;
}
if (method.getParameterTypes().length == 0) {//如果参数个数为空跳过
}
。。。。。
}
}
});
}
上面有个方法findAutowiredAnnotation,这个是查询字段是否带有@Autowired注解,看下是找到处理。
//this.autowiredAnnotationTypes里面包含@Value、@Autowired、@Inject这三个注解
for (Class<? extends Annotation> type : this.autowiredAnnotationTypes) {
//通过java反射得到的,里面包含属性上面的注解、字段类型、访问修饰符
AnnotationAttributes attributes = AnnotatedElementUtils.getMergedAnnotationAttributes(ao, type);
if (attributes != null) {
return attributes;
}
}
List<Annotation> annotations = Arrays.asList(element.getDeclaredAnnotations());//拿到该属性上面所有的注解
for (Annotation annotation : annotations) {
//判断属性
if (annotation.annotationType() == annotationType || annotation.annotationType().getName().equals(annotationName)) {
T result = processor.process(element, annotation, metaDepth);
//注解里面的方法为了设置默认值,因为我们在使用注解时,有的时候,不会设置注解里面的默认值,那么这一步就是为了设置默认值,后面会去读注解里面的这些字段的值
if (result != null) {
if (processor.aggregates() && metaDepth == 0) {
processor.getAggregatedResults().add(result);
}
}
}
}
上面介绍了怎么找到带带有@Autowired注解,关于如何给带有@Autowired属性变量赋值和执行带有被@Autowired注解修饰的方法。请看这里@Autowired赋值