由于第一次调用并未有任何处理,我们从第二次调用开始分析
createBeanInstance protected BeanWrapper createBeanInstance(String beanName, RootBeanDefinition mbd, @Nullable Object[] args){ // 获取beanClass Class<?> beanClass = resolveBeanClass(mbd, beanName); // 使用AutowiredAnnotationBeanPostProcessor进行构造器推断,找到所有的有参构造器 Constructor<?>[] ctors = determineConstructorsFromBeanPostProcessors(beanClass, beanName); if (ctors != null || mbd.getResolvedAutowireMode() == AUTOWIRE_CONSTRUCTOR || mbd.hasConstructorArgumentValues() || !ObjectUtils.isEmpty(args)) { // 实例化bean,并根据参数自动装配 return autowireConstructor(beanName, mbd, ctors, args); } // 调用无参的构造方法实例化 return instantiateBean(beanName, mbd); } determineConstructorsFromBeanPostProcessors protected Constructor<?>[] determineConstructorsFromBeanPostProcessors(@Nullable Class<?> beanClass, String beanName) throws BeansException { if (beanClass != null && hasInstantiationAwareBeanPostProcessors()) { for (BeanPostProcessor bp : getBeanPostProcessors()) { if (bp instanceof SmartInstantiationAwareBeanPostProcessor) { // 只有AutowiredAnnotationBeanPostProcessor进行了实现,其他的都返回null 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){ // 获取到所有的构造方法 rawCandidates = beanClass.getDeclaredConstructors(); for (Constructor<?> candidate : rawCandidates) { // 是否带有@Autowired注解 MergedAnnotation<?> ann = findAutowiredAnnotation(candidate); if (ann != null) { // 是否必须 boolean required = determineRequiredStatus(ann); candidates.add(candidate); } else if (candidate.getParameterCount() == 0) { // 无参构造器 defaultConstructor = candidate; } } // 候选的构造器不为空 if (!candidates.isEmpty()) { // 候选的构造器不为空而requiredConstructor为空表示有@Autowired标识的构造器 // 但是required=false if (requiredConstructor == null) { if (defaultConstructor != null) { // 将无参构造器也加入到候选构造器集合中 candidates.add(defaultConstructor); } } // 将集合中的构造器转化为数组 candidateConstructors = candidates.toArray(new Constructor<?>[0]); } // 候选的构造器为空,但有一个有参构造器,则使用有参构造器作为候选的构造器 else if (rawCandidates.length == 1 && rawCandidates[0].getParameterCount() > 0) { candidateConstructors = new Constructor<?>[] {rawCandidates[0]}; } // 返回候选构造器数组 return (candidateConstructors.length > 0 ? candidateConstructors : null); } autowireConstructor 实例化并自动装配,摘取代码片段 protected BeanWrapper autowireConstructor( String beanName, RootBeanDefinition mbd, @Nullable Constructor<?>[] ctors, @Nullable Object[] explicitArgs) { return new ConstructorResolver(this).autowireConstructor(beanName, mbd, ctors, explicitArgs); } public BeanWrapper autowireConstructor(String beanName, RootBeanDefinition mbd, @Nullable Constructor<?>[] chosenCtors, @Nullable Object[] explicitArgs) { for (Constructor<?> candidate : candidates) { // 获取参数的类型 Class<?>[] paramTypes = candidate.getParameterTypes(); // 获取依赖的bean argsHolder = createArgumentArray(beanName, mbd, resolvedValues, bw, paramTypes, paramNames..); // 调用instantiate方法进行实例化bean bw.setBeanInstance(instantiate(beanName, mbd, constructorToUse, argsToUse)); } }以上便是bean的实例化过程
applyMergedBeanDefinitionPostProcessors第三次主要是将标识了需要自动装配注解的属性或方法解析出来,包含的注解主要有 @Resource @Autowired @Value @Inject @PostConstruct @PreDestroy
protected void applyMergedBeanDefinitionPostProcessors(RootBeanDefinition mbd, Class<?> beanType, String beanName) { for (BeanPostProcessor bp : getBeanPostProcessors()) { if (bp instanceof MergedBeanDefinitionPostProcessor) { // CommonAnnotationBeanPostProcessor解析@PostConstruct @PreDestroy @Resource // AutowiredAnnotationBeanPostProcessor 解析@Autowired @Value @Inject MergedBeanDefinitionPostProcessor bdp = (MergedBeanDefinitionPostProcessor) bp; bdp.postProcessMergedBeanDefinition(mbd, beanType, beanName); } } }CommonAnnotationBeanPostProcessor#postProcessMergedBeanDefinition
public void postProcessMergedBeanDefinition(RootBeanDefinition beanDefinition, Class<?> beanType, String beanName) { // 父类为InitDestroyAnnotationBeanPostProcessor // 寻找@PostConstruct @PreDestroy注解的方法 // 用于bean的生命周期中初始化前的处理逻辑 super.postProcessMergedBeanDefinition(beanDefinition, beanType, beanName); // 寻找@Resource注解标识的属性或方法元数据 // 将这些元数据保存到缓存中,用于在属性装配阶段使用 InjectionMetadata metadata = findResourceMetadata(beanName, beanType, null); // 检查是否有重复的元数据,去重处理,如一个属性上既有@Autowired注解,又有@Resource注解 // 只使用一种方式进行注入,由于@Resource先进行解析,所以会选择@Resource的方式 metadata.checkConfigMembers(beanDefinition); }