【Spring源码分析】16-AutowiredAnnotationBeanPostProcessor

本文深入探讨Spring框架中依赖注入的工作原理,包括AutowireAnnotationBeanPostProcessor如何处理@Autowired注解,以及@Lookup注解的使用方式。同时,文章详细解释了候选构造方法的选择逻辑,并介绍了InjectionMetadata在依赖注入过程中扮演的角色。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

AutowiredAnnotationBeanPostProcessor继承了InstantiationAwareBeanPostProcessorAdapter间接实现了SmartInstantiationAwareBeanPostProcessor接口覆盖了determineCandidateConstructors()方法,此方法在实例化bean前选取一个构造方法数组,请参考bean创建的流程https://blog.youkuaiyun.com/shenchaohao12321/article/details/80441204https://blog.youkuaiyun.com/shenchaohao12321/article/details/80535708

先看determineCandidateConstructors()方法的前一小段是处理@Lookup注解的。

if (!this.lookupMethodsChecked.contains(beanName)) {
   try {
      ReflectionUtils.doWithMethods(beanClass, method -> {
         Lookup lookup = method.getAnnotation(Lookup.class);
         if (lookup != null) {
            Assert.state(beanFactory != null, "No BeanFactory available");
            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");
            }
         }
      });
   }
   catch (IllegalStateException ex) {
      throw new BeanCreationException(beanName, "Lookup method resolution failed", ex);
   }
   this.lookupMethodsChecked.add(beanName);
}

将beanClass被@Lookup标记的方法封装到LookupOverride中,通过mbd.getMethodOverrides().addOverride(override)加入到BeanDefinition中,后面会在AbstractAutowireCapableBeanFactory的instantiateBean()方法中,会使用CglibSubclassingInstantiationStrategy的instantiate()方法创建bean对象。

@Override
public Object instantiate(RootBeanDefinition bd, @Nullable String beanName, BeanFactory owner) {
   // Don't override the class with CGLIB if no overrides.
   if (!bd.hasMethodOverrides()) {
      Constructor<?> constructorToUse;
      synchronized (bd.constructorArgumentLock) {
         constructorToUse = (Constructor<?>) bd.resolvedConstructorOrFactoryMethod;
         if (constructorToUse == null) {
            final Class<?> clazz = bd.getBeanClass();
            if (clazz.isInterface()) {
               throw new BeanInstantiationException(clazz, "Specified class is an interface");
            }
            try {
               if (System.getSecurityManager() != null) {
                  constructorToUse = AccessController.doPrivileged(
                        (PrivilegedExceptionAction<Constructor<?>>) clazz::getDeclaredConstructor);
               }
               else {
                  constructorToUse = clazz.getDeclaredConstructor();
               }
               bd.resolvedConstructorOrFactoryMethod = constructorToUse;
            }
            catch (Throwable ex) {
               throw new BeanInstantiationException(clazz, "No default constructor found", ex);
            }
         }
      }
      return BeanUtils.instantiateClass(constructorToUse);
   }
   else {
      // Must generate CGLIB subclass.
      return instantiateWithMethodInjection(bd, beanName, owner);
   }
}

可以看到如果!bd.hasMethodOverrides()则走instantiateWithMethodInjection()方法,这个方法内使用了CglibSubclassingInstantiationStrategy内部类CglibSubclassCreator来创建cglib的代理对象。CglibSubclassCreator使用的callback拦截器就包含LookupOverrideMethodInterceptor。

private static class LookupOverrideMethodInterceptor extends CglibIdentitySupport implements MethodInterceptor {

   private final BeanFactory owner;

   public LookupOverrideMethodInterceptor(RootBeanDefinition beanDefinition, BeanFactory owner) {
      super(beanDefinition);
      this.owner = owner;
   }

