spring源码分析详解
2020-10-16 10:00:41 0 举报
AI智能生成
spring注解源码分析,学习交流
作者其他创作
大纲/内容
AnnotationConfigApplicationContext
this()
new AnnotatedBeanDefinitionReader
new ConditionEvaluator
AnnotationConfigUtils.registerAnnotationConfigProcessors
unwrapDefaultListableBeanFactory(registry)
ContextAnnotationAutowireCandidateResolver
registry.containsBeanDefinition
new RootBeanDefinition(ConfigurationClassPostProcessor.class)
ConfigurationClassPostProcessor.class
processConfigBeanDefinitions(registry);
registerPostProcessor
setRole
DefaultListableBeanFactory#registerBeanDefinition
validate()
prepareMethodOverrides
DefaultListableBeanFactory#getBeanDefinition
BeanDefinitionHolder
new ClassPathBeanDefinitionScanner
this(registry, true);
this(registry, useDefaultFilters, getOrCreateEnvironment(registry));
this()#ClassPathBeanDefinitionScanner
register(annotatedClasses)
registerBean
doRegisterBean
new AnnotatedGenericBeanDefinition
shouldSkip
resolveScopeMetadata
generateBeanName
AnnotationConfigUtils.processCommonDefinitionAnnotations(abd)
new BeanDefinitionHolder(abd, beanName);
AnnotationConfigUtils.applyScopedProxyMode(scopeMetadata, definitionHolder, this.registry);
BeanDefinitionReaderUtils.registerBeanDefinition(definitionHolder, this.registry);
refresh()
prepareRefresh();
ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();
prepareBeanFactory(beanFactory);
postProcessBeanFactory(beanFactory);
invokeBeanFactoryPostProcessors(beanFactory);
PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(beanFactory, getBeanFactoryPostProcessors());
PostProcessorRegistrationDelegate
BeanDefinitionRegistry
regularPostProcessors
registryProcessors
currentRegistryProcessors
getBeanNamesForType
BeanDefinitionRegistryPostProcessor.class
postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry)
registry.getBeanDefinitionNames();
registry.getBeanDefinition(beanName);
configCandidates.isEmpty()
configCandidates.sort((bd1, bd2)
sbr.getSingleton(CONFIGURATION_BEAN_NAME_GENERATOR)
parser
candidates
alreadyParsed
do {}
parser.parse(candidates);
deferredImportSelectors
parse(((AnnotatedBeanDefinition) bd).getMetadata(), holder.getBeanName());
processConfigurationClass(new ConfigurationClass(metadata, beanName));
ConfigurationClass
existingClass
SourceClass
do{}while()
doProcessConfigurationClass(configClass, sourceClass);
componentScans
scannedBeanDefinitions
for (BeanDefinitionHolder holder : scannedBeanDefinitions)
parse(bdCand.getBeanClassName(), holder.getBeanName());
processImports(configClass, sourceClass, getImports(sourceClass), true);
importResource
beanMethods
processInterfaces(configClass, sourceClass);
sourceClass.getSuperClass();
this.configurationClasses.put(configClass, configClass);
processDeferredImportSelectors();
deferredImports.sort(DEFERRED_IMPORT_COMPARATOR);
DeferredImportSelectorGrouping
processImports
importStack.push(configClass);
importCandidates
candidate.isAssignable(ImportSelector.class)
ImportSelector
ParserStrategyUtils.invokeAwareMethods(
selector, this.environment, this.resourceLoader, this.registry);
selector, this.environment, this.resourceLoader, this.registry);
(this.deferredImportSelectors != null && selector instanceof DeferredImportSelector)
importSourceClasses
candidate.isAssignable(ImportBeanDefinitionRegistrar.class)
addImportBeanDefinitionRegistrar
this.importStack.registerImport(
currentSourceClass.getMetadata(), candidate.getMetadata().getClassName());
processConfigurationClass(candidate.asConfigClass(configClass));
currentSourceClass.getMetadata(), candidate.getMetadata().getClassName());
processConfigurationClass(candidate.asConfigClass(configClass));
this.importStack.pop();
parser.validate();
this.reader.loadBeanDefinitions(configClasses);
alreadyParsed.addAll(configClasses);
candidates.clear();
if (registry.getBeanDefinitionCount() > candidateNames.length)
newCandidateNames
oldCandidateNames
alreadyParsedClasses
while (!candidates.isEmpty());
sortPostProcessors(currentRegistryProcessors, beanFactory);
registryProcessors.addAll(currentRegistryProcessors);
invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);
currentRegistryProcessors.clear();
registerBeanPostProcessors(beanFactory);
beanFactory.getBeanNamesForType(BeanPostProcessor.class, true, false);
int beanProcessorTargetCount = beanFactory.getBeanPostProcessorCount() + 1 + postProcessorNames.length;
priorityOrderedPostProcessors
internalPostProcessors
orderedPostProcessorNames
nonOrderedPostProcessorNames
for (String ppName : postProcessorNames)
beanFactory.getBean(ppName, BeanPostProcessor.class);
priorityOrderedPostProcessors.add(pp);
sortPostProcessors(priorityOrderedPostProcessors, beanFactory);
beanFactory.addBeanPostProcessor(new ApplicationListenerDetector(applicationContext));
initMessageSource();
initApplicationEventMulticaster();
getApplicationListeners()
getApplicationEventMulticaster().addApplicationListener(listener);
getBeanNamesForType(ApplicationListener.class, true, false);
getApplicationEventMulticaster().addApplicationListenerBean(listenerBeanName);
Set<ApplicationEvent> earlyEventsToProcess
getApplicationEventMulticaster().multicastEvent(earlyEvent);
SimpleApplicationEventMulticaster#multicastEvent(event, resolveDefaultEventType(event));
onRefresh();
registerListeners();
finishBeanFactoryInitialization(beanFactory);
beanFactory.freezeConfiguration();
beanFactory.preInstantiateSingletons();
getMergedLocalBeanDefinition(beanName);
getBean(beanName);
doGetBean(name, null, null, false);
transformedBeanName(name);
getSingleton(beanName);
getSingleton(beanName, true);
this.singletonObjects.get(beanName);
if (singletonObject == null && isSingletonCurrentlyInCreation(beanName))
synchronized (this.singletonObjects)
singletonObject =this.earlySingletonObjects.get(beanName);
if (singletonObject == null && allowEarlyReference)
singletonFactory = this.singletonFactories.get(beanName);
if (singletonFactory != null)
singletonObject = singletonFactory.getObject();
createBean(beanName, mbd, args);
resolveBeanClass(mbd, beanName);
mbdToUse.setBeanClass(resolvedClass);
mbdToUse.prepareMethodOverrides();
Object bean = resolveBeforeInstantiation(beanName, mbdToUse);
doCreateBean(beanName, mbdToUse, args);
if (mbd.isSingleton())
this.factoryBeanInstanceCache.remove(beanName);
if (instanceWrapper == null)
createBeanInstance(beanName, mbd, args);
Class<?> beanClass = resolveBeanClass(mbd, beanName);
if (beanClass != null && !Modifier.isPublic(beanClass.getModifiers()) && !mbd.isNonPublicAccessAllowed())
mbd.getInstanceSupplier();
instantiateUsingFactoryMethod(beanName, mbd, args);
autowireConstructor(beanName, mbd, null, null);
determineConstructorsFromBeanPostProcessors(beanClass, beanName);
instantiateBean(beanName, mbd);
applyMergedBeanDefinitionPostProcessors(mbd, beanType, beanName);
boolean earlySingletonExposure = (mbd.isSingleton() && this.allowCircularReferences &&
isSingletonCurrentlyInCreation(beanName));
isSingletonCurrentlyInCreation(beanName));
addSingletonFactory
populateBean(beanName, mbd, instanceWrapper);
boolean continueWithPropertyPopulation = true;
getBeanPostProcessors()
InstantiationAwareBeanPostProcessor
if (mbd.getResolvedAutowireMode() == AUTOWIRE_BY_NAME || mbd.getResolvedAutowireMode() == AUTOWIRE_BY_TYPE)
PropertyValues pvs = (mbd.hasPropertyValues() ? mbd.getPropertyValues() : null);
hasInstantiationAwareBeanPostProcessors();
needsDepCheck
applyPropertyValues(beanName, mbd, bw, pvs);
initializeBean(beanName, exposedObject, mbd);
invokeAwareMethods(beanName, bean);
BeanNameAware
BeanClassLoaderAware
BeanFactoryAware
applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName);
invokeInitMethods(beanName, wrappedBean, mbd);
applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName);
Object earlySingletonReference = getSingleton(beanName, false);
registerDisposableBeanIfNecessary(beanName, bean, mbd);
getObjectForBeanInstance(sharedInstance, name, beanName, mbd);
getCachedObjectForFactoryBean(beanName);
getObjectFromFactoryBean(factory, beanName, !synthetic);
this.earlySingletonObjects.put(beanName, singletonObject);
this.singletonFactories.remove(beanName);
if (sharedInstance != null && args == null)
getObjectForBeanInstance(sharedInstance, name, beanName, null);
else { isPrototypeCurrentlyInCreation(beanName) }
getParentBeanFactory();
if (parentBeanFactory != null && !containsBeanDefinition(beanName))
originalBeanName(name);
if (parentBeanFactory instanceof AbstractBeanFactory)
((AbstractBeanFactory) parentBeanFactory).doGetBean(
nameToLookup, requiredType, args, typeCheckOnly);
nameToLookup, requiredType, args, typeCheckOnly);
parentBeanFactory.getBean(nameToLookup, args);
parentBeanFactory.getBean(nameToLookup, requiredType);
markBeanAsCreated(beanName);
getMergedLocalBeanDefinition(beanName);
checkMergedBeanDefinition(mbd, beanName, args);
mbd.getDependsOn();
registerDependentBean(dep, beanName);
getBean(dep);
if (mbd.isSingleton())
getSingleton(beanName);
synchronized (this.singletonObjects)
Object singletonObject = this.singletonObjects.get(beanName);
beforeSingletonCreation(beanName);
singletonObject = singletonFactory.getObject();
createBean(beanName, mbd, args);
resolveBeanClass(mbd, beanName);
mbdToUse.setBeanClass(resolvedClass);
mbdToUse.prepareMethodOverrides();
Object bean = resolveBeforeInstantiation(beanName, mbdToUse);
doCreateBean(beanName, mbdToUse, args);
if (mbd.isSingleton())
this.factoryBeanInstanceCache.remove(beanName);
if (instanceWrapper == null)
createBeanInstance(beanName, mbd, args);
Class<?> beanClass = resolveBeanClass(mbd, beanName);
if (beanClass != null && !Modifier.isPublic(beanClass.getModifiers()) && !mbd.isNonPublicAccessAllowed())
mbd.getInstanceSupplier();
instantiateUsingFactoryMethod(beanName, mbd, args);
autowireConstructor(beanName, mbd, null, null);
determineConstructorsFromBeanPostProcessors(beanClass, beanName);
instantiateBean(beanName, mbd);
applyMergedBeanDefinitionPostProcessors(mbd, beanType, beanName);
boolean earlySingletonExposure = (mbd.isSingleton() && this.allowCircularReferences &&
isSingletonCurrentlyInCreation(beanName));
isSingletonCurrentlyInCreation(beanName));
addSingletonFactory
populateBean(beanName, mbd, instanceWrapper);
boolean continueWithPropertyPopulation = true;
getBeanPostProcessors()
InstantiationAwareBeanPostProcessor
if (mbd.getResolvedAutowireMode() == AUTOWIRE_BY_NAME || mbd.getResolvedAutowireMode() == AUTOWIRE_BY_TYPE)
PropertyValues pvs = (mbd.hasPropertyValues() ? mbd.getPropertyValues() : null);
hasInstantiationAwareBeanPostProcessors();
needsDepCheck
applyPropertyValues(beanName, mbd, bw, pvs);
initializeBean(beanName, exposedObject, mbd);
invokeAwareMethods(beanName, bean);
BeanNameAware
BeanClassLoaderAware
BeanFactoryAware
applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName);
invokeInitMethods(beanName, wrappedBean, mbd);
applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName);
Object earlySingletonReference = getSingleton(beanName, false);
registerDisposableBeanIfNecessary(beanName, bean, mbd);
singletonObject = this.singletonObjects.get(beanName);
afterSingletonCreation(beanName);
if (newSingleton)
addSingleton(beanName, singletonObject);
getObjectForBeanInstance(sharedInstance, name, beanName, mbd);
getCachedObjectForFactoryBean(beanName);
getObjectFromFactoryBean(factory, beanName, !synthetic);
destroySingleton(beanName);
else if (mbd.isPrototype())
beforePrototypeCreation(beanName);
prototypeInstance = createBean(beanName, mbd, args);
afterPrototypeCreation(beanName);
getObjectForBeanInstance(sharedInstance, name, beanName, mbd);
finishRefresh();
destroyBeans();
cancelRefresh(ex);
resetCommonCaches();
0 条评论
下一页