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 {
// ...
}
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
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;
}
}
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.
So write above property on application.properties like this.
spring.config.location=file:C:\\properties\\application.yml
spring.config.name=application
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;
}
}
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.
Since you are using spring boot 1.4.2, everything is simple. You just need specify additional argument for your application, such as java -jar name-of-application-version.jar --spring.config.location=file:///C:/properties/application.yml
. A more common method to achieve this is defining additional option for JVM like java -Dspring.config.location=file:///d:/private-bootstrap.yml -jar name-of-application-version.jar
. Both of these two methods sould work, cause they work fine on my desktop.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With