报错信息: java.lang.IllegalStateException: No application config found or it’s not a valid config! Please add to your spring config
    但是程序有配置ApplicationConfig Bean,不应该报错,
    调试发现,Service注入时,先加载了ReferenceBean,此时还没来得及加载ApplicationConfig,导致报错。
    源代码如下:

    1. public class DubboConfig {
    2. private static final Logger logger = LoggerFactory.getLogger(DubboConfig.class);
    3. @Bean
    4. public ApplicationConfig applicationConfig() {
    5. ApplicationConfig applicationConfig = new ApplicationConfig();
    6. applicationConfig.setName("application");
    7. return applicationConfig;
    8. }
    9. @Bean
    10. public ConfigCenterConfig configCenterConfig() {
    11. try {
    12. ConfigCenterConfig configCenterConfig = new ConfigCenterConfig();
    13. String namespace = ConfigureLoader.getInstance().getValue("dubbo.config-center.namespace");
    14. String serverAddr = ConfigureLoader.getInstance().getValue("dubbo.config-center.address");
    15. configCenterConfig.setAddress(serverAddr);
    16. configCenterConfig.setNamespace(namespace);
    17. configCenterConfig.setCheck(false);
    18. return configCenterConfig;
    19. } catch (Exception e) {
    20. logger.error("init dubbo config center error {}", e.getMessage());
    21. }
    22. return null;
    23. }
    24. @Bean
    25. public ChannelRuleQueryService channelRuleQueryService() {
    26. try {
    27. ReferenceBean<ChannelRuleQueryService> referenceBean = new ReferenceBean<>();
    28. referenceBean.setInterface(ChannelRuleQueryService.class);
    29. referenceBean.setCheck(false);
    30. return referenceBean.get();
    31. } catch (Exception e) {
    32. logger.error("get channel rule query service error {} ", e.getMessage());
    33. }
    34. return null;
    35. }

    解决办法:添加@DependsOn注解,指定先加载配置bean。

    1. @DependsOn({"applicationConfig","configCenterConfig"})
    2. @Bean
    3. public ChannelRuleQueryService channelRuleQueryService() {
    4. try {
    5. ReferenceBean<ChannelRuleQueryService> referenceBean = new ReferenceBean<>();
    6. referenceBean.setInterface(ChannelRuleQueryService.class);
    7. referenceBean.setCheck(false);
    8. return referenceBean.get();
    9. } catch (Exception e) {
    10. logger.error("get channel rule query service error {} ", e.getMessage());
    11. }
    12. return null;
    13. }