Commit 94e1e9e0 by SunWei峰

IOCAOP融汇贯通

parent 7f8f7bc7
......@@ -141,6 +141,7 @@ public abstract class AbstractAutoProxyCreator extends ProxyProcessorSupport
private final Map<Object, Class<?>> proxyTypes = new ConcurrentHashMap<>(16);
// TODO: 2022/8/19 before方法中用到,AOP中见到要联想到
private final Map<Object, Boolean> advisedBeans = new ConcurrentHashMap<>(256);
......@@ -292,7 +293,7 @@ public abstract class AbstractAutoProxyCreator extends ProxyProcessorSupport
// 根据给定的 bean 的 class 和 name 构建出一个key 格式是 beanClassName_beanName
Object cacheKey = getCacheKey(bean.getClass(), beanName);
if (this.earlyProxyReferences.remove(cacheKey) != bean) {
// 如果他适合做代理,则需要封装指定Bean
// 不相等说明之前没有put过,就需要封装指定Bean,
return wrapIfNecessary(bean, beanName, cacheKey);
}
}
......
......@@ -570,7 +570,7 @@ public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFac
// 单例情况下清除缓存。这里保存的是 FactoryBean 和 BeanWrapper 的映射关系。
// factoryBeanInstanceCache 是在创建其他bean的时候缓存了一下 FactoryBean。
if (mbd.isSingleton()) {
// TODO: 2022/8/3 什么时候会加缓存?不是很理解这个东西的作用
// FactoryBean 的beanName -> FactoryBean 的 BeanWrapper
instanceWrapper = this.factoryBeanInstanceCache.remove(beanName);
}
// 如果没有缓存,则重新创建
......@@ -638,12 +638,15 @@ public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFac
if (earlySingletonExposure) {
Object earlySingletonReference = getSingleton(beanName, false);
// 只有在检测到循环依赖的情况下才会不为空
// 前面属性都已经装配完毕,自己出现在了二级缓存中,一定是循环依赖的时候,被从三级缓存中拿到了二级缓存中
if (earlySingletonReference != null) {
// 如果传入的对象等于原始对象,那么直接把aop对象向原始对象赋值
// 若exposedObject在增强方法中没有被改变。(没有增强)
// 如果缓存的对象 exposedObject == bean,直接赋值之前暴露出来的bean
if (exposedObject == bean) {
exposedObject = earlySingletonReference;
}
// 如果提前暴露的对象和经过了完整的生命周期后的对象不相等
else if (!this.allowRawInjectionDespiteWrapping && hasDependentBean(beanName)) {
String[] dependentBeans = getDependentBeans(beanName);
Set<String> actualDependentBeans = new LinkedHashSet<>(dependentBeans.length);
......@@ -1204,7 +1207,9 @@ public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFac
// 但是 bean 的构造函数可能有多个,需要确定使用哪一个。
// 这里实际上是一个缓存,resolved 表示构造函数是否已经解析完成;autowireNecessary 表示是否需要自动装配
// Shortcut when re-creating the same bean...
// 是否已经完成解析
boolean resolved = false;
// 标志构造函数参数是否解析完成
boolean autowireNecessary = false;
if (args == null) {
synchronized (mbd.constructorArgumentLock) {
......@@ -1416,7 +1421,6 @@ public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFac
*/
@SuppressWarnings("deprecation") // for postProcessPropertyValues
protected void populateBean(String beanName, RootBeanDefinition mbd, @Nullable BeanWrapper bw) {
// 标记了@Autowired
if (bw == null) {
if (mbd.hasPropertyValues()) {
throw new BeanCreationException(
......@@ -1431,8 +1435,8 @@ public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFac
// Give any InstantiationAwareBeanPostProcessors the opportunity to modify the
// state of the bean before properties are set. This can be used, for example,
// to support styles of field injection.
// 给InstantiationAwareBeanPostProcessors最后一次机会在属性注入前修改Bean的属性值
// 具体通过调用postProcessAfterInstantiation方法,如果调用返回false,表示不必继续进行依赖注入,直接返回
// 给 InstantiationAwareBeanPostProcessors 最后一次机会在属性注入前修改Bean的属性值
// 具体通过调用 postProcessAfterInstantiation 方法,如果调用返回false,表示不必继续进行依赖注入,直接返回
if (!mbd.isSynthetic() && hasInstantiationAwareBeanPostProcessors()) {
for (InstantiationAwareBeanPostProcessor bp : getBeanPostProcessorCache().instantiationAware) {
// 返回值为是否继续填充bean
......@@ -1469,7 +1473,7 @@ public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFac
pvs = mbd.getPropertyValues();
}
// 3. 成员变量的注入
// 调用了InstantiationAwareBeanPostProcessor.postProcessProperties 方法
// 调用了 InstantiationAwareBeanPostProcessor.postProcessProperties 方法
// 和 postProcessPropertyValues 方法 来进行设值后处理
for (InstantiationAwareBeanPostProcessor bp : getBeanPostProcessorCache().instantiationAware) {
PropertyValues pvsToUse = bp.postProcessProperties(pvs, bw.getWrappedInstance(), beanName);
......@@ -1565,7 +1569,7 @@ public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFac
// Do not allow eager init for type matching in case of a prioritized post-processor.
boolean eager = !(bw.getWrappedInstance() instanceof PriorityOrdered);
DependencyDescriptor desc = new AutowireByTypeDependencyDescriptor(methodParam, eager);
// 解析指定beanName 的属性所匹配的值,并把解析到的属性名存储在 autowiredBeanNames 中
// 解析指定 beanName 的属性所匹配的值,并把解析到的属性名存储在 autowiredBeanNames 中
// 当属性存在多个封装bean时,如 @Autowired List<Bean> beans,会找到所有的匹配Bean 类型的bean并将其注入。
// 这里的返回值是真正的需要注入的属性, autowiredBeanNames 是需要注入的属性(可能是集合)的names
Object autowiredArgument = resolveDependency(desc, beanName, autowiredBeanNames, converter);
......
......@@ -265,7 +265,7 @@ public abstract class AbstractBeanFactory extends FactoryBeanRegistrySupport imp
logger.trace("Returning cached instance of singleton bean '" + beanName + "'");
}
}
// 返回对应的实例,有时候在诸如FactoryBean的情况下,并不是返回实力本身,而是返回指定方法返回的实例
// 返回对应的实例,有时候在诸如 FactoryBean 的情况下,并不是返回实力本身,而是返回指定方法返回的实例
beanInstance = getObjectForBeanInstance(sharedInstance, name, beanName, null);
}
......@@ -336,7 +336,7 @@ public abstract class AbstractBeanFactory extends FactoryBeanRegistrySupport imp
// 缓存依赖调用,注意这里传入的key是被依赖的bean名称
registerDependentBean(dep, beanName);
try {
//递归调用getBean方法,注册Bean之间的依赖(如C需要晚于B初始化,而B需要晚于A初始化)
// 递归调用getBean方法,注册Bean之间的依赖(如C需要晚于B初始化,而B需要晚于A初始化)
getBean(dep);
}
catch (NoSuchBeanDefinitionException ex) {
......@@ -349,7 +349,7 @@ public abstract class AbstractBeanFactory extends FactoryBeanRegistrySupport imp
//如果BeanDefinition为单例
// Create bean instance.
if (mbd.isSingleton()) {
// 创建Bean实例对象,并且注册给依赖的对象
// 创建Bean实例对象,并且注册给依赖的对象
sharedInstance = getSingleton(beanName, () -> {
try {
return createBean(beanName, mbd, args);
......@@ -1804,7 +1804,7 @@ public abstract class AbstractBeanFactory extends FactoryBeanRegistrySupport imp
Object beanInstance, String name, String beanName, @Nullable RootBeanDefinition mbd) {
// Don't let calling code try to dereference the factory if the bean isn't a factory.
// 整个这个判断的意思是说,如果要获取的bean是FactoryBean的引用,但是beanInstance不是FactoryBean的就抛异常
// 整个这个判断的意思是说,如果要获取的bean是 FactoryBean 的引用,但是 beanInstance 不是 FactoryBean 的就抛异常
// 1. 验证: 判断输入的bean的name是否是FactoryBean相关的BeanName(前缀是&)
......
......@@ -343,7 +343,7 @@ class ConstructorResolver {
"Could not resolve matching constructor on bean class [" + mbd.getBeanClassName() + "] " +
"(hint: specify index/type/name arguments for simple parameters to avoid type ambiguities)");
}
// 如果ambiguousConstructors 不为空说明有多个构造函数可适配,并且 如果不允许多个存在,则抛出异常
// 如果 ambiguousConstructors 不为空说明有多个构造函数可适配,并且 如果不允许多个存在,则抛出异常
else if (ambiguousConstructors != null && !mbd.isLenientConstructorResolution()) {
throw new BeanCreationException(mbd.getResourceDescription(), beanName,
"Ambiguous constructor matches found on bean class [" + mbd.getBeanClassName() + "] " +
......
......@@ -16,6 +16,8 @@
package org.springframework.beans.factory.support;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
......@@ -90,7 +92,7 @@ public abstract class FactoryBeanRegistrySupport extends DefaultSingletonBeanReg
Object object = this.factoryBeanObjectCache.get(beanName);
// 如果缓存中没有
if (object == null) {
// 激活 FactoryBean<?> 中的 getObject() 方法获取开发者
// 激活 FactoryBean<?> 中的 getObject() 方法获取
// 定制的初始化逻辑
object = doGetObjectFromFactoryBean(factory, beanName);
// Only post-process and store if not put there already during getObject() call above
......@@ -108,7 +110,7 @@ public abstract class FactoryBeanRegistrySupport extends DefaultSingletonBeanReg
}
beforeSingletonCreation(beanName);
try {
// TODO: 2022/8/2 BeanPostProcessor的后置处理器postProcessAfterInitialization,还没学到
// TODO: 2022/8/2 BeanPostProcessor的前置处理器不使用吗
object = postProcessObjectFromFactoryBean(object, beanName);
}
catch (Throwable ex) {
......
......@@ -741,7 +741,7 @@ public abstract class AbstractApplicationContext extends DefaultResourceLoader
// Tell the internal bean factory to use the context's class loader etc.
// 设置 BeanFactory 的 ClassLoader 为当前 context 的 classLoader
beanFactory.setBeanClassLoader(getClassLoader());
// 设置 BeanFactory 的表达式处理器(SpE),Spring3增加
// 设置 BeanFactory 的表达式处理器(SpEL),Spring3增加
if (!shouldIgnoreSpel) {
beanFactory.setBeanExpressionResolver(new StandardBeanExpressionResolver(beanFactory.getBeanClassLoader()));
}
......@@ -990,7 +990,7 @@ public abstract class AbstractApplicationContext extends DefaultResourceLoader
}
// Initialize LoadTimeWeaverAware beans early to allow for registering their transformers early.
// 3.初始化LoadTimeWeaverAware Bean实例对象
// 3.初始化 LoadTimeWeaverAware Bean实例对象
String[] weaverAwareNames = beanFactory.getBeanNamesForType(LoadTimeWeaverAware.class, false, false);
for (String weaverAwareName : weaverAwareNames) {
getBean(weaverAwareName);
......
......@@ -139,7 +139,7 @@ final class PostProcessorRegistrationDelegate {
// Next, invoke the BeanDefinitionRegistryPostProcessors that implement Ordered.
// 4.调用所有实现了 Ordered 接口的 BeanDefinitionRegistryPostProcessor 实现类(过程跟上面的步骤3基本一样)
// 找出所有实现 BeanDefinitionRegistryPostProcessor 接口的类,
// 这边重复查找是因为执行完上面的BeanDefinitionRegistryPostProcessor,
// 这边重复查找是因为执行完上面的 BeanDefinitionRegistryPostProcessor,
// 可能会新增了其他的BeanDefinitionRegistryPostProcessor, 因此需要重新查找
postProcessorNames = beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
for (String ppName : postProcessorNames) {
......@@ -190,7 +190,7 @@ final class PostProcessorRegistrationDelegate {
// uninitialized to let the bean factory post-processors apply to them!
// 到这里, 入参 beanFactoryPostProcessors 和容器中的所有 BeanDefinitionRegistryPostProcessor 已经全部处理完毕,
// 下面开始处理容器中的所有 BeanFactoryPostProcessor
// 8.找出所有实现BeanFactoryPostProcessor接口的类
// 8.找出所有实现 BeanFactoryPostProcessor 接口的类
String[] postProcessorNames =
beanFactory.getBeanNamesForType(BeanFactoryPostProcessor.class, true, false);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment