diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessor.java b/spring-beans/src/main/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessor.java index 53d36462bb5785ba8cc82fc52b8d6065400cd74b..623b2e1bdc07969bcdd289bc8c1e720acec44157 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessor.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessor.java @@ -294,7 +294,7 @@ public class AutowiredAnnotationBeanPostProcessor extends InstantiationAwareBean if (!candidate.isSynthetic()) { nonSyntheticConstructors++; } - else if (primaryConstructor != null) { + else if (primaryConstructor != null) { // 针对于 kotlin continue; } AnnotationAttributes ann = findAutowiredAnnotation(candidate); @@ -442,6 +442,7 @@ public class AutowiredAnnotationBeanPostProcessor extends InstantiationAwareBean final List currElements = new ArrayList<>(); ReflectionUtils.doWithLocalFields(targetClass, field -> { + // 加载@Autowired,@Value AnnotationAttributes ann = findAutowiredAnnotation(field); if (ann != null) { if (Modifier.isStatic(field.getModifiers())) { @@ -490,7 +491,8 @@ public class AutowiredAnnotationBeanPostProcessor extends InstantiationAwareBean @Nullable private AnnotationAttributes findAutowiredAnnotation(AccessibleObject ao) { - if (ao.getAnnotations().length > 0) { // autowiring annotations have to be local + if (ao.getAnnotations().length > 0) { + // autowiring annotations have to be local for (Class type : this.autowiredAnnotationTypes) { AnnotationAttributes attributes = AnnotatedElementUtils.getMergedAnnotationAttributes(ao, type); if (attributes != null) { @@ -507,7 +509,7 @@ public class AutowiredAnnotationBeanPostProcessor extends InstantiationAwareBean * are found. Otherwise, the autowiring process will simply bypass the field * or method when no beans are found. * @param ann the Autowired annotation - * @return whether the annotation indicates that a dependency is required + * @return whether the annotation indicates that a dependency is required, true is required, and false is not required. */ protected boolean determineRequiredStatus(AnnotationAttributes ann) { return (!ann.containsKey(this.requiredParameterName) || diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractAutowireCapableBeanFactory.java b/spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractAutowireCapableBeanFactory.java index 3a6aa8867233c0407cab1c003ab9044b9c20ca3b..8bc58e7768ff8354b0b53255fe6c5383424428e0 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractAutowireCapableBeanFactory.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractAutowireCapableBeanFactory.java @@ -500,6 +500,7 @@ public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFac try { // Give BeanPostProcessors a chance to return a proxy instead of the target bean instance. + // 针对实现InstantiationAwareBeanPostProcessor接口的代理类进行操作 Object bean = resolveBeforeInstantiation(beanName, mbdToUse); if (bean != null) { return bean; @@ -575,7 +576,7 @@ public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFac // Eagerly cache singletons to be able to resolve circular references // even when triggered by lifecycle interfaces like BeanFactoryAware. - // 提前暴露对象 + // 提前暴露对象; 是单例模式,允许循环依赖,当前bean正在创建 boolean earlySingletonExposure = (mbd.isSingleton() && this.allowCircularReferences && isSingletonCurrentlyInCreation(beanName)); if (earlySingletonExposure) { @@ -583,13 +584,17 @@ public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFac logger.trace("Eagerly caching bean '" + beanName + "' to allow for resolving potential circular references"); } - addSingletonFactory(beanName, () -> getEarlyBeanReference(beanName, mbd, bean)); // 加入三级缓存 + // 将当前bean加入到三级缓存当中 + // getEarlyBeanReference(beanName, mbd, bean) 针对于代理对象处理 + addSingletonFactory(beanName, () -> getEarlyBeanReference(beanName, mbd, bean)); } - // Initialize the bean instance. + // Initialize the bean instance. 初始化bean对象 Object exposedObject = bean; try { - populateBean(beanName, mbd, instanceWrapper); // 填充属性 + // 填充属性 + populateBean(beanName, mbd, instanceWrapper); + // 初始化bean exposedObject = initializeBean(beanName, exposedObject, mbd); } catch (Throwable ex) { @@ -940,7 +945,8 @@ public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFac */ protected Object getEarlyBeanReference(String beanName, RootBeanDefinition mbd, Object bean) { Object exposedObject = bean; - if (!mbd.isSynthetic() && hasInstantiationAwareBeanPostProcessors()) { // aop + // hasInstantiationAwareBeanPostProcessors表名,是否实现有InstantiationAwareBeanPostProcessor接口注册,spring aop代理就是实现了InstantiationAwareBeanPostProcessor接口. DefaultAdvisorAutoProxyCreator + if (!mbd.isSynthetic() && hasInstantiationAwareBeanPostProcessors()) { for (BeanPostProcessor bp : getBeanPostProcessors()) { if (bp instanceof SmartInstantiationAwareBeanPostProcessor) { SmartInstantiationAwareBeanPostProcessor ibp = (SmartInstantiationAwareBeanPostProcessor) bp; @@ -1093,8 +1099,10 @@ public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFac if (!mbd.isSynthetic() && hasInstantiationAwareBeanPostProcessors()) { Class targetType = determineTargetType(beanName, mbd); if (targetType != null) { + // 执行前置“实例化”方法,获取代理对象 bean = applyBeanPostProcessorsBeforeInstantiation(targetType, beanName); if (bean != null) { + // 执行后置“初始化”方法 bean = applyBeanPostProcessorsAfterInitialization(bean, beanName); } } @@ -1375,7 +1383,7 @@ public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFac } PropertyValues pvs = (mbd.hasPropertyValues() ? mbd.getPropertyValues() : null); - + // 解析@Autowried注解 int resolvedAutowireMode = mbd.getResolvedAutowireMode(); if (resolvedAutowireMode == AUTOWIRE_BY_NAME || resolvedAutowireMode == AUTOWIRE_BY_TYPE) { MutablePropertyValues newPvs = new MutablePropertyValues(pvs); @@ -1401,6 +1409,7 @@ public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFac for (BeanPostProcessor bp : getBeanPostProcessors()) { if (bp instanceof InstantiationAwareBeanPostProcessor) { InstantiationAwareBeanPostProcessor ibp = (InstantiationAwareBeanPostProcessor) bp; + // 获取@Autowired相关propertyValue,(AutowiredAnnotationBeanPostProcessor) PropertyValues pvsToUse = ibp.postProcessProperties(pvs, bw.getWrappedInstance(), beanName); if (pvsToUse == null) { if (filteredPds == null) { @@ -1760,10 +1769,12 @@ public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFac Object wrappedBean = bean; if (mbd == null || !mbd.isSynthetic()) { + // 执行初始化前置增强器 wrappedBean = applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName); } try { + // 执行初始化方法 invokeInitMethods(beanName, wrappedBean, mbd); } catch (Throwable ex) { @@ -1772,12 +1783,19 @@ public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFac beanName, "Invocation of init method failed", ex); } if (mbd == null || !mbd.isSynthetic()) { + // 执行初始化后置增强器 wrappedBean = applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName); } return wrappedBean; } + /** + * 设置xxxAware值 + * BeanNameAware, BeanClassLoaderAware, BeanFactoryAware + * @param beanName + * @param bean + */ private void invokeAwareMethods(String beanName, Object bean) { if (bean instanceof Aware) { if (bean instanceof BeanNameAware) { diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/support/DefaultSingletonBeanRegistry.java b/spring-beans/src/main/java/org/springframework/beans/factory/support/DefaultSingletonBeanRegistry.java index d5cb54e582ec82a3b008b42392fa717da1916805..a8795378d70a4acfc95a388ca0805d8322df6443 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/support/DefaultSingletonBeanRegistry.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/support/DefaultSingletonBeanRegistry.java @@ -154,8 +154,11 @@ public class DefaultSingletonBeanRegistry extends SimpleAliasRegistry implements protected void addSingletonFactory(String beanName, ObjectFactory singletonFactory) { Assert.notNull(singletonFactory, "Singleton factory must not be null"); synchronized (this.singletonObjects) { + // singletonObjects一级缓存 if (!this.singletonObjects.containsKey(beanName)) { + // 三级缓存 this.singletonFactories.put(beanName, singletonFactory); + // 二级缓存 this.earlySingletonObjects.remove(beanName); this.registeredSingletons.add(beanName); } @@ -344,6 +347,7 @@ public class DefaultSingletonBeanRegistry extends SimpleAliasRegistry implements * Return whether the specified singleton bean is currently in creation * (within the entire factory). * @param beanName the name of the bean + * @return true:in creation; false: not creation */ public boolean isSingletonCurrentlyInCreation(String beanName) { return this.singletonsCurrentlyInCreation.contains(beanName); diff --git a/spring-context/src/main/java/org/springframework/context/support/ApplicationContextAwareProcessor.java b/spring-context/src/main/java/org/springframework/context/support/ApplicationContextAwareProcessor.java index 563803cd090aec231fcc6092f82e948e866e5d94..ea5b66056a5e1e9dc2ddbf94dfe36bd4352f0d04 100644 --- a/spring-context/src/main/java/org/springframework/context/support/ApplicationContextAwareProcessor.java +++ b/spring-context/src/main/java/org/springframework/context/support/ApplicationContextAwareProcessor.java @@ -58,6 +58,7 @@ import org.springframework.util.StringValueResolver; * @see org.springframework.context.ApplicationContextAware * @see org.springframework.context.support.AbstractApplicationContext#refresh() */ +// 通过BeanPostProcessor前置增强器,让用户获取ApplicationContext对象 class ApplicationContextAwareProcessor implements BeanPostProcessor { private final ConfigurableApplicationContext applicationContext; diff --git a/spring-context/src/main/java/org/springframework/context/support/PostProcessorRegistrationDelegate.java b/spring-context/src/main/java/org/springframework/context/support/PostProcessorRegistrationDelegate.java index a5ac40fe696e3f8131e9e26dd06bc72f91a3e319..7ba80df403aac4e6d352af30aae0e351cfd26e88 100644 --- a/spring-context/src/main/java/org/springframework/context/support/PostProcessorRegistrationDelegate.java +++ b/spring-context/src/main/java/org/springframework/context/support/PostProcessorRegistrationDelegate.java @@ -193,6 +193,7 @@ final class PostProcessorRegistrationDelegate { // Register BeanPostProcessorChecker that logs an info message when // a bean is created during BeanPostProcessor instantiation, i.e. when // a bean is not eligible for getting processed by all BeanPostProcessors. + // 获取自定义BeanPostProcessor int beanProcessorTargetCount = beanFactory.getBeanPostProcessorCount() + 1 + postProcessorNames.length; beanFactory.addBeanPostProcessor(new BeanPostProcessorChecker(beanFactory, beanProcessorTargetCount)); diff --git a/spring-core/src/main/java/org/springframework/core/PriorityOrdered.java b/spring-core/src/main/java/org/springframework/core/PriorityOrdered.java index 2090cd35f9cf189be19d2c169a77aa5466b317e7..c68a0c00b43cbbdfe2cc9bcec65560688840550a 100644 --- a/spring-core/src/main/java/org/springframework/core/PriorityOrdered.java +++ b/spring-core/src/main/java/org/springframework/core/PriorityOrdered.java @@ -44,5 +44,6 @@ package org.springframework.core; * @see org.springframework.beans.factory.config.PropertyOverrideConfigurer * @see org.springframework.beans.factory.config.PropertyPlaceholderConfigurer */ +// 优先级标记接口 public interface PriorityOrdered extends Ordered { } diff --git a/spring-core/src/main/java/org/springframework/core/io/InputStreamSource.java b/spring-core/src/main/java/org/springframework/core/io/InputStreamSource.java index b769895ece7772ca1b6ca51780d4ada4bfe291c8..c79c86461280d808fc78a6a8bab810d2da883f86 100644 --- a/spring-core/src/main/java/org/springframework/core/io/InputStreamSource.java +++ b/spring-core/src/main/java/org/springframework/core/io/InputStreamSource.java @@ -41,6 +41,8 @@ import java.io.InputStream; public interface InputStreamSource { /** + * 解析文件流,解析url,file,classpath + * * Return an {@link InputStream} for the content of an underlying resource. *

It is expected that each call creates a fresh stream. *

This requirement is particularly important when you consider an API such diff --git a/spring-core/src/main/java/org/springframework/core/io/Resource.java b/spring-core/src/main/java/org/springframework/core/io/Resource.java index 1995ee783e6d7e2ca6469b05ef8e0831462ccfe6..372e0169347314a0a848c9e2538a9cb3d8e8bbc7 100644 --- a/spring-core/src/main/java/org/springframework/core/io/Resource.java +++ b/spring-core/src/main/java/org/springframework/core/io/Resource.java @@ -52,6 +52,8 @@ import org.springframework.lang.Nullable; public interface Resource extends InputStreamSource { /** + * 检查资源是否存在 + * * Determine whether this resource actually exists in physical form. *

This method performs a definitive existence check, whereas the * existence of a {@code Resource} handle only guarantees a valid @@ -60,6 +62,8 @@ public interface Resource extends InputStreamSource { boolean exists(); /** + * 表名资源是否可读, 是否可以调用{@link org.springframework.core.io.InputStreamSource#getInputStream()}该方法 + * * Indicate whether non-empty contents of this resource can be read via * {@link #getInputStream()}. *

Will be {@code true} for typical resource descriptors that exist diff --git a/spring-core/src/main/java/org/springframework/core/io/ResourceLoader.java b/spring-core/src/main/java/org/springframework/core/io/ResourceLoader.java index 9a19fda7dd6b646c99c3b8872ac896ad6935f084..2b97874fa8c10791ca67e6574cc330968c873139 100644 --- a/spring-core/src/main/java/org/springframework/core/io/ResourceLoader.java +++ b/spring-core/src/main/java/org/springframework/core/io/ResourceLoader.java @@ -67,6 +67,8 @@ public interface ResourceLoader { Resource getResource(String location); /** + * 获取资源类加载器 + * * Expose the ClassLoader used by this ResourceLoader. *

Clients which need to access the ClassLoader directly can do so * in a uniform manner with the ResourceLoader, rather than relying diff --git a/spring-core/src/main/java/org/springframework/util/ClassUtils.java b/spring-core/src/main/java/org/springframework/util/ClassUtils.java index 62d7d07151d96bdde7f6f04309901ff6f0327aae..0b3cb4160b796decb42024c9fa82188449dc31e6 100644 --- a/spring-core/src/main/java/org/springframework/util/ClassUtils.java +++ b/spring-core/src/main/java/org/springframework/util/ClassUtils.java @@ -167,6 +167,9 @@ public abstract class ClassUtils { } /** + * 返回一个classLoader,先去获取加载当前线程的classLoader,如果为空,返回ClassUtils的ClassLoader,如果还为空,则返回系统类加载器,否则返回null + * + *

* Return the default ClassLoader to use: typically the thread context * ClassLoader, if available; the ClassLoader that loaded the ClassUtils * class will be used as fallback. diff --git a/spring-demo/src/main/java/com/czf/transactional/TransactionalDemo.java b/spring-demo/src/main/java/com/czf/transactional/TransactionalDemo.java new file mode 100644 index 0000000000000000000000000000000000000000..b1460b9950479335b8c28e82c83ceda2e4a3999a --- /dev/null +++ b/spring-demo/src/main/java/com/czf/transactional/TransactionalDemo.java @@ -0,0 +1,17 @@ +package com.czf.transactional; + +import org.springframework.context.ApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; + +/** + * @Author: Scott + * @Date: 2021/6/22 21:57 + */ +public class TransactionalDemo { + + public static void main(String[] args) { + ApplicationContext applicationContext = new ClassPathXmlApplicationContext("application-transaction.xml"); + applicationContext.getBean(""); + } + +} diff --git a/spring-demo/src/main/resources/application-transactional.xml b/spring-demo/src/main/resources/application-transactional.xml new file mode 100644 index 0000000000000000000000000000000000000000..ee77722f42eccbd77db765d4d3608f0998963099 --- /dev/null +++ b/spring-demo/src/main/resources/application-transactional.xml @@ -0,0 +1,8 @@ + + + +