简体   繁体   English

覆盖application.properties中的值

[英]Override value in application.properties

I write desktop application which connected with API client and this API forces setting URL value 我编写了与API client连接的桌面应用程序,并且此API强制设置URL值

@Value("${ig.api.domain.URL}")
private String igApiDomainURL;

Setting igApiDomainURL showing above is in AbstractService.class of client api libraries so I can't change it. 设置igApiDomainURL显示的igApiDomainURL在客户端api库的AbstractService.class ,因此无法更改。

I create BeanConfiguration.java which load application.properties where the ig.api.domain.URL was defined. 我创建BeanConfiguration.java其负载application.properties其中ig.api.domain.URL定义。

BeanConfiguration.java looks like this: BeanConfiguration.java看起来像这样:

@Configuration
@PropertySource("application.properties")
public class BeanConfiguration {

    @Bean
    public static PropertySourcesPlaceholderConfigurer placeholderConfigurer() {
        return new PropertySourcesPlaceholderConfigurer();
    }

    @Bean
    public HttpClient httpClient() {
        return HttpClients.createDefault();
    }
}

... and application.properties contains: ...和application.properties包含:

ig.api.domain.URL=https://demo-api.ig.com/gateway/deal

I want change URL address defined in application.properties during riunning application (change the URL address depending on the type of account - DEMO/LIVE). 我想在运行application.properties期间更改application.properties中定义的URL地址(根据帐户类型-DEMO / LIVE更改URL地址)。

Any suggestions ? 有什么建议么 ?

After a long discussion, what finally worked for this very specific scenario was doing something like this: 经过长时间的讨论,最终对于这种特定情况起作用的是执行以下操作:

Create a property file for each possible profile you want, like: 为所需的每个可能的配置文件创建一个属性文件,例如:

application-dev.properties
application-prod.properties

Properties content example: 属性内容示例:

property.i.want=abcd

Set the env before creating the ApplicationContext: 在创建ApplicationContext之前设置环境:

System.setProperty("spring.profiles.active", "dev");
ClassPathXmlApplicationContext  context =
                new ClassPathXmlApplicationContext(new String[] {"spring.xml"});

Then set the property source manually, ie: 然后手动设置属性源,即:

@Bean
public static PropertySourcesPlaceholderConfigurer placeholderConfigurer() throws IOException {
    String profile = System.getProperty("spring.profiles.active");
    PropertySourcesPlaceholderConfigurer pspc = new PropertySourcesPlaceholderConfigurer();
    Resource resource = new ClassPathResource(String.format("application-%s.properties", profile));
    Properties props = PropertiesLoaderUtils.loadProperties(resource);
    pspc.setProperties(props);
    pspc.setPropertySources();
    return pspc;
}

Definitly not the prettiest solution though. 绝对不是最漂亮的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 mvn test - 覆盖 application.properties 中的值 - mvn test - override values in application.properties 子应用程序中的application.properties文件不会覆盖主应用程序中的application.properties文件 - application.properties file from child application not override application.properties file in main application 使用动态值覆盖 Junit 测试中的默认 Spring-Boot application.properties 设置 - Override default Spring-Boot application.properties settings in Junit Test with dynamic value @Value 没有从 application.properties 获取值 - @Value not getting the value from application.properties 用application.properties覆盖默认的Spring-Boot application.properties - Override default Spring-Boot application.properties with application.properties 寻找一种在应用程序运行时覆盖 application.properties 值的方法 - Looking for a way to override application.properties values during application runtime 在接口注释中使用application.properties值 - Use application.properties value in interface annotation 无法使用application.properties设置值 - Failed to set value with application.properties 来自 application.properties 的 Mapstruct 值 - Mapstruct value from application.properties 从 .txt 读取值到 application.properties - read value from .txt to application.properties
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM