Spring IOC容器 源码解析系列,建议你们按顺序阅读,欢迎讨论spring
(spring源码均为4.1.6.RELEASE版本)缓存
部门有实习生来的时候,都会先了解系统的基本架构,再写一个基本的demo。一般强调的就是新建的类上要加@Controller或@Service,而类中的引用的其余对象的属性上加上@Autowired,而后写点增删改查就ok了。通常初学一个框架时只需知道如何使用,但想要用的好,就必需要深刻了解其原理。所谓知其然,也要知其因此然。在Spring使用注解后,由于其自己就是非侵入式的设计,致使使用的人能感知到的就是几个注解。这样的框架的设计天然是极好的,对于使用的人来讲,更是值得学习。数据结构
@Controller和@Service都是继承自@Component,只是分别标识Controller层和Service层,用于对类进行分类。@Component做为Spring的类扫描的默认注解,在Spring源码-IOC容器(九)-Component-Scan源码解析中已介绍过。而@Autowired能够说是Spring依赖注入的最重要的注解,本章就来详细解析它的使用原理。架构
@Autowired最经常使用的方式就是放在属性或setter方法上,而其还能放在构造函数以及非setter方法的任意方法上。app
@Target({ElementType.CONSTRUCTOR, ElementType.FIELD, ElementType.METHOD, ElementType.ANNOTATION_TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface Autowired { /** * Declares whether the annotated dependency is required. * <p>Defaults to {@code true}. */ boolean required() default true; }
能够看到支持的目标包括构造函数,属性,方法,也能够被做为元注解。这里须要强调的有两个:框架
@Autowired是根据对象类型来进行匹配的,若是想经过指定具体的bean的名称,可使用@Qualifier。@Autowired只有required属性能够设置,默认为true,即默认必须能匹配到相应的bean,不然就会报出NoSuchBeanDefinitionException异常。若是容许依赖注入失败,则能够设置required=false。ide
@Autowired的解析器是AutowiredAnnotationBeanPostProcessor,它是BeanPostProcessor后置处理器的子类。若是经过BeanFactory的启动方式,须要手动注入。函数
xml文件post
<bean id="autowiredAnnotationBeanPostProcessor" class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
而且经过学习
beanFactory.addBeanPostProcessor((BeanPostProcessor) beanFactory. getBean("autowiredAnnotationBeanPostProcessor"));
添加到beanPostProcessor集合中。
但一般状况下component-scan扫描的配置处理中默认就加载了AutowiredAnnotationBeanPostProcessor。
<context:component-scan base-package=".."/>
若是不使用component-scan的扫描配置,能够经过
<context:annotation-config/>
来默认配置。
在Spring的bean实例化过程当中有许多BeanPostProcessor扩展点,支持内置的以及用户自定义的BeanPostProcessor对bean的实例化进行干预和操做。BeanPostProcessor接口自己只支持bean的初始化先后的扩展,但它的子接口则增长了更多扩展点。
对于AutowiredAnnotationBeanPostProcessor,先来看它的实现
public class AutowiredAnnotationBeanPostProcessor extends InstantiationAwareBeanPostProcessorAdapter implements MergedBeanDefinitionPostProcessor, PriorityOrdered, BeanFactoryAware
它实现了MergedBeanDefinitionPostProcessor接口,而继承的InstantiationAwareBeanPostProcessorAdapter实际上是SmartInstantiationAwareBeanPostProcessor接口的默认实现类。因此基本支持了全部的扩展点,但不少都是继承的默认方法,主要实现的有三个方法:
第一个方法支撑了@Autowired做用于构造函数,第二和三方法支撑了@Autowired做用于属性和方法。
在AbstractAutowireCapableBeanFactory中的createBeanInstance方法中,对bean进行实例化操做。若是没有经过xml指定构造函数或工厂方法,则会判断是否有InstantiationAwareBeanPostProcessor注册到容器中,并判断是否为其子接口SmartInstantiationAwareBeanPostProcessor的实现类,而后调用determineCandidateConstructors方法返回构造函数候选者集合。
AbstractAutowireCapableBeanFactory.createBeanInstance() // Need to determine the constructor... Constructor<?>[] ctors = determineConstructorsFromBeanPostProcessors(beanClass, beanName); if (ctors != null || mbd.getResolvedAutowireMode() == RootBeanDefinition.AUTOWIRE_CONSTRUCTOR || mbd.hasConstructorArgumentValues() || !ObjectUtils.isEmpty(args)) { return autowireConstructor(beanName, mbd, ctors, args); } protected Constructor<?>[] determineConstructorsFromBeanPostProcessors(Class<?> beanClass, String beanName) throws BeansException { if (beanClass != null && hasInstantiationAwareBeanPostProcessors()) { for (BeanPostProcessor bp : getBeanPostProcessors()) { if (bp instanceof SmartInstantiationAwareBeanPostProcessor) { SmartInstantiationAwareBeanPostProcessor ibp = (SmartInstantiationAwareBeanPostProcessor) bp; Constructor<?>[] ctors = ibp.determineCandidateConstructors(beanClass, beanName); if (ctors != null) { return ctors; } } } } return null; }
此时就进入AutowiredAnnotationBeanPostProcessor的determineCandidateConstructors方法中
public Constructor<?>[] determineCandidateConstructors(Class<?> beanClass, final String beanName) throws BeansException { // 解析Lookup注解,并设置到BeanDefinition中 if (!this.lookupMethodsChecked.contains(beanName)) { ReflectionUtils.doWithMethods(beanClass, new ReflectionUtils.MethodCallback() { @Override public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException { Lookup lookup = method.getAnnotation(Lookup.class); if (lookup != null) { LookupOverride override = new LookupOverride(method, lookup.value()); try { RootBeanDefinition mbd = (RootBeanDefinition) beanFactory.getMergedBeanDefinition(beanName); mbd.getMethodOverrides().addOverride(override); } catch (NoSuchBeanDefinitionException ex) { throw new BeanCreationException(beanName, "Cannot apply @Lookup to beans without corresponding bean definition"); } } } }); this.lookupMethodsChecked.add(beanName); } // Quick check on the concurrent map first, with minimal locking. Constructor<?>[] candidateConstructors = this.candidateConstructorsCache.get(beanClass); if (candidateConstructors == null) { synchronized (this.candidateConstructorsCache) { candidateConstructors = this.candidateConstructorsCache.get(beanClass); if (candidateConstructors == null) { // 拿到全部public的构造函数 Constructor<?>[] rawCandidates = beanClass.getDeclaredConstructors(); List<Constructor<?>> candidates = new ArrayList<Constructor<?>>(rawCandidates.length); Constructor<?> requiredConstructor = null; Constructor<?> defaultConstructor = null; for (Constructor<?> candidate : rawCandidates) { // 寻找构造函数上的@Autowired注解 AnnotationAttributes ann = findAutowiredAnnotation(candidate); if (ann != null) { // 只容许有一个required=true的@Autowired if (requiredConstructor != null) { throw new BeanCreationException(beanName, "Invalid autowire-marked constructor: " + candidate + ". Found constructor with 'required' Autowired annotation already: " + requiredConstructor); } // 无参构造函数上设置@Autowired抛出异常 if (candidate.getParameterTypes().length == 0) { throw new IllegalStateException( "Autowired annotation requires at least one argument: " + candidate); } boolean required = determineRequiredStatus(ann); if (required) { if (!candidates.isEmpty()) { throw new BeanCreationException(beanName, "Invalid autowire-marked constructors: " + candidates + ". Found constructor with 'required' Autowired annotation: " + candidate); } requiredConstructor = candidate; } candidates.add(candidate); } else if (candidate.getParameterTypes().length == 0) { defaultConstructor = candidate; } } if (!candidates.isEmpty()) { // Add default constructor to list of optional constructors, as fallback. if (requiredConstructor == null) { if (defaultConstructor != null) { candidates.add(defaultConstructor); } else if (candidates.size() == 1 && logger.isWarnEnabled()) { logger.warn("Inconsistent constructor declaration on bean with name '" + beanName + "': single autowire-marked constructor flagged as optional - this constructor " + "is effectively required since there is no default constructor to fall back to: " + candidates.get(0)); } } candidateConstructors = candidates.toArray(new Constructor<?>[candidates.size()]); } else { candidateConstructors = new Constructor<?>[0]; } this.candidateConstructorsCache.put(beanClass, candidateConstructors); } } } return (candidateConstructors.length > 0 ? candidateConstructors : null); }
基本步骤为:
返回的构造函数集合根据必定的策略找到最佳的一个,而后经过newInstance方法实例化bean对象。
在bean实例化完成后,MergedBeanDefinitionPostProcessor的扩展点支持对BeanDefinition进行处理。
AbstractAutowireCapableBeanFactory.doCreateBean() // Allow post-processors to modify the merged bean definition. synchronized (mbd.postProcessingLock) { if (!mbd.postProcessed) { applyMergedBeanDefinitionPostProcessors(mbd, beanType, beanName); mbd.postProcessed = true; } }
而在AutowiredAnnotationBeanPostProcessor的实现中,经过查找并组装全部有@Autowired的属性及方法。
public void postProcessMergedBeanDefinition(RootBeanDefinition beanDefinition, Class<?> beanType, String beanName) { if (beanType != null) { // 查找全部@Autowired的属性和方法,组装成InjectionMetadata,存放到Map中 InjectionMetadata metadata = findAutowiringMetadata(beanName, beanType, null); metadata.checkConfigMembers(beanDefinition); } }
在findAutowiringMetadata方法中,对属性和方法分开解析并组装不一样的数据结构中,AutowiredFieldElement和AutowiredMethodElement。
private InjectionMetadata findAutowiringMetadata(String beanName, Class<?> clazz, PropertyValues pvs) { // Fall back to class name as cache key, for backwards compatibility with custom callers. // 缓存key为bean名称 String cacheKey = (StringUtils.hasLength(beanName) ? beanName : clazz.getName()); // Quick check on the concurrent map first, with minimal locking. // 先从Map中查询是否存在 InjectionMetadata metadata = this.injectionMetadataCache.get(cacheKey); if (InjectionMetadata.needsRefresh(metadata, clazz)) { synchronized (this.injectionMetadataCache) { metadata = this.injectionMetadataCache.get(cacheKey); if (InjectionMetadata.needsRefresh(metadata, clazz)) { if (metadata != null) { metadata.clear(pvs); } try { // 构建元数据数据结构 metadata = buildAutowiringMetadata(clazz); this.injectionMetadataCache.put(cacheKey, metadata); } catch (NoClassDefFoundError err) { throw new IllegalStateException("Failed to introspect bean class [" + clazz.getName() + "] for autowiring metadata: could not find class that it depends on", err); } } } } return metadata; } // 根据Class对象,对属性和方法分别进行组装 private InjectionMetadata buildAutowiringMetadata(Class<?> clazz) { LinkedList<InjectionMetadata.InjectedElement> elements = new LinkedList<InjectionMetadata.InjectedElement>(); Class<?> targetClass = clazz; do { LinkedList<InjectionMetadata.InjectedElement> currElements = new LinkedList<InjectionMetadata.InjectedElement>(); // 属性 for (Field field : targetClass.getDeclaredFields()) { AnnotationAttributes ann = findAutowiredAnnotation(field); if (ann != null) { if (Modifier.isStatic(field.getModifiers())) { if (logger.isWarnEnabled()) { logger.warn("Autowired annotation is not supported on static fields: " + field); } continue; } boolean required = determineRequiredStatus(ann); currElements.add(new AutowiredFieldElement(field, required)); } } // 方法 for (Method method : targetClass.getDeclaredMethods()) { Method bridgedMethod = BridgeMethodResolver.findBridgedMethod(method); if (!BridgeMethodResolver.isVisibilityBridgeMethodPair(method, bridgedMethod)) { continue; } AnnotationAttributes ann = findAutowiredAnnotation(bridgedMethod); if (ann != null && method.equals(ClassUtils.getMostSpecificMethod(method, clazz))) { if (Modifier.isStatic(method.getModifiers())) { if (logger.isWarnEnabled()) { logger.warn("Autowired annotation is not supported on static methods: " + method); } continue; } if (method.getParameterTypes().length == 0) { if (logger.isWarnEnabled()) { logger.warn("Autowired annotation should be used on methods with actual parameters: " + method); } } boolean required = determineRequiredStatus(ann); PropertyDescriptor pd = BeanUtils.findPropertyForMethod(bridgedMethod, clazz); currElements.add(new AutowiredMethodElement(method, required, pd)); } } elements.addAll(0, currElements); targetClass = targetClass.getSuperclass(); } while (targetClass != null && targetClass != Object.class); return new InjectionMetadata(clazz, elements); }
这样在实例化后,bean中全部@Autowired的属性和方法都被组装成InjectionMetadata存储到以bean名称为key的Map中。
AbstractAutowireCapableBeanFactory的populateBean方法用来对bean进行依赖注入,而在真正的依赖注入前,一样检查是否有InstantiationAwareBeanPostProcessor,并调用postProcessPropertyValues方法对bean的依赖进行外部扩展处理。
AutowiredAnnotationBeanPostProcessor的postProcessPropertyValues也很是清晰。
public PropertyValues postProcessPropertyValues( PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName) throws BeansException { // 再次查找全部@Autowired的属性和方法,若是有缓存,直接从缓存中获取 InjectionMetadata metadata = findAutowiringMetadata(beanName, bean.getClass(), pvs); try { // 执行属性的依赖注入和方法的执行 metadata.inject(bean, beanName, pvs); } catch (Throwable ex) { throw new BeanCreationException(beanName, "Injection of autowired dependencies failed", ex); } return pvs; }
findAutowiringMetadata在上面已经介绍过了,此时直接从Map的缓存中获取便可。主要来看InjectionMetadata的inject方法。
public void inject(Object target, String beanName, PropertyValues pvs) throws Throwable { Collection<InjectedElement> elementsToIterate = (this.checkedElements != null ? this.checkedElements : this.injectedElements); if (!elementsToIterate.isEmpty()) { boolean debug = logger.isDebugEnabled(); for (InjectedElement element : elementsToIterate) { if (debug) { logger.debug("Processing injected element of bean '" + beanName + "': " + element); } element.inject(target, beanName, pvs); } } }
遍历取出全部的InjectedElement,包括属性和方法,分别执行inject方法。
1.AutowiredFieldElement
主要的操做是
经过beanFactory的resolveDependency方法从beanFactory中匹配相同类型的BeanDefition,并经过getBean方法实例化对象,若是有多个匹配项,默认返回第一个。
向容器注册依赖关系,并缓存RuntimeBeanReference类型的FieldValue
经过反射注入属性值
protected void inject(Object bean, String beanName, PropertyValues pvs) throws Throwable { Field field = (Field) this.member; try { Object value; if (this.cached) { value = resolvedCachedArgument(beanName, this.cachedFieldValue); } else { DependencyDescriptor desc = new DependencyDescriptor(field, this.required); desc.setContainingClass(bean.getClass()); Set<String> autowiredBeanNames = new LinkedHashSet<String>(1); TypeConverter typeConverter = beanFactory.getTypeConverter(); // 从beanFactory匹配类型相同的bean对象 value = beanFactory.resolveDependency(desc, beanName, autowiredBeanNames, typeConverter); synchronized (this) { if (!this.cached) { if (value != null || this.required) { this.cachedFieldValue = desc; // 注册对象依赖关系 registerDependentBeans(beanName, autowiredBeanNames); if (autowiredBeanNames.size() == 1) { String autowiredBeanName = autowiredBeanNames.iterator().next(); if (beanFactory.containsBean(autowiredBeanName)) { if (beanFactory.isTypeMatch(autowiredBeanName, field.getType())) { this.cachedFieldValue = new RuntimeBeanReference(autowiredBeanName); } } } } else { this.cachedFieldValue = null; } this.cached = true; } } } if (value != null) { // 反射注入属性值 ReflectionUtils.makeAccessible(field); field.set(bean, value); } } catch (Throwable ex) { throw new BeanCreationException("Could not autowire field: " + field, ex); } }
}
2.AutowiredMethodElement
方法的操做同属性相近,只是根据方法中的每一个参数的类型去beanFactory中去匹配bean对象。
遍历方法中的全部参数,按照类型从beanFactory中匹配相应的BeanDefition并实例化,过程同AutowiredFieldElement中一致
注册对象依赖关系,方法中全部的参数都是bean的依赖方,而后每一个参数分别缓存为RuntimeBeanReference类型的结构。
调用反射方法执行方法
protected void inject(Object bean, String beanName, PropertyValues pvs) throws Throwable { if (checkPropertySkipping(pvs)) { return; } Method method = (Method) this.member; try { Object[] arguments; if (this.cached) { // Shortcut for avoiding synchronization... arguments = resolveCachedArguments(beanName); } else { Class<?>[] paramTypes = method.getParameterTypes(); arguments = new Object[paramTypes.length]; DependencyDescriptor[] descriptors = new DependencyDescriptor[paramTypes.length]; Set<String> autowiredBeanNames = new LinkedHashSet<String>(paramTypes.length); TypeConverter typeConverter = beanFactory.getTypeConverter(); // 遍历方法中的参数,查找对应的bean实例 for (int i = 0; i < arguments.length; i++) { MethodParameter methodParam = new MethodParameter(method, i); DependencyDescriptor desc = new DependencyDescriptor(methodParam, this.required); desc.setContainingClass(bean.getClass()); descriptors[i] = desc; Object arg = beanFactory.resolveDependency(desc, beanName, autowiredBeanNames, typeConverter); if (arg == null && !this.required) { arguments = null; break; } arguments[i] = arg; } synchronized (this) { if (!this.cached) { if (arguments != null) { this.cachedMethodArguments = new Object[arguments.length]; for (int i = 0; i < arguments.length; i++) { this.cachedMethodArguments[i] = descriptors[i]; } // 注册全部的参数为bean的依赖方 registerDependentBeans(beanName, autowiredBeanNames); if (autowiredBeanNames.size() == paramTypes.length) { Iterator<String> it = autowiredBeanNames.iterator(); for (int i = 0; i < paramTypes.length; i++) { String autowiredBeanName = it.next(); if (beanFactory.containsBean(autowiredBeanName)) { if (beanFactory.isTypeMatch(autowiredBeanName, paramTypes[i])) { this.cachedMethodArguments[i] = new RuntimeBeanReference(autowiredBeanName); } } } } } else { this.cachedMethodArguments = null; } this.cached = true; } } } if (arguments != null) { // 反射执行方法 ReflectionUtils.makeAccessible(method); method.invoke(bean, arguments); } } catch (InvocationTargetException ex) { throw ex.getTargetException(); } catch (Throwable ex) { throw new BeanCreationException("Could not autowire method: " + method, ex); } }
这样全部的@Autowired设置都执行完成,注解方式的依赖注入就经过BeanPostProcessor这种扩展点的方式完成。