[英]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.