亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Bean實(shí)例化之前修改BeanDefinition示例詳解

 更新時間:2022年12月26日 15:25:55   作者:刨紅薯的小羊竿爾  
這篇文章主要為大家介紹了Bean實(shí)例化之前修改BeanDefinition示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

BeanFactory

BeanFactory是Spring中容器功能的基礎(chǔ),用于存放所有已經(jīng)加載的bean,為了保證程序上的高可擴(kuò)展性,Spring針對BeanFactory做了大量的擴(kuò)展。BeanFactoryPostProcessor是BeanFactory的后置處理器;比如在postProcessBeanFactory方法中,可以獲取BeanDefinition的相關(guān)對象,并且修改該對象的屬性。

@FunctionalInterface
public interface BeanFactoryPostProcessor {
    void postProcessBeanFactory(ConfigurableListableBeanFactory var1) throws BeansException;
}

通過BeanDefinition對象實(shí)例化Bean對象

Spring IOC在實(shí)例化Bean對象之前,需要先讀取Bean的相關(guān)屬性,保存到BeanDefinition對象中,然后通過BeanDefinition對象,實(shí)例化Bean對象。如果我們需要修改BeanDefinition對象中的屬性,就可以實(shí)現(xiàn)BeanFactoryPostProcessor接口,重寫postProcessBeanFactory方法進(jìn)行自定義,是不是很方便呢。

    @Component
    public class MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
    
        @Override
        public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException {
            DefaultListableBeanFactory defaultListableBeanFactory = (DefaultListableBeanFactory) configurableListableBeanFactory;
            BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder.genericBeanDefinition(User.class);
            beanDefinitionBuilder.addPropertyValue("id", 123456);
            beanDefinitionBuilder.addPropertyValue("label", "技術(shù)那些事兒");
            defaultListableBeanFactory.registerBeanDefinition("user", beanDefinitionBuilder.getBeanDefinition());
        }
    }

BeanFactoryPostProcessor使用目的:在BeanFactory標(biāo)準(zhǔn)初始化之后調(diào)用,用來定制和修改BeanFactory的內(nèi)容;

BeanFactoryPostProcessor工作時機(jī):所有的bean定義已經(jīng)保存加載到beanFactory中,但bean的實(shí)例還沒創(chuàng)建;

BeanFactoryPostProcessor和BeanPostProcessor類似,可以對beanDefinition進(jìn)行處理,也就是說SpringIOC容器允許BeanFactoryPostProcessor在容器實(shí)際實(shí)例化任何bean之前讀取beanDefinition,并有可能修改它。并且我們還可以配置自定義的BeanFactoryPostProcessor,如果想改變bean,那么使用beanPostProcessor。

并可以通過order屬性來控制BeanFactoryPostProcessor的執(zhí)行次序;因為只有實(shí)現(xiàn)了Ordered接口時,才可以設(shè)置這個屬性,所以在實(shí)現(xiàn)BeanFactoryPostProcessor的時候,就應(yīng)該考慮實(shí)現(xiàn)Ordered接口。

BeanFactoryPostProcessor的作用域是容器級的,只和你使用的容器有關(guān),如果在一個容器中定義了一個BeanFactoryPostProcessor,它僅僅對此容器中的beanDefinition進(jìn)行后處理。BeanFactoryPostProcessor不會對定義在另一個容器中的bean進(jìn)行后處理,即便這兩個容器都在同一個層次上。

BeanFactoryPostProcessor還有一個子接口BeanDefinitionRegistryPostProcessor,其內(nèi)部添加了一個方法postProcessBeanDefinitionRegistry:

void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException;

ConfigurationClassPostProcessor這個類非常重要,其實(shí)現(xiàn)了BeanDefinitionRegistryPostProcessor接口與PriorityOrdered接口:

public class ConfigurationClassPostProcessor implements BeanDefinitionRegistryPostProcessor,
                PriorityOrdered, ResourceLoaderAware, BeanClassLoaderAware, EnvironmentAware 
