IOC、AOP、事务加载过程
2021-02-08 17:02:52 0 举报
AI智能生成
IOC、AOP、事务加载过程
作者其他创作
大纲/内容
this();
new DefaultListableBeanFactory();
new AnnotatedBeanDefinitionReader(this);
registerAnnotationConfigProcessors(registry, null);
unwrapDefaultListableBeanFactory(registry);
setDependencyComparator(AnnotationAwareOrderComparator.INSTANCE);
setAutowireCandidateResolver(new ContextAnnotationAutowireCandidateResolver());
registerPostProcessor(registry, def, CONFIGURATION_ANNOTATION_PROCESSOR_BEAN_NAME
registerPostProcessor(registry, def, AUTOWIRED_ANNOTATION_PROCESSOR_BEAN_NAME)
registerPostProcessor(registry, def, COMMON_ANNOTATION_PROCESSOR_BEAN_NAME)
registerPostProcessor(registry, def, PERSISTENCE_ANNOTATION_PROCESSOR_BEAN_NAME)
registerPostProcessor(registry, def, EVENT_LISTENER_PROCESSOR_BEAN_NAME)
registerPostProcessor(registry, def, EVENT_LISTENER_FACTORY_BEAN_NAME)
new ClassPathBeanDefinitionScanner(this);
registerDefaultFilters();
register(componentClasses);
beanDefinitionMap.put(beanName, beanDefinition)
refresh();
prepareRefresh();
initPropertySources();
getEnvironment().validateRequiredProperties();
this.earlyApplicationEvents = new LinkedHashSet<>();
obtainFreshBeanFactory();
refreshBeanFactory();
javaconfig(GenericApplicationContext)
xml(AbstractRefreshableApplicationContext)
loadBeanDefinitions(beanDefinitionReader);
getBeanFactory();
prepareBeanFactory(beanFactory);
setBeanClassLoader(getClassLoader());
setBeanExpressionResolver(new StandardBeanExpressionResolver(beanFactory.getBeanClassLoader()));
addPropertyEditorRegistrar(new ResourceEditorRegistrar(this, getEnvironment()));
addBeanPostProcessor(new ApplicationContextAwareProcessor(this));
ignoreDependencyInterface(EnvironmentAware.class);
ignoreDependencyInterface(EmbeddedValueResolverAware.class);
ignoreDependencyInterface(ResourceLoaderAware.class);
ignoreDependencyInterface(ApplicationEventPublisherAware.class);
ignoreDependencyInterface(MessageSourceAware.class);
ignoreDependencyInterface(ApplicationContextAware.class);
ignoreDependencyInterface(ApplicationStartup.class);
registerResolvableDependency(BeanFactory.class, beanFactory);
registerResolvableDependency(ResourceLoader.class, this);
registerResolvableDependency(ApplicationEventPublisher.class, this);
registerResolvableDependency(ApplicationContext.class, this);
addBeanPostProcessor(new ApplicationListenerDetector(this));
setTempClassLoader(new ContextTypeMatchClassLoader(beanFactory.getBeanClassLoader()));
registerSingleton(ENVIRONMENT_BEAN_NAME, getEnvironment());
registerSingleton(SYSTEM_PROPERTIES_BEAN_NAME, getEnvironment().getSystemProperties()
registerSingleton(SYSTEM_ENVIRONMENT_BEAN_NAME, getEnvironment().getSystemEnvironment());
registerSingleton(APPLICATION_STARTUP_BEAN_NAME, getApplicationStartup());
postProcessBeanFactory(beanFactory);
invokeBeanFactoryPostProcessors(beanFactory);
beanFactory实现了BeanDefinitionRegistryPostProcessors
循环自定义的beanFactoryPostProcessors,扫描BeanDefinition并解析
首先筛选出实现PriorityOrdered接口的带注册能力的BeanFactory后置处理器,并实例化BeanFactory后置处理器,先排序,扫描BeanDefinition并解析
再筛选出实现Ordered接口的带注册能力的BeanFactory后置处理器,并实例化BeanFactory后置处理器,先排序,扫描BeanDefinition并解析
最后筛选出没有实现PriorityOrdered和Ordered接口的带注册能力的BeanFactory后置处理器,并实例化BeanFactory后置处理器,先排序,扫描BeanDefinition并解析
由于实现了BeanDefinitionRegistryPostProcessors,也实现了BeanFactoryPostProcessors,所以还会调用invokeBeanFactoryPostProcessors
beanFactory实现BeanFactoryPostProcessors
获取所有实现了BeanFactoryPostProcessors的BeanFactoryPostProcessor
首先筛选出实现PriorityOrdered接口的BeanFactory后置处理器,并实例化BeanFactory后置处理器,先排序,扫描BeanDefinition并解析
再筛选出实现Ordered接口的BeanFactory后置处理器,并实例化BeanFactory后置处理器,先排序,扫描BeanDefinition并解析
最后筛选出没有实现PriorityOrdered和Ordered接口的BeanFactory后置处理器,并实例化BeanFactory后置处理器,先排序,扫描BeanDefinition并解析
registerBeanPostProcessors(beanFactory);
先注册实现PriorityOrdered接口的Bean后置处理器
再注册实现Ordered接口的Bean后置处理器
再注册没实现Ordered和PriorityOrdered接口的Bean后置处理器
initMessageSource();
initApplicationEventMulticaster();
onRefresh();
registerListeners();
finishBeanFactoryInitialization(beanFactory);
beanFactory.freezeConfiguration();
beanFactory.setTempClassLoader(null);
beanFactory.preInstantiateSingletons();
获取所有的BeanDefinition名称,判断是否是factoryBean
getBean(beanName);
doGetBean(name, null, null, false);
finishRefresh();
clearResourceCaches();
initLifecycleProcessor();
getLifecycleProcessor().onRefresh();
publishEvent(new ContextRefreshedEvent(this));
LiveBeansView.registerApplicationContext(this);
resetCommonCaches();
invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry, beanFactory.getApplicationStartup());
parse(candidates);
componentScanParser.parse(componentScan, sourceClass.getMetadata().getClassName());
scanner.doScan(StringUtils.toStringArray(basePackages));
findCandidateComponents(basePackage);
isCandidateComponent(metadataReader)
postProcessBeanDefinition((AbstractBeanDefinition) candidate, beanName);
beanDefinition.applyDefaults(this.beanDefinitionDefaults);
registerBeanDefinition(definitionHolder, this.registry);
parse(bdCand.getBeanClassName(), holder.getBeanName());
invokeBeanFactoryPostProcessors();
enhanceConfigurationClasses(beanFactory);
直接把Bean后置处理器add到集合中
publishEvent();
getApplicationEventMulticaster().multicastEvent(applicationEvent, eventType);
invokeListener(listener, event);
getApplicationListeners(event, type)
retrieveApplicationListeners(eventType, sourceType, newRetriever);
钩子方法
getSingleton()
beforeSingletonCreation(beanName);
createBean()
resolveBeforeInstantiation(beanName, mbdToUse);
applyBeanPostProcessorsBeforeInstantiation(targetType, beanName);
applyBeanPostProcessorsAfterInitialization(bean, beanName);
doCreateBean(beanName, mbdToUse, args);
createBeanInstance(beanName, mbd, args);
createBeanInstance(beanName, mbd, args);
applyMergedBeanDefinitionPostProcessors(mbd, beanType, beanName);
addSingletonFactory(beanName, () -> getEarlyBeanReference(beanName, mbd, bean));
getEarlyBeanReference(beanName, mbd, bean)
先缓存,调用wrapIfNecessary
getAdvicesAndAdvisorsForBean(bean.getClass(), beanName, null);
findEligibleAdvisors(beanClass, beanName);
获取候选的Advisors,并筛选出能应用的
创建动态代理
createAopProxy()
populateBean(beanName, mbd, instanceWrapper);
applyPropertyValues(beanName, mbd, bw, pvs);
initializeBean(beanName, exposedObject, mbd);
applyBeanPostProcessorsBeforeInstantiation(targetType, beanName);
applyBeanPostProcessorsAfterInitialization(bean, beanName);
afterSingletonCreation(beanName);
addSingleton(beanName, singletonObject);
一级缓存中取
createBean(beanName, mbd, args);
循环所有的Bean后置处理器,调用postProcessBeforeInstantiation(初始化之前)
shouldSkip(beanClass, beanName)
buildAspectJAdvisors()
Import扫描
@Import(AspectJAutoProxyRegistrar.class)
registerAspectJAnnotationAutoProxyCreatorIfNecessary(registry);
registerOrEscalateApcAsRequired(AnnotationAwareAspectJAutoProxyCreator.class, registry, source);
@Import(TransactionManagementConfigurationSelector.class)
AdviceModeImportSelector
registerOrEscalateApcAsRequired(InfrastructureAdvisorAutoProxyCreator.class, registry, source);
0 条评论
下一页