【问题标题】:Spring Profiling弹簧分析
【发布时间】:2017-10-24 14:28:51
【问题描述】:

我有 2 个 URL,其中 1 个专用于 Dev,另一个专用于 Prod。 我也在使用 Spring 分析,其中我有单独的文件用于 dev 和 prod application-dev.properties 和 application-prod.properties 以及我的 开发环境的 appication.properties 文件看起来像这样

spring.profiles.active=dev

现在在我的 java 代码中,我想要一个属性,该属性将根据我使用的弹簧配置文件绑定到适当的值。我该怎么做。

当前的 Java 类:-

//DEV
    private static final String WIND_RESOURCE_EXTRACTOR_URL = "https://localhost:9090/dev";

    //FOR PROD
    //private static final String WIND_RESOURCE_EXTRACTOR_URL = "https://localhost:9090/prod";

所以我想在我的 application-dev.properties 或 application-prod.properties 文件中提及这个属性,我的 java 类应该根据当前的 spring 配置文件选择正确的值。

【问题讨论】:

    标签: java spring spring-profiles


    【解决方案1】:

    您只需要从属性文件中注入值。假设所有设置都已经完成,从属性读取到环境,我假设它已经完成。

        public class MyClazz {
             private final String myUrl;
             @Autowired
             public MyClazz(@Value("${my.url.property.name}") String myUrl){
               this.myUrl = myUrl;
       }
    

    在你的两个属性文件中放置一个my.url.property.name=WhateverValueIwant

    【讨论】:

      猜你喜欢
      • 2017-12-22
      • 1970-01-01
      • 2019-05-03
      • 2011-09-19
      • 2018-09-08
      • 1970-01-01
      • 2017-09-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多