@Override
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) {
	int registryId = System.identityHashCode(registry);
	if (this.registriesPostProcessed.contains(registryId)) {
		throw new IllegalStateException(
				"postProcessBeanDefinitionRegistry already called on this post-processor against " + registry);
	}
	if (this.factoriesPostProcessed.contains(registryId)) {
		throw new IllegalStateException(
				"postProcessBeanFactory already called on this post-processor against " + registry);
	}
	this.registriesPostProcessed.add(registryId);
	/**
	 * @author lcy
	 * 處理配置beanDefinition
	 */
	processConfigBeanDefinitions(registry);
}

就是在這個方法里將配置類指定的類解析為BeanDefinition對象,并放入BeanFactory的beanDefinitionMap中。還有一點(diǎn)需要說明,這個類是在執(zhí)行AnnotationConfigApplicationContext的無參構(gòu)造方法時被解析為beanDefinition并放入map中的。在AnnotationConfigApplicationContext的refresh()內(nèi)部的 invokeBeanFactoryPostProcessors(beanFactory)方法完成了對 BeanFactoryPostProcessor對象與BeanDefinitionRegistryPostProcessor對象相關(guān)方法的執(zhí)行。

以上就是Bean實(shí)例化之前修改BeanDefinition示例詳解的詳細(xì)內(nèi)容,更多關(guān)于Bean實(shí)例化修改BeanDefinition的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • 淺析java快速排序算法

    淺析java快速排序算法

    這篇文章主要介紹了淺析java快速排序算法,需要的朋友可以參考下
    2015-02-02
  • MyBatis配置與CRUD超詳細(xì)講解

    MyBatis配置與CRUD超詳細(xì)講解

    這篇文章主要介紹了MyBatis配置與CRUD,CRUD是指在做計算處理時的增加(Create)、讀取(Read)、更新(Update)和刪除(Delete)幾個單詞的首字母簡寫。CRUD主要被用在描述軟件系統(tǒng)中數(shù)據(jù)庫或者持久層的基本操作功能
    2023-02-02
  • SpringBoot Mybatis如何配置多數(shù)據(jù)源并分包

    SpringBoot Mybatis如何配置多數(shù)據(jù)源并分包

    這篇文章主要介紹了SpringBoot Mybatis如何配置多數(shù)據(jù)源并分包,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-05-05
  • JAVA實(shí)現(xiàn)JSON后端向前端傳遞數(shù)據(jù)

    JAVA實(shí)現(xiàn)JSON后端向前端傳遞數(shù)據(jù)

    本篇文章主要介紹了JAVA實(shí)現(xiàn)JSON后端向前端傳遞數(shù)據(jù),這里整理了詳細(xì)的代碼,具有一定的參考價值,有需要的小伙伴可以參考下。
    2017-03-03
  • Spring自動配置之condition條件判斷上篇

    Spring自動配置之condition條件判斷上篇

    這篇文章主要為大家介紹了SpringBoot condition條件判斷功能的使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-08-08
  • SpringBoot整合Tomcat連接池的使用

    SpringBoot整合Tomcat連接池的使用

    這篇文章主要介紹了SpringBoot整合Tomcat連接池的使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-04-04
  • SpringBoot實(shí)現(xiàn)阿里云快遞物流查詢的示例代碼

    SpringBoot實(shí)現(xiàn)阿里云快遞物流查詢的示例代碼

    本文將基于springboot實(shí)現(xiàn)快遞物流查詢,物流信息的獲取通過阿里云第三方實(shí)現(xiàn),具有一定的參考價值,感興趣的可以了解一下
    2021-10-10
  • Spring?AOP簡介及統(tǒng)一處理

    Spring?AOP簡介及統(tǒng)一處理

    AOP面向切面編程,它是一種思想,它是對某一類事情的集中處理,本文給大家介紹Spring?AOP簡介及統(tǒng)一處理,感興趣的朋友跟隨小編一起看看吧
    2023-09-09
  • 基于java實(shí)現(xiàn)斗地主代碼實(shí)例解析

    基于java實(shí)現(xiàn)斗地主代碼實(shí)例解析

    這篇文章主要介紹了基于java實(shí)現(xiàn)斗地主代碼實(shí)例解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-07-07
  • Spring中Bean初始化和銷毀的方式總結(jié)

    Spring中Bean初始化和銷毀的方式總結(jié)

    這篇文章主要為大家整理了Spring中Bean初始化和銷毀的多種方式,文中的示例代碼講解詳細(xì),具有一定的借鑒價值,需要的可以了解一下
    2023-04-04

最新評論