Spring流程图
2022-06-06 16:16:01 5 举报
Bean的生成流程
作者其他创作
大纲/内容
调用父类容器的getBean()方法
是
Object singletonObject = this.singletonObjects.get(beanName);
1.准备刷新,prepareRefresh()2.创建容器对象,obtainFreshBeanFactory()3.对各种属性进行填充,prepareBeanFactory(beanFactory)4.子类扩展,postProcessBeanFactory()5.执行BFPP中的方法,invokeBeanFactoryPostProcessors()6.注册BPP,registerBeanPostProcessors()7.设置国际语言处理,initMessageSource()8.初始化事件监听多路广播器,initApplicationEventMulticaster()9.留个子类初始化其他bean,onRefresh()10.注册监听器,registerListeners()11.初始化剩下的单实例(非懒加载)finishBeanFactoryInitialization(beanFactory)12.完成刷新过程,finishRefresh()13.清空运行时的缓存,resetCommonCaches()
1.调用父类AbstractApplicationContext构造方法,创建资源解析器PathMatchingResourcePatternResolver2.设置配置路径setConfigLocations(configLocations)3.刷新容器refresh()
给BeanPostProcessors一个机会返回代理来替代真正的实例,应用实例化前置处理器。如果包含AOP相关操作,则会在此处生成Advisor对象,方便后续继续调用
否
验证访问修饰符
1.创建createBeanFactory(),new DefaultListableBeanFactory(getInternalParentBeanFactory())2.设置序列化ID,beanFactory.setSerializationId(getId())3.设置相关属性,customizeBeanFactory(beanFactory)4.初始化documentReader,并进行xml文件读取以及解析,loadBeanDefinitions(beanFactory)
ctor.newInstance()
String beanName = transformedBeanName(name)转换bean的名称
当对象都是单例的时候会尝试解决循环依赖的的问题,但是原型模式下如果存在循环依赖的情况,那么直接抛出异常
1.refreshBeanFactory()2.getBeanFactory()
isPrototypeCurrentlyInCreation(beanName)
1.添加表达式语言处理器StandardBeanExpressionResolver2.添加属性编辑器ResourceEditorRegistrar3.添加BeanPostProcessor集合4.设置要忽略的接口ignoreDependencyInterface()5.添加自动装备的特殊规则registerResolvableDependency()6.将系统环境添加到一级缓存中
AutowiredAnnotationBeanPostProcessor处理自动注@Autowired,@Value
抛出异常
boolean earlySingletonExposure = (mbd.isSingleton() && this.allowCircularReferences &&isSingletonCurrentlyInCreation(beanName));
mbd == null || !mbd.isSynthetic()
refreshBeanFactory()
创建Bean定义信息集合List<String> beanNames = new ArrayList<>(this.beanDefinitionNames)
获取当前bean的构造方法来生成具体对象
不满足
合并父类RootBeanDefinitionRootBeanDefinition bd = getMergedLocalBeanDefinition(beanName);
通过scope创建具体对象,一般不会走此方法
getBean(dep)
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(\"spring.xml\");创建Spring应用程序上下文
通过AOP生成代理对象
Object sharedInstance = getSingleton(beanName)
mbd.getInitMethodName()
CommonAnnotationBeanPostProcessor处理@PostConstruct,@PreDestroy,@Resource
finishBeanFactoryInitialization(beanFactory)
做容器刷新前的准备工作1.设置容器的启动时间2.设置活跃状态为true3.设置关闭状态为false4.扩展方法initPropertySources(),初始化属性资源5.获取Environment对象,并加载当前系统的属性值到Environment对象中6.准备监听器和事件的集合对象,默认为空集合
1.EnvironmentAware2.EmbeddedValueResolverAware3.ResourceLoaderAware4.ApplicationEventPublisherAware5.MessageSourceAware6.ApplicationContextAware7.ApplicationStartupAware
doGetBean(beanName)
1.添加转换服务setConversionService(ConversionService)2.添加内置值处理器addEmbeddedValueResolver()3.冻结Bean定义信息freezeConfiguration()4.开始实例化preInstantiateSingletons()
getBean(beanName)
Object bean = instanceWrapper.getWrappedInstance()
mbd.isPrototype()判断是否是原型
1.Object bean = getBean(FACTORY_BEAN_PREFIX + beanName)
String[] dependsOn = mbd.getDependsOn()如果存在依赖的bean,那么则优先实例化依赖的bean
ApplicationContextAwareProcessor完成其他Aware接口子类的设置工作
obtainFreshBeanFactory()
RootBeanDefinition mbd = getMergedLocalBeanDefinition(beanName)
BeanWrapper bw = new BeanWrapperImpl(beanInstance);添加bean对象到包装类,并对包装类做初始化操作
BeanFactory parentBeanFactory = getParentBeanFactory()获取当前容器的父容器
this.singletonsCurrentlyInDestruction判断当前对象是否被销毁
isInitializingBean && (mbd == null || !mbd.hasAnyExternallyManagedInitMethod(\"afterPropertiesSet\"))
判断是否是非静态,非单例,非懒加载!bd.isAbstract() && bd.isSingleton() && !bd.isLazyInit()
singletonObject == null
parentBeanFactory != null && !containsBeanDefinition(beanName)
dependsOn != null
获取默认构造方法进行实例化操作
autowireConstructor()通过此方法生成具体对象并且返回
if (bean instanceof BeanNameAware) { ((BeanNameAware) bean).setBeanName(beanName);}if (bean instanceof BeanClassLoaderAware) { ClassLoader bcl = getBeanClassLoader(); if (bcl != null) { ((BeanClassLoaderAware) bean).setBeanClassLoader(bcl); }}if (bean instanceof BeanFactoryAware) { ((BeanFactoryAware) bean).setBeanFactory(AbstractAutowireCapableBeanFactory.this);}
singletonObject = singletonFactory.getObject()调用getObject方法来创建对象
返回bean
mbd.getFactoryMethodName() != null是否包含FactortMethod方法
markBeanAsCreated(beanName)标记当前对象
prepareRefresh()
mbd != null && bean.getClass() != NullBean.class
instanceSupplier != null是否包含Supplier方法
mbd.isSingleton()判断是否是单例
afterPropertiesSet()
Class<?> beanType = instanceWrapper.getWrappedClass()
判断是否实现FactoryBean接口isFactoryBean(beanName)
prepareBeanFactory(beanFactory)
!mbd.postProcessed是否允许post-processors修改BD
满足
loadBeanDefinitions
1.此处做了BeanDefinition对象的转换,当我们xml文件中加载beanDefinition对象的时候,封装的对象是GenericBeanDefinition2.此处要做类型转换,如果是子类的bean的话,要合并父类的相关属性
Supplier<?> instanceSupplier = mbd.getInstanceSupplier()
sharedInstance != null
取出集合的元素,创建Bean
earlySingletonExposure 判断是否提前暴露
是否立即被加载
在此处扫描添加Spring框架中的插件<context:component-scan base-package=\"*\" />1.ConfigurationClassPostProcessor2.AutowiredAnnotationBeanPostProcessor3.CommonAnnotationBeanPostProcessor4.EventListenerMethodProcessor5.DefaultEventListenerFactory字符替换类PropertySourcesPlaceholderConfigurer
mbdToUse.prepareMethodOverrides()验证及准备方法覆盖 lookup-method, replace-method
收藏
0 条评论
下一页