How can I have Spring Boot only look for config files under the the directories specified by spring.config.location property and not look under the default location as specified in the ConfigFileApplicationListener javadoc.
Setting spring.config.location, causes the ConfigFileApplicationListener to look in both spring.config.location directories and the default locations.
you can use profiles.
in your application.properties you will only have :
spring.profiles.active=other
and have a file in the same folder named application-other.properties where you define your properties.
the default properties can be in a file named application-default.properties and when you want to use it, juste change the value in application.properties to 'default'.
Spring-boot documentation of Profiles
Related
I have below files under resources folder in a standard Spring Boot app .
Spring.active.profile is set to dev
In which order the properties files are read .?
1)application.yml
2)bootstrap.yml
3)application_dev.yml
4)bootstrap_dev.yml
As Spring doc mentions
Profile specific properties are loaded from the same locations as
standard application.properties, with profiles specific files
overriding the default ones
This would mean that first the application.yml is read and then the application_dev.yml is read and overrides values from the default application.yml if needed.
Same for bootstrap.yml and bootstrap-dev.yml
Also as you can see here
bootstrap.yml is loaded before application.yml.
So to answer your question the order should be
bootstrap.yml
bootstrap_dev.yml
application.yml
application_dev.yml
bootstrap files are always the first: bootstrap.yml then bootstrap-{profile}.yml. then application.yml and application-{profile}.yml.
the property values are overridden by the next files so:
a: 1 from application.yml will be overridden by a: 55 from application-{profile}.yml
Is there a way to override Spring's default file extension for the properties file? I know that you can change the name with the spring.config.name setting, and the location with the spring.config.location setting, but it still expects the file to have a filename extension of .properties. So far I have been unable to tell it to use a file like system.props as a properties file.
I've created my application.properties file:
spring.config.additional-location=C:\Users\user\
spring.datasource.url=jdbc:postgresql://<db>:<port>/<db>
I need to feed Spring with an additional file located on C:\Users\user\application.properties:
spring.datasource.username=user
spring.datasource.password=password
As you can see I've tried to use spring.config.additional-location property into my application.properties file.
However, bootstrap tells me that no authentication has been provided.
you can use another file name by specifying a spring.config.name environment property. You can also refer to an explicit location by using the spring.config.location environment property (which is a comma-separated list of directory locations or file paths). The following example shows how to specify a different file name:
$ java -jar myproject.jar --spring.config.name=myproject
Reference URL:
https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html
You can specify your alternative properties using #PropertySources like this:
#PropertySources({
#PropertySource({"classpath:application.properties"}),
#PropertySource(value = {"file:${conf-dir}}/application-override.properties" },ignoreResourceNotFound = true)
})
public class AppConfig {
...
The properties in the bottom PropertySource will override properties from first one, if the file exists.
The documentation says to use file:C:/Users/user/
the "file:" or "classpath:" part is important.
There are numerous other ways to do this, too.
- profiles (and per profile application-<profile>.properties)
- #Configuration + #PropertySource
- ...
I have properties file with name : transactionexpiry.properties in my project's src/main/resources folder.
I am able to read the properties in the code with #PropertySource("classpath:/transactionexpiry.properties")
Now I wan't to add application scope and add environment specific config files as transactionexpiry-dev.properties, transactionexpiry-local.properties, etc
But the same works with application.properties, application-dev.properties, application-local.properties
Is there a way to make it work with my previous set-up?
If you are using spring profiles:
-Dspring.profiles.active=dev
Then you can call the properties file like:
#PropertySource("classpath:/transactionexpiry${spring.profiles.active}.properties")
I'm developing a spring boot application(Let's call this MyLib). It uses spring-cloud-stream. The idea is that this application will be used as a jar by another java application(Let's call this MyApp. It may not be a spring boot). What I'm trying to do is that MyApp will specify the spring.cloud.stream.bindings.<channel>.destination which will be used by the code inside MyLib.
Is this achievable?
Load application.properties from an external jar
I don't know about spring.cloud.stream.bindings specifically, however a spring Boot application can load its application-${profile}.properties from multiple locations including jars on the classpath:
Spring Boot documentation:
SpringApplication will load properties from application.properties
files in the following locations and add them to the Spring
Environment:
A /config subdirectory of the current directory.
The current directory
A classpath /config package
The classpath root
The list is ordered by precedence (properties defined in locations
higher in the list override those defined in lower locations).
...
The default search path
classpath:,classpath:/config,file:,file:config/ is always used,
irrespective of the value of spring.config.location
In other words, the application-${profile}.properties file must be at ./ or ./config/ inside your MyApp.jar.
You may also define additional lookup folders with spring.config.location, however this must be done at runtime:
spring.config.name and spring.config.location are used very early to determine which files
have to be loaded so they have to be defined as an environment
property (typically OS env, system property or command line argument).
Lookup order
You mentioned
:
Overriding spring properties in application.properties
The order with which the properties are considered is the following (from spring documentation):
#TestPropertySource annotations on your tests.
Command line arguments.
Properties from SPRING_APPLICATION_JSON (inline JSON embedded in an environment variable or system property)
ServletConfig init parameters.
ServletContext init parameters.
JNDI attributes from java:comp/env.
Java System properties (System.getProperties()).
OS environment variables.
A RandomValuePropertySource that only has properties in random.*.
Profile-specific application properties outside of your packaged jar (application-{profile}.properties and YAML variants)
Profile-specific application properties packaged inside your jar (application-{profile}.properties and YAML variants)
Application properties outside of your packaged jar (application.properties and YAML variants).
Application properties packaged inside your jar (application.properties and YAML variants).
#PropertySource annotations on your #Configuration classes.
Default properties (specified using SpringApplication.setDefaultProperties).