spring - 如何激活配置文件作为环境变量

标签 spring spring-boot spring-profiles

我在 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"

Bootstrap application

现在,当我尝试运行我的应用程序时,出现错误:

expected single matching bean but found 2: productionConfig,developmentConfig

所以请帮助我我错过了什么?

提前致谢!

最佳答案

我正在添加编程参数,我们必须添加虚拟机参数

关于spring - 如何激活配置文件作为环境变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31087074/

相关文章:

java - Aspect 停用 Spring Controller Mapping

java - org.springframework.beans.factory.BeanCreationException : Error creating bean with name 'XXX'

java - 如何在Spring Boot中从实体类调用基于服务的类

maven - Spring Boot 始终使用相同的配置文件

java - 密码验证服务

java - Spring启动时出现firebird数据库错误

java - Spring Boot - 返回带有对象数组的 JSON

java - 如何在 spring boot 中以编程方式设置 defaultLocale

unix - Spring Profile值混杂

java - 无法使用 Spring Boot 加载配置文件特定的属性文件