   @Override
   public Object intercept(Object obj, Method method, Object[] args, MethodProxy mp) throws Throwable {
      // Cast is safe, as CallbackFilter filters are used selectively.
      LookupOverride lo = (LookupOverride) getBeanDefinition().getMethodOverrides().getOverride(method);
      Assert.state(lo != null, "LookupOverride not found");
      Object[] argsToUse = (args.length > 0 ? args : null);  // if no-arg, don't insist on args at all
      if (StringUtils.hasText(lo.getBeanName())) {
         return (argsToUse != null ? this.owner.getBean(lo.getBeanName(), argsToUse) :
               this.owner.getBean(lo.getBeanName()));
      }
      else {
         return (argsToUse != null ? this.owner.getBean(method.getReturnType(), argsToUse) :
               this.owner.getBean(method.getReturnType()));
      }
   }
}

如果调用的方法是被标记的,如果配置了@Lookup的value属性则从BeanFactory中取出bean name为@Lookup的value属性的bean对象返回,否则根据方法的返回类型从BeanFactory中取出相应的bean对象。

以上就是@Lookup的处理过程。

determineCandidateConstructors()方法的剩余部分就是挑选出候选的构造方法,从所有声明的构造方法选取带有@Autowired注解的,但是@Autowired的required==true的只能有一个,如果还有无参构造方法,再加上无参构造方法。如下:

// Quick check on the concurrent map first, with minimal locking.
Constructor<?>[] candidateConstructors = this.candidateConstructorsCache.get(beanClass);
if (candidateConstructors == null) {
   // Fully synchronized resolution now...
   synchronized (this.candidateConstructorsCache) {
      candidateConstructors = this.candidateConstructorsCache.get(beanClass);
      if (candidateConstructors == null) {
         Constructor<?>[] rawCandidates;
         try {
            rawCandidates = beanClass.getDeclaredConstructors();
         }
         catch (Throwable ex) {
            throw new BeanCreationException(beanName,
                  "Resolution of declared constructors on bean Class [" + beanClass.getName() +
                  "] from ClassLoader [" + beanClass.getClassLoader() + "] failed", ex);
         }
         List<Constructor<?>> candidates = new ArrayList<>(rawCandidates.length);
         Constructor<?> requiredConstructor = null;
         Constructor<?> defaultConstructor = null;
         Constructor<?> primaryConstructor = BeanUtils.findPrimaryConstructor(beanClass);
         int nonSyntheticConstructors = 0;
         for (Constructor<?> candidate : rawCandidates) {
            // 非合成的,用户自定义的构造方法都是非合成的,JVM可能会产生合成的因此过滤掉
            if (!candidate.isSynthetic()) {
               nonSyntheticConstructors++;
            }
            else if (primaryConstructor != null) {
               continue;
            }
            // 构造方法上所有注解属性
            AnnotationAttributes ann = findAutowiredAnnotation(candidate);
            // 子类没有的话可能父类构造方法存在(覆盖了)
            if (ann == null) {
               Class<?> userClass = ClassUtils.getUserClass(beanClass);
               if (userClass != beanClass) {
                  try {
                     Constructor<?> superCtor =
                           userClass.getDeclaredConstructor(candidate.getParameterTypes());
                     ann = findAutowiredAnnotation(superCtor);
                  }
                  catch (NoSuchMethodException ex) {
                     // Simply proceed, no equivalent superclass constructor found...
                  }
               }
            }
            if (ann != null) {
               // 只允许存在一个@Autowired(required=true)的构造方法
               if (requiredConstructor != null) {
                  throw new BeanCreationException(beanName,
                        "Invalid autowire-marked constructor: " + candidate +
                        ". Found constructor with 'required' Autowired annotation already: " +
                        requiredConstructor);
               }
               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.getParameterCount() == 0) {
               defaultConstructor = candidate;
            }
         }
         if (!candidates.isEmpty()) {
            // Add default constructor to list of optional constructors, as fallback.
            if (requiredConstructor == null) {
               // 如果不存在@Autowired构造方法,无惨构造方法没有@Autowired也作为候选
               if (defaultConstructor != null) {
                  candidates.add(defaultConstructor);
               }
               else if (candidates.size() == 1 && logger.isInfoEnabled()) {
                  logger.info("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<?>[0]);
         }
         // 只存在一个构造方法只能用它了
         else if (rawCandidates.length == 1 && rawCandidates[0].getParameterCount() > 0) {
            candidateConstructors = new Constructor<?>[] {rawCandidates[0]};
         }
         // 如果只有两个构造方法,一个是带有@Primary一个是无参不带注解的,则这两个都作为候选构造方法
         else if (nonSyntheticConstructors == 2 && primaryConstructor != null &&
               defaultConstructor != null && !primaryConstructor.equals(defaultConstructor)) {
            candidateConstructors = new Constructor<?>[] {primaryConstructor, defaultConstructor};
         }
         // 只有一个@Primary构造方法
         else if (nonSyntheticConstructors == 1 && primaryConstructor != null) {
            candidateConstructors = new Constructor<?>[] {primaryConstructor};
         }
         else {
            candidateConstructors = new Constructor<?>[0];
         }
         this.candidateConstructorsCache.put(beanClass, candidateConstructors);
      }
   }
}
return (candidateConstructors.length > 0 ? candidateConstructors : null);

一个bean实例创建之后但此时还没有调用populateBean()方法完成属性的填充时,会经历MergedBeanDefinitionPostProcessor的postProcessMergedBeanDefinition()方法。

@Override
public void postProcessMergedBeanDefinition(RootBeanDefinition beanDefinition, Class<?> beanType, String beanName) {
   // InjectionMetadata可对指定对象进行注入(字段或方法)
   InjectionMetadata metadata = findAutowiringMetadata(beanName, beanType, null);
   metadata.checkConfigMembers(beanDefinition);
}
private InjectionMetadata findAutowiringMetadata(String beanName, Class<?> clazz, @Nullable PropertyValues pvs) {
   // Fall back to class name as cache key, for backwards compatibility with custom callers.
   String cacheKey = (StringUtils.hasLength(beanName) ? beanName : clazz.getName());
   // Quick check on the concurrent map first, with minimal locking.
   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);
            }
            // 构建由@Autowired、@Value、@Inject注解的字段或方法的InjectionMetadata
            metadata = buildAutowiringMetadata(clazz);
            this.injectionMetadataCache.put(cacheKey, metadata);
         }
      }
   }
   return metadata;
}

