2

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

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

提前致谢!

4

2 回答 2

3

我正在添加编程参数,我们必须添加 vm 参数

于 2015-06-27T17:51:38.823 回答
1

您为什么要尝试使用 Java 配置环境属性?

您可以将所有配置放入 application.properties。然后,如果您想要开发环境,您只需覆盖 application-dev.properties 中所需的属性。

application-prod.properties 中的 prod 也是如此。

然后像使用 -Dspring.profiles.active=dev 一样开始,您将能够使用 @Value 检索值。

于 2015-06-27T11:00:10.023 回答