We are using a spring boot application, where properties are loaded from application.yml
file instead of application.properties
, located at src/main/resources/
which looks like below:
config:
host: localhost:8080
server: 123
And they are being pulled in a .java
file like this
@ConfigurationProperties( prefix="config")
public class ConnectionImpl implements Connection{
@Value("${config.host}")
private Stringhost;
}
I am able to retrieve properties this way.
But we are trying to move the config properties from application.yml
to a different .yml
file which is located at a different location. (src/main/resources/env-config
).
Now I am not able to retrieve properties same way, i.e, using @Value
annotation. Is there any other annotation I need to add ?
From the documentation:
SpringApplication
will load properties fromapplication.properties
(orapplication.yml
) files in the following locations and add them to the SpringEnvironment
:
- A
/config
subdirectory of the current directory.- The current directory
- A classpath
/config
package- The class path root
If you don’t like
application.properties
as the configuration file name you can switch to another by specifying a spring.config.name environment property. You can also refer to an explicit location using thespring.config.location
environment property (comma-separated list of directory locations, or file paths).The default search path
classpath:,classpath:/config,file:,file:config/
is always used, irrespective of the value ofspring.config.location
. This search path is ordered from lowest to highest precedence (file:config/
wins). If you do specify your own locations, they take precedence over all of the default locations and use the same lowest to highest precedence ordering. In that way you can set up default values for your application inapplication.properties
(or whatever other basename you choose withspring.config.name
) and override it at runtime with a different file, keeping the defaults.
You need to supply a command line argument that tells SpringApplication
where specifically to look. If everything in resources/
is added to the classpath root, then your command line would look like:
java -jar myproject.jar --Dspring.config.location=classpath:/env-config/service-config.yml
If you have a general application.yml
under resources/
, the properties in there will still be loaded but will take a lower precedence to the properties file specified on the command line.
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