injectionMetadataCache是一个Map对象,缓存beanName与InjectionMetadata。InjectionMetadata对象通过buildAutowiringMetadata()方法来创建的。

private InjectionMetadata buildAutowiringMetadata(final Class<?> clazz) {
   LinkedList<InjectionMetadata.InjectedElement> elements = new LinkedList<>();
   Class<?> targetClass = clazz;
   do {
      final LinkedList<InjectionMetadata.InjectedElement> currElements = new LinkedList<>();
      ReflectionUtils.doWithLocalFields(targetClass, field -> {
         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);
               }
               return;
            }
            boolean required = determineRequiredStatus(ann);
            // 保存带有自动装配注解的非静态字段
            currElements.add(new AutowiredFieldElement(field, required));
         }
      });

      ReflectionUtils.doWithLocalMethods(targetClass, method -> {
         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())) {
               if (logger.isWarnEnabled()) {
                  logger.warn("Autowired annotation is not supported on static methods: " + method);
               }
               return;
            }
            if (method.getParameterCount() == 0) {
               if (logger.isWarnEnabled()) {
                  logger.warn("Autowired annotation should only be used on methods with 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);
}

buildAutowiringMetadata()方法分为对字段与方法的@Autowired(@Value,@Inject)注解处理。将标记@Autowired的费静态字段封装为AutowiredFieldElement对象,将标记@Autowired的非静态方法封装到AutowiredMethodElement对象然后都加入到InjectionMetadata中,此过程是一个循环过程此类处理完会沿着父类继续向上处理。AutowiredFieldElement与AutowiredMethodElement都是InjectionMetadata.InjectedElement的子类,都覆盖了父类的inject()方法这个下面会介绍。

通过findAutowiringMetadata()方法得到了InjectionMetadata对象之后调用了给对象的checkConfigMembers()方法。这个方法就是将上面保存的InjectedElement对象筛选出那些不存在于beanDefinition中的赋值给checkedElements。

public void checkConfigMembers(RootBeanDefinition beanDefinition) {
   Set<InjectedElement> checkedElements = new LinkedHashSet<>(this.injectedElements.size());
   for (InjectedElement element : this.injectedElements) {
      Member member = element.getMember();
      if (!beanDefinition.isExternallyManagedConfigMember(member)) {
         beanDefinition.registerExternallyManagedConfigMember(member);
         checkedElements.add(element);
         if (logger.isDebugEnabled()) {
            logger.debug("Registered injected element on class [" + this.targetClass.getName() + "]: " + element);
         }
      }
   }
   this.checkedElements = checkedElements;
}

到这为止就是postProcessMergedBeanDefinition()方法的全部实现逻辑并没有做什么实际事情,其实是为了后面的行为做铺垫,请看postProcessPropertyValues()方法(在Spring5.1推荐使用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;
}

@Deprecated
@Override
public PropertyValues postProcessPropertyValues(
      PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName) {

   return postProcessProperties(pvs, bean, beanName);
}

首先调用 findAutowiringMetadata()方法从缓存中再次取到InjectionMetadata对象,调用inject()方法完成autowired注入。

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()) {
      boolean debug = logger.isDebugEnabled();
      for (InjectedElement element : elementsToIterate) {
         if (debug) {
            logger.debug("Processing injected element of bean '" + beanName + "': " + element);
         }
         element.inject(target, beanName, pvs);
      }
   }
}

因为之前已经把类@Autowired注解标记的字段或方法封装到了响应的InjectedElement子类中了,此时依次调用子类的inject()方法完成对象的注入,下面分别看一看这两种注入的实现。

AutowiredFieldElement,调用BeanFactory的resolveDependency()方法返回一个符合字段类型的bean对象设置到此bean上。

@Override
protected void inject(Object bean, @Nullable String beanName, @Nullable PropertyValues pvs) throws Throwable {
   Field field = (Field) this.member;
   Object value;
   // 解析过一次会缓存cachedFieldValue
   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<>(1);
      Assert.state(beanFactory != null, "No BeanFactory available");
      TypeConverter typeConverter = beanFactory.getTypeConverter();
      try {
         value = beanFactory.resolveDependency(desc, beanName, autowiredBeanNames, typeConverter);
      }
      catch (BeansException ex) {
         throw new UnsatisfiedDependencyException(null, beanName, new InjectionPoint(field), ex);
      }
      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) &&
                        beanFactory.isTypeMatch(autowiredBeanName, field.getType())) {
                     // 以后注入就可以直接使用这个带有beanname的DependencyDescriptor更高效
                     this.cachedFieldValue = new ShortcutDependencyDescriptor(
                           desc, autowiredBeanName, field.getType());
                  }
               }
            }
            else {
               this.cachedFieldValue = null;
            }
            this.cached = true;
         }
      }
   }
   // 字段赋值
   if (value != null) {
      ReflectionUtils.makeAccessible(field);
      field.set(bean, value);
   }
}

AutowiredMethodElement,类似通过setter方法完成属性的注入。

