spring源码解析一bean
学习spring源码是一件非常煎熬且很有挑战的事情,在我们日常的工作中用的最多的框架可能就是spring及其的拓展,这是一个极其庞大的家族,首先我们来先来学习spring源码,然后在它的基础上学习springmvc,springboot,springcolud等。
1、BeanFactory对bean的生命周期进行管理,生产各种所需要的bean供我们使用
BeanFactory是一个接口,它的子接口跟实现有:AnnotationConfigApplicationContext是用来管理注解bean的容器;GenericApplicationContext通用应用上下文;AnnotationConfigRegistry注解配置注册表,ListableBeanFactory根据各种条件获取bean的配置清单;ConfigurableBeanFactory提供配置factory的各种方法
2、BeanFactoryPostProcessor:对beanFactory进行操作更新的类;FunctionalInterfacepublicinterfaceBeanFactoryPostProcessor{Modifytheapplicationcontextsinternalbeanfactoryafteritsstandardinitialization。Allbeandefinitionswillhavebeenloaded,butnobeanswillhavebeeninstantiatedyet。Thisallowsforoverridingoraddingpropertieseventoeagerinitializingbeans。parambeanFactorythebeanfactoryusedbytheapplicationcontextthrowsorg。springframework。beans。BeansExceptionincaseoferrorsvoidpostProcessBeanFactory(ConfigurableListableBeanFactorybeanFactory)throwsBeansException;}
BeanFactoryPostProcessor就一个方法postProcessBeanFactory,看一下他的实现ConfigurationClassPostProcessorclassConfigurationClassPostProcessorimplementsBeanDefinitionRegistryPostProcessor,PriorityOrdered,ResourceLoaderAware,BeanClassLoaderAware,EnvironmentAware{
ConfigurationClassPostProcessor实现了好几个接口,我们简单说一下:BeanDefinitionRegistryPostProcessor:BeanFactoryPostProcessor的子接口,在常规的BeanFactoryPostProcessor检测之前注册bean的定义信息
PriorityOrdered:设置优先级
ResourceLoaderAware:设置资源加载器,可以获取到外部资源文件
BeanClassLoaderAware:设置类加载器
EnvironmentAware:设置环境变量添加CGLIB增强处理及ImportAwareBeanPostProcessor后置处理类PreparetheConfigurationclassesforservicingbeanrequestsatruntimebyreplacingthemwithCGLIBenhancedsubclasses。OverridepublicvoidpostProcessBeanFactory(ConfigurableListableBeanFactorybeanFactory){intfactoryIdSystem。identityHashCode(beanFactory);if(this。factoriesPostProcessed。contains(factoryId)){thrownewIllegalStateException(postProcessBeanFactoryalreadycalledonthispostprocessoragainstbeanFactory);}this。factoriesPostProcessed。add(factoryId);if(!this。registriesPostProcessed。contains(factoryId)){构建和验证一个类是否被Configuration修饰,并做相关的解析工作processConfigBeanDefinitions((BeanDefinitionRegistry)beanFactory);}增强配置enhanceConfigurationClasses(beanFactory);添加后置处理类beanFactory。addBeanPostProcessor(newImportAwareBeanPostProcessor(beanFactory));}
3、BeanDefinition:bean的定义描述信息;
从加载配置文件后生成bean定义信息,主要就是设置类名,作用域,单例还是原型,是否懒加载等
4、beanPostProcessor:对初始化前后进行处理的类;
主要就两个方法publicinterfaceBeanPostProcessor{初始化方法调用前要进行的处理逻辑Applythis{codeBeanPostProcessor}tothegivennewbeaninstanceibeforeianybeaninitializationcallbacks(likeInitializingBeans{codeafterPropertiesSet}oracustominitmethod)。Thebeanwillalreadybepopulatedwithpropertyvalues。Thereturnedbeaninstancemaybeawrapperaroundtheoriginal。pThedefaultimplementationreturnsthegiven{codebean}asis。parambeanthenewbeaninstanceparambeanNamethenameofthebeanreturnthebeaninstancetouse,eithertheoriginalorawrappedone;if{codenull},nosubsequentBeanPostProcessorswillbeinvokedthrowsorg。springframework。beans。BeansExceptionincaseoferrorsseeorg。springframework。beans。factory。InitializingBeanafterPropertiesSetNullabledefaultObjectpostProcessBeforeInitialization(Objectbean,StringbeanName)throwsBeansException{returnbean;}在初始化方法指定后要进行的处理逻辑Applythis{codeBeanPostProcessor}tothegivennewbeaninstanceiafterianybeaninitializationcallbacks(likeInitializingBeans{codeafterPropertiesSet}oracustominitmethod)。Thebeanwillalreadybepopulatedwithpropertyvalues。Thereturnedbeaninstancemaybeawrapperaroundtheoriginal。pIncaseofaFactoryBean,thiscallbackwillbeinvokedforboththeFactoryBeaninstanceandtheobjectscreatedbytheFactoryBean(asofSpring2。0)。ThepostprocessorcandecidewhethertoapplytoeithertheFactoryBeanorcreatedobjectsorboththroughcorresponding{codebeaninstanceofFactoryBean}checks。pThiscallbackwillalsobeinvokedafterashortcircuitingtriggeredbya{linkInstantiationAwareBeanPostProcessorpostProcessBeforeInstantiation}method,incontrasttoallother{codeBeanPostProcessor}callbacks。pThedefaultimplementationreturnsthegiven{codebean}asis。parambeanthenewbeaninstanceparambeanNamethenameofthebeanreturnthebeaninstancetouse,eithertheoriginalorawrappedone;if{codenull},nosubsequentBeanPostProcessorswillbeinvokedthrowsorg。springframework。beans。BeansExceptionincaseoferrorsseeorg。springframework。beans。factory。InitializingBeanafterPropertiesSetseeorg。springframework。beans。factory。FactoryBeanNullabledefaultObjectpostProcessAfterInitialization(Objectbean,StringbeanName)throwsBeansException{returnbean;}
想学习源码的同学可以加一个关注,将推出一系列源码文章,大家一起学习!
公众号回复【redis设计与实现】获取电子书
带孩子移居南美洲厄瓜多尔的国际学校怎样申请?大家好,这里是南瓜君!我将在本头条号持续发布厄瓜多尔国际学校信息,感兴趣的家长朋友记得点个关注哦心今天接着上期提到的基多英国学校展开讲讲基多英国学校报名流程是什么?国内孩子达到什么
乌克兰内政部长在基辅郊外的直升机坠毁事件中丧生大家好在该国与俄罗斯的战争接近11个月大关之际,坠机的原因尚不清楚。尚塔尔达席尔瓦(ChantalDaSilva)和帕特里克史密斯(PatrickSmith)当局称,周三一架直升机
乌克兰直升机坠机致内务部长等至少18人身亡,拜登刚刚回应环球网报道1月18日,一架直升机在乌克兰基辅州坠毁并引发火灾,造成至少18人死亡,其中包括乌内务部长莫纳斯特尔斯基。据法新社报道,美国总统拜登和其夫人吉尔当天晚些时候发表声明,称这
哈兰德挪威不是一个大国,但我们希望能参加2026世界杯直播吧1月18日讯哈兰德接受著名时尚杂志GQ的采访时,谈到了关于曼城挪威国家队的话题。老实说,我不认为曼城在终结能力上有所欠缺。像去年曼城对皇马的欧冠半决赛,看到阿森西奥头球失误,
军事化证据,不顾俄国防部的警告,美星链系统部分交付乌克兰这将成为俄军的合法打击目标,俄国防部曾经发出过这样的强硬警告,这劝退了部分乌克兰的支持者。就像美国星链系统所属的公司,就不再向乌克兰士兵提供军事化的卫星服务。几个月的时间过去了,似
爱尔兰在最具旅行美学的国家排行榜上排名第17位随着情绪板变得越来越受欢迎,打造适合社交媒体的完美假期也变得越来越流行。我们很多人的旅行灵感都来自电影和电视在白莲花度假村第二季大获成功后,去西西里岛旅行的预订量肯定会激增。然而,
食鸡知味!广东名鸡是怎样炼成的?民以食为天,热爱美食的中国人在描述一道美食时可以用上无数美丽的辞藻形容其味道。东门买彘骨,醯酱点橙薤。蒸鸡最知名,美不数鱼蟹,闲散时在周遭转上两圈,陆游就收获了香气喷喷的猪鸡鱼蟹堂
小井盖,大视界以色列人祖鲁尼和他的终极井盖新华社耶路撒冷1月19日电(记者吕迎旭张天朗)为什么大多数井盖是圆的?井盖是不是都是金属做的?井盖上的图案有什么含义?7年前的一天早上,以色列人埃利祖鲁尼在特拉维夫雅孔公园慢跑时,
阿姆斯特丹的今昔从伦敦转乘落地荷兰首府,这个城市只要你从国内去英伦大多是必经之地,笔者来过这里也有十数次了,趁闲暇时间写几句吧阿姆斯特丹,是荷兰最大城市,市区人口约一百余万。位于该国西部。其名称源
包粽子临近过年,刚好今天我休息,妈妈决定包粽子。提前一个晚上把糯米和绿豆泡水,把粽叶洗了,猪肉提前放各种配料腌制好备用。糯米泡水脱皮绿豆泡水今天一大早妈妈就起来忙活了,把提前泡好糯米和绿
白雪公主我们可爱伟大的祖国,幅原辽阔,山川秀丽,如此美丽的大好河山,蕴含着丰富多彩的自然和文化底蕴,江山如此多娇,吸引着万千的游人络绎不绝地前去观赏。很多人都非常喜欢山山水水的,喜欢走走看