DAN*_*L K 6 environment spring properties spring-boot
I use spring boot 1.4.2 and now on try to set properties.
I have four cases about using .properties and .yml to external(inside resources) and local file system (outside project folder).
The .properties external (inside resource folder) works well both @Value(${property.name}) and @PropertySource last one use value attribute can load from the file system like below.
@Component
@PropertySource(value="file:C:\\properties\\application-dev.properties")
@PropertySource(value="file:C:\\properties\\application-test.properties")
public class ExternalProperties {
// ...
}
Run Code Online (Sandbox Code Playgroud)
But .yml isn't that works well under resource folder when the file name 'application.yml'
The .yml can be loaded by @ConfigurationProperties and need 'propdeps-plugin'
my .yml file
environments:
dev:
url: http://dev.bar.com
name: Developer Setup
prod:
url: http://foo.bar.com
name: My Cool App
Run Code Online (Sandbox Code Playgroud)
This code works well
@Component
// external(inside resources)
@ConfigurationProperties(prefix="environments")
// outside project folder
@ConfigurationProperties(locations={"file:C:\\properties\\application.yml"}, prefix = "environments")
public class YamlProperties {
private Map<String, String> dev = new HashMap<>();
private Map<String, String> prod = new HashMap<>();
public Map<String, String> getDev() {
return this.dev;
}
public Map<String, String> getProd() {
return this.prod;
}
}
Run Code Online (Sandbox Code Playgroud)
That code has problem of deprecated location attribute (I read many articles but can't figure out clearly) so I need find API doc and find 'ConfigFileApplicationListener' from this section.
# SPRING CONFIG - using environment property only (ConfigFileApplicationListener)
spring.config.location= # Config file locations.
spring.config.name=application # Config file name.
Run Code Online (Sandbox Code Playgroud)
So write above property on application.properties like this.
spring.config.location=file:C:\\properties\\application.yml
spring.config.name=application
Run Code Online (Sandbox Code Playgroud)
and reloading the .yml property (I didn't try excute jar. I use the war and test through Controller)
@Component
@ConfigurationProperties(prefix="environments")
public class YamlProperties {
private Map<String, String> dev = new HashMap<>();
private Map<String, String> prod = new HashMap<>();
public Map<String, String> getDev() {
return this.dev;
}
public Map<String, String> getProd() {
return this.prod;
}
}
Run Code Online (Sandbox Code Playgroud)
This code didn't load the .yml from local file system C: drive but when add application.yml file in the resource folder then works well.
So how to set the spring.config.location for load .yml
And I want to know about why the location attribute works yet although deprecated since 1.4 ver.
and wondering how to use the ConfigFileApplicationListener I can't follow up the code it's hard to understand give some tip~!
EDIT:
I miss understood about this that when the war start again and make context again using the local file system properties. This is not the collectly linked it but remain for future step.
The tomcat restart than war deploy the new one so I have the file on local system it contain properties if I change this file's data that could be udated properties context when tomcat restart.
Why I keep try this work that I use public github account and protect connect db info something else to. I get this stuff and go on next issue like git-encrpt, spring-cloude, nginx, docker. Thank you for any help really helpful.
由于您使用的是 spring boot 1.4.2,所以一切都很简单。您只需要为您的应用程序指定其他参数,例如java -jar name-of-application-version.jar --spring.config.location=file:///C:/properties/application.yml
. 实现此目的的更常见方法是为 JVM 定义附加选项,例如java -Dspring.config.location=file:///d:/private-bootstrap.yml -jar name-of-application-version.jar
. 这两种方法都可以工作,因为它们在我的桌面上工作得很好。
归档时间: |
|
查看次数: |
32533 次 |
最近记录: |