报错信息: java.lang.IllegalStateException: No application config found or it’s not a valid config! Please add
但是程序有配置ApplicationConfig Bean,不应该报错,
调试发现,Service注入时,先加载了ReferenceBean,此时还没来得及加载ApplicationConfig,导致报错。
源代码如下:
public class DubboConfig {
private static final Logger logger = LoggerFactory.getLogger(DubboConfig.class);
@Bean
public ApplicationConfig applicationConfig() {
ApplicationConfig applicationConfig = new ApplicationConfig();
applicationConfig.setName("application");
return applicationConfig;
}
@Bean
public ConfigCenterConfig configCenterConfig() {
try {
ConfigCenterConfig configCenterConfig = new ConfigCenterConfig();
String namespace = ConfigureLoader.getInstance().getValue("dubbo.config-center.namespace");
String serverAddr = ConfigureLoader.getInstance().getValue("dubbo.config-center.address");
configCenterConfig.setAddress(serverAddr);
configCenterConfig.setNamespace(namespace);
configCenterConfig.setCheck(false);
return configCenterConfig;
} catch (Exception e) {
logger.error("init dubbo config center error {}", e.getMessage());
}
return null;
}
@Bean
public ChannelRuleQueryService channelRuleQueryService() {
try {
ReferenceBean<ChannelRuleQueryService> referenceBean = new ReferenceBean<>();
referenceBean.setInterface(ChannelRuleQueryService.class);
referenceBean.setCheck(false);
return referenceBean.get();
} catch (Exception e) {
logger.error("get channel rule query service error {} ", e.getMessage());
}
return null;
}
解决办法:添加@DependsOn注解,指定先加载配置bean。
@DependsOn({"applicationConfig","configCenterConfig"})
@Bean
public ChannelRuleQueryService channelRuleQueryService() {
try {
ReferenceBean<ChannelRuleQueryService> referenceBean = new ReferenceBean<>();
referenceBean.setInterface(ChannelRuleQueryService.class);
referenceBean.setCheck(false);
return referenceBean.get();
} catch (Exception e) {
logger.error("get channel rule query service error {} ", e.getMessage());
}
return null;
}