@Override
protected void inject(Object bean, @Nullable String beanName, @Nullable PropertyValues pvs) throws Throwable {
   if (checkPropertySkipping(pvs)) {
      return;
   }
   Method method = (Method) this.member;
   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> autowiredBeans = new LinkedHashSet<>(paramTypes.length);
      Assert.state(beanFactory != null, "No BeanFactory available");
      TypeConverter typeConverter = beanFactory.getTypeConverter();
      for (int i = 0; i < arguments.length; i++) {
         MethodParameter methodParam = new MethodParameter(method, i);
         DependencyDescriptor currDesc = new DependencyDescriptor(methodParam, this.required);
         currDesc.setContainingClass(bean.getClass());
         descriptors[i] = currDesc;
         try {
            Object arg = beanFactory.resolveDependency(currDesc, beanName, autowiredBeans, typeConverter);
            if (arg == null && !this.required) {
               arguments = null;
               break;
            }
            arguments[i] = arg;
         }
         catch (BeansException ex) {
            throw new UnsatisfiedDependencyException(null, beanName, new InjectionPoint(methodParam), ex);
         }
      }
      synchronized (this) {
         if (!this.cached) {
            if (arguments != null) {
               Object[] cachedMethodArguments = new Object[paramTypes.length];
               System.arraycopy(descriptors, 0, cachedMethodArguments, 0, arguments.length);
               registerDependentBeans(beanName, autowiredBeans);
               if (autowiredBeans.size() == paramTypes.length) {
                  Iterator<String> it = autowiredBeans.iterator();
                  for (int i = 0; i < paramTypes.length; i++) {
                     String autowiredBeanName = it.next();
                     if (beanFactory.containsBean(autowiredBeanName) &&
                           beanFactory.isTypeMatch(autowiredBeanName, paramTypes[i])) {
                        cachedMethodArguments[i] = new ShortcutDependencyDescriptor(
                              descriptors[i], autowiredBeanName, paramTypes[i]);
                     }
                  }
               }
               this.cachedMethodArguments = cachedMethodArguments;
            }
            else {
               this.cachedMethodArguments = null;
            }
            this.cached = true;
         }
      }
   }
   if (arguments != null) {
      try {
         ReflectionUtils.makeAccessible(method);
         method.invoke(bean, arguments);
      }
      catch (InvocationTargetException ex){
         throw ex.getTargetException();
      }
   }
}

