SpringIOC
2021-08-19 15:05:42 0 举报
AI智能生成
Spring IOC 源码脑图
作者其他创作
大纲/内容
SpringIOC
AbstractApplicationContext
ClassPathXmlApplicationContext
refresh();
obtainFreshBeanFactory()
AnnotationConfigApplicationContext
创建Bean工厂 BeanFactoryDefaultListableBeanFactory 注意实现了Bean定义注册器 BeanDefinitionRegistry
创建Bean定义读取器 BeanDefinitionReader(AnnotatedBeanDefinitionReader)
beanDefs.add -> ConfigurationClassPostProcessor
beanDefs.add -> AutowiredAnnotationBeanPostProcessor
beanDefs.add -> CommonAnnotationBeanPostProcessor
beanDefs.add -> EventListenerMethodProcessor
beanDefs.add -> DefaultEventListenerFactory
创建Bean定义扫描器 BeanDefinitionScanner(ClassPathBeanDefinitionScanner)
注册传入的配置类 register(componentClasses); -> reader.register(componentClasses);
obtainFreshBeanFactory();
refreshed=true
beanFactory.setSerializationId
return beanFactory;
invokeBeanFactoryPostProcessors(beanFactory);
BeanDefinitionRegistryPostProcessorPriorityOrderedpostProcessBeanDefinitionRegistry
org.springframework.context.annotation.internalConfigurationAnnotationProcessorConfigurationClassPostProcessorprocessConfigBeanDefinitions
configCandidatesConfigurationClassUtils.checkConfigurationClassCandidate
configCandidates -> do…while
ConfigurationClassParser.parse(candidates);
doProcessConfigurationClass
@Component -> processMemberClasses
@PropertySource -> processPropertySource
@ComponentScan
componentScanParser.parse
scanner.doScan(basePackages);
findCandidateComponents
scanCandidateComponents
getResources(packageSearchPath)
isCandidateComponent
registerBeanDefinitionbeanDefinitionNames.add(beanName);
ConfigurationClassUtils.checkConfigurationClassCandidateConfigurationClassParser.parse(candidates);
@Import -> processImports
@ImportResource -> configClass.addImportedResource
@Bean -> retrieveBeanMethodMetadata
interfaces -> processInterfaces
configClasses=parser.getConfigurationClasses();
reader.loadBeanDefinitions(configClasses);
newCandidateNames = registry.getBeanDefinitionNames() - candidateNames;
ConfigurationClassUtils.checkConfigurationClassCandidate(newCandidateBeanDefinition);
configCandidates -> ConfigurationClassParser.parse(candidates);
BeanDefinitionRegistryPostProcessorOrderedpostProcessBeanDefinitionRegistry
BeanDefinitionRegistryPostProcessorpostProcessBeanDefinitionRegistry
BeanDefinitionRegistryPostProcessorpostProcessBeanFactory
enhanceConfigurationClasses传入的配置类会被CGLIB代理,对类内部对象进行getBean
BeanFactoryPostProcessor(AbstractApplicationContext.beanFactoryPostProcessors用context.add才有)postProcessBeanFactory
BeanFactoryPostProcessorPriorityOrderedpostProcessBeanFactory
BeanFactoryPostProcessorOrderedpostProcessBeanFactory
BeanFactoryPostProcessorpostProcessBeanFactory
finishBeanFactoryInitialization(beanFactory);
beanFactory.freezeConfiguration();
beanFactory.preInstantiateSingletons();
List<String> beanNames=new ArrayList<>(this.beanDefinitionNames);
RootBeanDefinition bd=getMergedLocalBeanDefinition(beanName);
!bd.isAbstract() && bd.isSingleton() && !bd.isLazyInit()非抽象、单例、非懒加载
isFactoryBean(beanName)是否为工厂Bean
getBean(beanName);
String beanName=transformedBeanName(name);
singletonObjects.get(beanName);从一级缓存拿对象没有
isSingletonCurrentlyInCreation(beanName) 一级缓存为空看是否正在创建singletonsCurrentlyInCreation.contains(beanName);
earlySingletonObjects.get(beanName);一级缓存没有且正在创建,检查二级缓存
allowEarlyReference=true (resolving a circular reference)二级缓存没有 且 允许早期引用当前创建的单例
synchronized(singletonObjects)
singletonObjects.get(beanName)检查一级缓存没有
earlySingletonObjects.get(beanName)检查二级缓存没有
ObjectFactory<?> singletonFactory=singletonFactories.get(beanName);singletonObject=singletonFactory.getObject();从三级缓存取出来函数接口创建实例
singletonFactories.remove(beanName);从三级缓存移除
isPrototypeCurrentlyInCreation(beanName)若在创建中,则 throw BeanCurrentlyInCreationExceptionSpring 只能解决单例对象的 setter 注入的循环依赖,不能解决构造器注入。
parentBeanFactory.getBean有父容器,则检查父容器
!typeCheckOnly -> markBeanAsCreated(beanName);typeCheckOnly:是否获取实例是为了进行类型检查,而不是为了实际使用标记bean被创建了或者正要被创建
String[] dependsOn=mbd.getDependsOn();解析依赖Bean,若有则先创建依赖的Bean
getBean(dep);
mbd.isSingleton()
Object singletonObject=singletonObjects.get(beanName);为空,一级缓存没有,进行创建
beforeSingletonCreation(beanName);标记正在创建
inCreationCheckExclusions.contains(beanName)
singletonsCurrentlyInCreation.add(beanName)标记正在创建
singletonObject=singletonFactory.getObject();
afterSingletonCreation(beanName);
singletonsCurrentlyInCreation.remove(beanName)移除正在创建
singletonFactories.remove(beanName);从三级缓存中移除
earlySingletonObjects.remove(beanName);从二级缓存中移除(循环依赖时 早期对象 存在于二级缓存)
registeredSingletons.add(beanName);用来记录保存已经处理的bean
RootBeanDefinition mbdToUse=mbd;
mbdToUse.prepareMethodOverrides();XML中的 lookup-method 和 replace-method 场景
getBeanPostProcessors()
bp instanceof InstantiationAwareBeanPostProcessor
mbd.isSingleton() -> factoryBeanInstanceCache.remove(beanName);
…………
Object bean=instanceWrapper.getWrappedInstance();
Class<?> beanType=instanceWrapper.getWrappedClass();
synchronized(mbd.postProcessingLock)
earlySingletonExposure= mbd.isSingleton() && allowCircularReferences && singletonsCurrentlyInCreation.contains(beanName)处理循环依赖
!singletonObjects.containsKey(beanName)一级缓存没有
earlySingletonObjects.remove(beanName);从二级缓存移除
registeredSingletons.add(beanName);
!mbd.isSynthetic()&&hasInstantiationAwareBeanPostProcessors
bp instanceof SmartInstantiationAwareBeanPostProcessor包括AbstractAutoProxyCreator,可使用AOP创建动态代理
mbd.getResolvedAutowireMode()
hasInstantiationAwareBeanPostProcessors
BeanNameAware.setBeanName(beanName);
BeanClassLoaderAware.setBeanClassLoader(getBeanClassLoader());
BeanFactoryAware.setBeanFactory(AbstractAutowireCapableBeanFactory.this);
earlySingletonExposure使用上边的变量
mbd.isPrototype()
beforePrototypeCreation(beanName);
afterPrototypeCreation(beanName);
elseString scopeName=mbd.getScope();Scope scope=this.scopes.get(scopeName);
prepareRefresh();
prepareBeanFactory(beanFactory);
postProcessBeanFactory(beanFactory);
registerBeanPostProcessors(beanFactory);
initMessageSource();
initApplicationEventMulticaster();
onRefresh();
registerListeners();
finishRefresh();
resetCommonCaches();
BeanPostProcessors
Aware
EnvironmentAware.setEnvironment(applicationContext.getEnvironment());
EmbeddedValueResolverAware.setEmbeddedValueResolver(embeddedValueResolver);
ResourceLoaderAware.setResourceLoader(applicationContext);
ApplicationEventPublisherAware.setApplicationEventPublisher(applicationContext);
MessageSourceAware.setMessageSource(applicationContext);
ApplicationContextAware.setApplicationContext(applicationContext);
0 条评论
回复 删除
下一页