如何将配置文件激活为环境变量
How to active profile as environment variable
我在 spring 引导应用程序中使用 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
所以请帮我看看我错过了什么?
提前致谢!
为什么要尝试使用 Java 配置环境属性?
您可以将所有配置放入 application.properties。
然后,如果你想要开发环境,你只需覆盖你想要的属性 application-dev.properties.
应用程序中的产品相同-prod.properties。
然后像使用 -Dspring.profiles.active=dev 一样开始,您将能够使用 @Value 检索值。
我正在添加编程参数,我们必须添加 vm 参数
我在 spring 引导应用程序中使用 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
所以请帮我看看我错过了什么?
提前致谢!
为什么要尝试使用 Java 配置环境属性?
您可以将所有配置放入 application.properties。 然后,如果你想要开发环境,你只需覆盖你想要的属性 application-dev.properties.
应用程序中的产品相同-prod.properties。
然后像使用 -Dspring.profiles.active=dev 一样开始,您将能够使用 @Value 检索值。
我正在添加编程参数,我们必须添加 vm 参数