<think>嗯,用户想让我带他学习Spring源码。好的,首先我需要确定用户的基础。他可能已经有一定的Java和Spring使用经验,现在想深入源码理解内部机制。不过作为刚开始学习源码的人,可能会觉得Spring庞大的代码库有点难以入手。我需要给他一个结构化的学习路径,从基础开始逐步深入。 首先,我应该推荐他先掌握Spring的核心概念,比如IoC和AOP。因为这些是Spring框架的基础,理解了这些概念后再看源码会更清晰。然后,建议他熟悉设计模式,因为Spring中大量使用了工厂、代理、模板方法等模式。比如BeanFactory就是工厂模式的应用,AOP用了代理模式,这些都是关键点。 接下来,源码环境的搭建是必须的。用户需要知道如何下载Spring的源码,用Gradle构建,并导入到IDE中。可能还需要提到调试技巧,比如如何设置断点,跟踪Bean的创建过程,这样能更直观地理解流程。 然后,分模块学习会比较有效。从spring-beans和spring-context开始,这两个模块是IoC的核心。Bean的生命周期、BeanFactory和ApplicationContext的区别,这些都是重点。用户可能需要了解BeanDefinition的加载过程,BeanPostProcessor的作用,以及依赖注入的实现机制。 AOP部分在spring-aop和spring-aspects模块,需要解释动态代理的实现,JDK动态代理和CGLIB的区别,以及如何通过AspectJ集成实现切面编程。 在Web部分,spring-web和spring-webmvc是重点,DispatcherServlet的处理流程、HandlerMapping和ViewResolver的作用,这些都是MVC的核心。用户可能需要了解请求是如何被处理的,拦截器如何工作,以及RestTemplate和新的WebClient的使用。 响应式编程方面,Spring WebFlux是一个重要的模块,需要讲解Reactor库和响应式编程模型,对比传统的Servlet模型,帮助用户理解非阻塞处理的好处。 源码阅读技巧也很重要,比如如何找到入口类,使用调用栈分析,日志输出和单元测试辅助理解。这些方法能帮助用户更高效地阅读代码。 最后,实践和参考资源是必不可少的。建议用户自己写示例,改造简化版框架,参与社区讨论,查阅官方文档和书籍,比如《Spring源码深度解析》。 现在,我需要把这些思路整理成一个结构化的回答,分步骤讲解,并给出示例代码,比如BeanPostProcessor的使用,AOP的配置,MVC的控制器示例,以及WebFlux的简单应用。这样用户可以通过实际代码加深理解。 相关问题部分,要涵盖IoC、AOP、设计模式、Bean生命周期、动态代理、WebFlux对比Servlet、BeanFactory和ApplicationContext的区别,以及循环依赖的处理。这些问题能帮助用户进一步巩固知识点,并引导他继续深入学习。</think><< **Spring源码学习路径及核心要点分析** **一、学习准备** 1. **前置知识要求** - 熟练掌握Java反射、动态代理、注解等核心机制 - 理解设计模式(工厂、代理、模板方法、观察者等) - 熟悉Spring基础使用(XML/注解配置) 2. **源码获取与调试** ```bash git clone https://github.com/spring-projects/spring-framework.git gradlew :spring-oxm:compileTestJava # 编译关键模块 ``` - 推荐使用IntelliJ IDEA进行源码分析 - 开启"Build project automatically"和"Compile independent modules in parallel" **二、核心模块分析路线** 1. **IoC容器(spring-beans、spring-context)** - 关键入口:`AbstractApplicationContext.refresh()` - Bean生命周期跟踪示例: ```java public class MyBeanPostProcessor implements BeanPostProcessor { @Override public Object postProcessBeforeInitialization(Object bean, String beanName) { System.out.println("Before init: " + beanName); return bean; } } ``` 2. **AOP实现(spring-aop、spring-aspects)** - 代理创建过程:`DefaultAopProxyFactory` - 示例AOP配置: ```java @Aspect public class LoggingAspect { @Before("execution(* com.example.service.*.*(..))") public void logBefore(JoinPoint joinPoint) { System.out.println("Method called: " + joinPoint.getSignature()); } } ``` 3. **事务管理(spring-tx)** - 核心类:`PlatformTransactionManager` - 事务拦截器:`TransactionInterceptor` **三、核心设计模式解析** 1. 工厂模式:`BeanFactory` 2. 代理模式:`JdkDynamicAopProxy` 3. 模板方法:`JdbcTemplate` 4. 观察者模式:`ApplicationEvent`机制 **四、关键流程深度分析** 1. **Bean创建流程** `BeanDefinition` → 实例化 → 属性填充 → 初始化 → 销毁 2. **依赖注入实现** - 字段注入:`AutowiredAnnotationBeanPostProcessor` - 构造器注入:`ConstructorResolver` 3. **循环依赖解决** - 三级缓存机制: - singletonObjects(一级) - earlySingletonObjects(二级) - singletonFactories(三级) **五、调试技巧** 1. 关键断点设置: - `DefaultListableBeanFactory.preInstantiateSingletons()` - `AbstractAutowireCapableBeanFactory.createBean()` 2. 调试配置示例: ```java public class SourceDebug { public static void main(String[] args) { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class); MyService service = context.getBean(MyService.class); service.execute(); } } ``` **六、学习建议** 1. 从具体功能点切入(如@Autowired实现) 2. 结合官方文档(spring.io)对照阅读 3. 使用UML工具绘制核心类图 4. 参与Spring项目Issues讨论 **示例源码解析(Bean初始化):** ```java // AbstractApplicationContext.java public void refresh() { // 1. 准备刷新上下文 prepareRefresh(); // 2. 初始化BeanFactory ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory(); // 3. BeanFactory预处理 prepareBeanFactory(beanFactory); // 4. 后置处理BeanFactory postProcessBeanFactory(beanFactory); // 5. 调用BeanFactoryPostProcessor invokeBeanFactoryPostProcessors(beanFactory); // 6. 注册BeanPostProcessor registerBeanPostProcessors(beanFactory); // 7. 初始化消息源 initMessageSource(); // 8. 初始化事件广播器 initApplicationEventMulticaster(); // 9. 子类扩展点 onRefresh(); // 10. 注册监听器 registerListeners(); // 11. 完成Bean初始化 finishBeanFactoryInitialization(beanFactory); // 12. 完成刷新 finishRefresh(); } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值