我在 Spring Boot 应用程序中使用 Spring 配置文件配置环境变量。我在那里做了配置
我的界面是
public interface EnvConfiguration {
String getServerUrl();
}
我的开发配置是
@Component
public class DevelopmentConfig implements EnvConfiguration{
@Value("${DEV}")
private String serverUrl;
@Override
public String getServerUrl(){
return serverUrl;
}
}
@Configuration
@Profile("dev")
public class DevelopmentProfile {
@Bean
public EnvConfiguration getDevelopmentConfig(){
return new DevelopmentConfig();
}
}
与我为生产环境配置的相同
@Component
public class ProductionConfig implements EnvConfiguration {
@Value("${PROD}")
private String serverUrl;
@Override
public String getServerUrl(){
return serverUrl;
}
}
@Configuration
@Profile("prod")
public class ProductionProfile {
@Bean
public EnvConfiguration getProductionConfig(){
return new ProductionConfig();
}
}
现在我使用运行配置->agruments 在 Eclipse 中配置环境变量
-Dspring.profiles.active="dev"
现在,当我尝试运行我的应用程序时,出现错误:
expected single matching bean but found 2: productionConfig,developmentConfig
所以请帮助我我错过了什么?
提前致谢!
最佳答案
我正在添加编程参数,我们必须添加虚拟机参数
关于spring - 如何激活配置文件作为环境变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31087074/