IOC、AOP、事务加载过程
2021-02-08 17:02:52 0 举报
AI智能生成
IOC、AOP、事务加载过程
作者其他创作
大纲/内容
new AnnotationConfigApplicationContext();
this();
new DefaultListableBeanFactory();
new AnnotatedBeanDefinitionReader(this);
负责解析用注解实现的监听器
finishBeanFactoryInitialization
找到@EventListener标识的方法
创建监听器,并add到ApplicationListener中
unwrapDefaultListableBeanFactory(registry);
setDependencyComparator(AnnotationAwareOrderComparator.INSTANCE);
setAutowireCandidateResolver(new ContextAnnotationAutowireCandidateResolver());
new ClassPathBeanDefinitionScanner(this);
registerDefaultFilters();
register(componentClasses);
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()));
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);
addBeanPostProcessor(new ApplicationListenerDetector(this));
setTempClassLoader(new ContextTypeMatchClassLoader(beanFactory.getBeanClassLoader()));
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);
finishRefresh();
clearResourceCaches();
initLifecycleProcessor();
getLifecycleProcessor().onRefresh();
publishEvent(new ContextRefreshedEvent(this));
LiveBeansView.registerApplicationContext(this);
resetCommonCaches();
parse(candidates);
scanner.doScan(StringUtils.toStringArray(basePackages));
findCandidateComponents(basePackage);
isCandidateComponent(metadataReader)
beanDefinition.applyDefaults(this.beanDefinitionDefaults);
invokeBeanFactoryPostProcessors();
enhanceConfigurationClasses(beanFactory);
直接把Bean后置处理器add到集合中
publishEvent();
钩子方法
getSingleton()
beforeSingletonCreation(beanName);
createBean()
创建动态代理
createAopProxy()
afterSingletonCreation(beanName);
一级缓存中取
buildAspectJAdvisors()
Import扫描
@Import(AspectJAutoProxyRegistrar.class)
registerAspectJAnnotationAutoProxyCreatorIfNecessary(registry);
@Import(TransactionManagementConfigurationSelector.class)
AdviceModeImportSelector
0 条评论
回复 删除
下一页