整体位置
调用DefaultListableBeanFactory.registerBeanDefinition
5 6次
6 1次 入口主文件
10 140多次,包括自动配置类,自定义注解那些(最多)
16 1次org.springframework.aop.config.internalAutoProxyCreator
上游1
public class AnnotationConfigServletWebServerApplicationContext extends ServletWebServerApplicationContext implements AnnotationConfigRegistry {
public AnnotationConfigServletWebServerApplicationContext() {
this.reader = new AnnotatedBeanDefinitionReader(this);//1
this.scanner = new ClassPathBeanDefinitionScanner(this);
}
@Override
public void registerBeanDefinition(String beanName, BeanDefinition beanDefinition)throws BeanDefinitionStoreException {
//org.springframework.context.annotation.internalConfigurationAnnotationProcessor
//org.springframework.context.annotation.internalAutowiredAnnotationProcessor
//org.springframework.context.annotation.internalCommonAnnotationProcessor
//org.springframework.context.event.internalEventListenerProcessor
//org.springframework.context.event.internalEventListenerFactory
//springbootUploadApplication
this.beanFactory.registerBeanDefinition(beanName, beanDefinition);//5
}
}
public AnnotatedBeanDefinitionReader(BeanDefinitionRegistry registry, Environment environment) {
Assert.notNull(registry, "BeanDefinitionRegistry must not be null");
Assert.notNull(environment, "Environment must not be null");
this.registry = registry;
this.conditionEvaluator = new ConditionEvaluator(registry, environment, null);
AnnotationConfigUtils.registerAnnotationConfigProcessors(this.registry);//2
}
//注册BeanPostProcessor/BeanFactoryPostProcessor/AutowireCandidateResolver
public abstract class AnnotationConfigUtils {
//3
private static BeanDefinitionHolder registerPostProcessor(BeanDefinitionRegistry registry, RootBeanDefinition definition, String beanName) {
definition.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
//4
registry.registerBeanDefinition(beanName, definition);//这个registry就是AnnotationConfigServletWebServerApplicationContext,回到了上面
return new BeanDefinitionHolder(definition, beanName);
}
}
上游2
起始11#invokeBeanFactoryPostProcessors
AnnotationConfigServletWebServerApplicationContext.invokeBeanFactoryPostProcessors
PostProcessorRegistrationDelegate#invokeBeanFactoryPostProcessors #invokeBeanDefinitionRegistryPostProcessors
ConfigurationClassPostProcessor#postProcessBeanDefinitionRegistry #processConfigBeanDefinitions
分支1 自定义@Controller @Configuration
ConfigurationClassParser#parse #processConfigurationClass #doProcessConfigurationClass
ComponentScanAnnotationParser#parse
ClassPathBeanDefinitionScanner#doScan #registerBeanDefinition
BeanDefinitionReaderUtils#registerBeanDefinition
终于DefaultListableBeanFactory.registerBeanDefinition
分支2 myServlet myFilter myListener servletComponentRegisteringPostProcessor
ConfigurationClassBeanDefinitionReader#loadBeanDefinitions 遍历#loadBeanDefinitionsForConfigurationClass #loadBeanDefinitionsForBeanMethod #this.registry.registerBeanDefinition(beanName, beanDefToRegister);
终于DefaultListableBeanFactory.registerBeanDefinition
分支3
ConfigurationClassBeanDefinitionReader#loadBeanDefinitionsForConfigurationClass #loadBeanDefinitionsFromRegistrars
ServletComponentScanRegistrar#registerBeanDefinitions #registerBeanDefinitions #addPostProcessor
终于DefaultListableBeanFactory.registerBeanDefinition
分支4 org.springframework.boot.autoconfigure.AutoConfigurationPackages
ConfigurationClassBeanDefinitionReader#loadBeanDefinitionsFromRegistrars
org.springframework.boot.autoconfigure.AutoConfigurationPackages.Registrar#registerBeanDefinitions #registerBeanDefinitions #register
终于DefaultListableBeanFactory.registerBeanDefinition
分支5 org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration
分支6 dispatcherServlet
ConfigurationClassBeanDefinitionReader#loadBeanDefinitionsForBeanMethod
核心代码
作为AnnotationConfigServletWebServerApplicationContext的一个属性
public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFactory implements ConfigurableListableBeanFactory, BeanDefinitionRegistry, Serializable {
/** List of bean definition names, in registration order.存储Bean名称 */
private volatile List<String> beanDefinitionNames = new ArrayList<>(256);
/** Map of bean definition objects, keyed by bean name. map存储Bean名称和Bean定义*/
private final Map<String, BeanDefinition> beanDefinitionMap = new ConcurrentHashMap<>(256);
//6
@Override
public void registerBeanDefinition(String beanName, BeanDefinition beanDefinition) throws BeanDefinitionStoreException {
//所有注册Bean都会经过这里!!
//部分逻辑
if (hasBeanCreationStarted()) {
// Cannot modify startup-time collection elements anymore (for stable iteration)
synchronized (this.beanDefinitionMap) {
this.beanDefinitionMap.put(beanName, beanDefinition);
List<String> updatedDefinitions = new ArrayList<>(this.beanDefinitionNames.size() + 1);
updatedDefinitions.addAll(this.beanDefinitionNames);
updatedDefinitions.add(beanName);
this.beanDefinitionNames = updatedDefinitions;
removeManualSingletonName(beanName);
}
}
else {
// Still in startup registration phase
this.beanDefinitionMap.put(beanName, beanDefinition);
this.beanDefinitionNames.add(beanName);
removeManualSingletonName(beanName);
}
}
}
总结下关系:
- 所有注册Bean的入口就是DefaultListableBeanFactory#registerBeanDefinition(String beanName, BeanDefinition beanDefinition)
- 真正的容器在于AnnotationConfigServletWebServerApplicationContext#属性private final DefaultListableBeanFactory beanFactory;
类图
应用vs容器真正类
public class AnnotationConfigServletWebServerApplicationContext extends ServletWebServerApplicationContext
implements AnnotationConfigRegistry
祖父
public class GenericApplicationContext extends AbstractApplicationContext implements BeanDefinitionRegistry {
//存放Bean对象的东西
private final DefaultListableBeanFactory beanFactory;
}
//存放Bean对象的东西
public class DefaultListableBeanFactory{
/** Map from dependency type to corresponding autowired value. */
private final Map<Class<?>, Object> resolvableDependencies = new ConcurrentHashMap<>(16);
/** Map of bean definition objects, keyed by bean name. */
private final Map<String, BeanDefinition> beanDefinitionMap = new ConcurrentHashMap<>(256);
/** Map from bean name to merged BeanDefinitionHolder. */
private final Map<String, BeanDefinitionHolder> mergedBeanDefinitionHolders = new ConcurrentHashMap<>(256);
/** Map of singleton and non-singleton bean names, keyed by dependency type. */
private final Map<Class<?>, String[]> allBeanNamesByType = new ConcurrentHashMap<>(64);
/** Map of singleton-only bean names, keyed by dependency type. */
private final Map<Class<?>, String[]> singletonBeanNamesByType = new ConcurrentHashMap<>(64);
/** List of bean definition names, in registration order. */
private volatile List<String> beanDefinitionNames = new ArrayList<>(256);
/** List of names of manually registered singletons, in registration order. */
private volatile Set<String> manualSingletonNames = new LinkedHashSet<>(16);
/** Cached array of bean definition names in case of frozen configuration. */
@Nullable
private volatile String[] frozenBeanDefinitionNames;
}