Is it possible to reference a specific property from spring boot application.properties in an external property file - spring-boot

In spring boot I have an application.properties file in which I have a property
apple = X.
I have another custom property file Custom.properties. I want to reference apple in this custom property file so I have this:
orange = ${spring.apple}.
But this does not work at runtime.
Is there any way to get access to an application.properties property from another custom properties file?
Edit:
I also tried orange=${apple}, that does not work either. I set spring.config.location to point to the directory that has application.properties and my custom.properties file also. I assumed spring boot would load any properties files it sees in spring.config.location but looks like it does not.
Is there any way to load my custom.properties file in addition to application.properties?

Related

Spring #Value property doesn't read nested YAML properties correctly

I have a simple YAML property policy.statement that I want to read from a configuration java file using #PropertySource and #Value annotations. If I use only #Value(${statement}) it reads ok, but if I use #Value(${policy.statement}) if can't find. And at the YAML file I use policy: [enter] statement: value . Why doesn't #Value work if I use ${property1.property2} but only if I use ${property2} alone ? It doesn't make sense. Print and source link attached.
full project at the "doubt" branch at the project link:
https://github.com/danielpm1982/springboot2-health-record/tree/doubt
[SOLVED]
You can't use PropertySource or PropertySources Spring Annotation with custom named yaml files... only with .properties txt files. So, to solve the problem, instead of using custom named .yaml files I had to use the default named application.yaml. This way there's no need to use PropertySource for the .yaml file (only for the .properties ones) and it all worked fine, including the different profile selection of my yaml file properties according to the profile set at the application.properties .
Thanks to Deadpool:
https://stackoverflow.com/users/9959152/deadpool
Full project available at the master branch of the project link:
https://github.com/danielpm1982/springboot2-health-record

how to load custom properties in application.properties

In spring boot , there is an property file application.property, along with this property , I have created an extra property file named myownprop.properties.
How can I load myownprop.properties in application.property? means how to include another named properties in application.properties?
any update ?
You could use the spring.config.additional-location property as described here: https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html#boot-features-external-config-application-property-files. It allows you to add additional files without messing with the nice default hierarchy of property sources that Spring Boot defines.
You could add a new active profile in your current application.properties
spring:
profiles:
active: dev, additional
and then add a new application-additional.properties file in your resource folder. The entries within the new file are then available as they are in the application.properties.

SpringBoot custom spring.config.location

I have a simple SpringBoot application with the following structure:
I'm using a standard application.yml file where I'm storing all the necessary props and use #ConfigurationProperties annotation to inject them where necessary.
Now for one bean I have quite a lot of props and I don't want to overwhelm my common application.yml file with all that props. So I want a separate one (which I placed under service dir in classpath).
According to Spring docs I can use something like:
java -jar myproject.jar --spring.config.location=classpath:/service/application.yml
But that's not working, I got NullPointer which means property was not injected.
What Am I doing wrong? How can I use another *.yml file together with application.yml?
P.S. I know I could place it under the config folder in classpath, but what if I need two custom files?
If you have 2 configs in different places, spring.config.location will accept a comma separated list of those locations
--spring.config.location=classpath:/resources/,classpath:/service/
You could also just call the other file like "config.yml" and then use a different name
--spring.config.name=application,config

Using Expressions in Spring application.properties file

Can expressions be used as a right-hand-side value in a Spring application.properties file?
For example, something like this:
logging.level.com.acme=#{'${MY_RUN_ENV}'=='PROD'?'WARN':'DEBUG'}
That, specifically, does not work. But, I'm wondering if I can do something similar to what's intended there
No you can not use SpEL within properties files.
Finally, while you can write a SpEL expression in #Value, such
expressions are not processed from Application property files.
You can however use placeholders within properties files, eg:
app.name=MyApp
app.description=${app.name} is a Spring Boot application
For your use case, you should look at the profile-specific configuration mechanism.
Which allows you to load different config based on an environment profile.
No this is not possible, From spring boot reference:
Feature #ConfigurationProperties
SpEL evaluation No
Instead you can have an application-default.properties in production and in it define loglevel=WARN.
And in your application.properties:
loglevel=DEBUG
logging.level.com.acme=${loglevel}
The profile-specific properties file(-default by default) should override the properties from application.properties, more info here.
Use profile based properties file.
In application-dev.properties :
logging.level.com.acme=WARN
and in application-prod.properties :
logging.level.com.acme=DEBUG
FYI when spring boot doesn't find a propertie in a profile based file it use the value in the default one . So you can set properties in application.properties and override them in a profile based file when their value changed.

How do I access Spring properties in a logback configuration

Is there a way to access Spring properties within a logback.xml file?
I know one can import a properties file if you know its location, but I'm using Spring profiles to control where the properties file should be loaded or not.
Is there done kind of connector that asked me to feed Spring data into logback? This would only be at startup; I don't need to be able to do this on the fly.
I'm guessing you do have to import a property file (common property file, non-environment specific one) that will contain the name of the property that you are going to use in the logback.xml, and that you want to optionally override the value of the property for some environment (you need at least one property file containing the name of the property, because you will be using that property in the logback.xml, and you need it to be available to be able to use it).
For the optional environment-override, how about including an additional property file? For example, we use both application.properties and application-${spring.profiles.active}.properties files. Then if we don't need to override the property for some environment, we simply don't include it in the environment specific property file (application-dev.properties, etc.)

Resources