欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

spring 或者spring boot 調(diào)整bean加載順序的方式

 更新時(shí)間:2023年03月01日 10:37:02   作者:zhongzunfa  
這篇文章主要介紹了spring 或者spring boot 調(diào)整bean加載順序的方式,本文通過實(shí)例代碼講解三種調(diào)整類加載順序的方式,代碼簡(jiǎn)單易懂,對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

spring 或者spring boot 調(diào)整bean 的加載順序

接下來講解三種調(diào)整類加載順序的方式:

1、使用@Order調(diào)整配置類加載順序

@Configuration
@Order(1)
public class ConfigA {
    @Bean
    public ServiceA serviceA(){
        System.out.println("ConfigA 執(zhí)行");
        return new ServiceA();
    }
}
@Configuration
@Order(2)
public class ConfigB {
    @Bean
    public ServiceB serviceB(){
        System.out.println("ConfigB 執(zhí)行");
        return new ServiceB();
    }
}

2、使用@Order調(diào)整配置類加載順序

@Configuration
public class Config {
    @Bean
    @Order(1)
    public CListener bListener(){
        return new CListener();
    }

     @Bean
     @Order(2)
    public DListener dListener(){
        return new DListener();
    }
}

3、實(shí)現(xiàn)ordered 接口:

public class AListener implements ApplicationListener<ContextRefreshedEvent>, Ordered {

    private CuratorManagerComponent curatorManagerComponent;

    @Override
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {

        logger.info("AListener 執(zhí)行了。");

    }

    @Override
    public int getOrder() {

        return HIGHEST_PRECEDENCE;
    }
}
public class BListener implements ApplicationListener<ContextRefreshedEvent>, Ordered {

    private CuratorManagerComponent curatorManagerComponent;

    @Override
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {

        logger.info("BListener 執(zhí)行了。");

    }

    @Override
    public int getOrder() {

        return HIGHEST_PRECEDENCE + 2;
    }
}

@Configuration
public class Config {

    @Bean
    public BListener bListener(){
        return new BListener();
    }

     @Bean
    public AListener aListener(){
        return new AListener();
    }
}

對(duì)于實(shí)現(xiàn)ordered方式, 的需要在 getOrder 返回?cái)?shù)值, 數(shù)值越小說明優(yōu)先級(jí)越高。

到此這篇關(guān)于spring 或者spring boot 調(diào)整bean加載順序的方式的文章就介紹到這了,更多相關(guān)spring boot bean加載順序內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論