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

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

Related

How can i refer one property file from another property file in spring cloud config

I have to implement spring cloud config for an existing project where i have an use case to handle
Here Some property files are referred by other property files like
logging.propFile=classpath:/cfg/xyz.properties and these property files are used in multiple places like
Properties property = new Properties();
property.load(new FileInputStream(propsPath));
logger = somefactory.createfactory(property.get("logging.propFile")); and this factory creation is defined in a different jar.
If i move all property files in git repo files i mentioned will not be available in class path and if i go for code change a lot of code change is involved.What is the best way to handle this situation.
Is there any way to refer one property file to another in spring cloud config .
Spring Cloud Config server allows for reading static files through HTTP. So you can put your references properties file into the configuration git repository and reference them through http://<configserver_url>/*/*/<branch_name>/xyz.properties. This only works when your code can handle URLs, so the FileInputStream in your example would not do it.
See https://cloud.spring.io/spring-cloud-config/multi/multi__serving_alternative_formats.html and https://cloud.spring.io/spring-cloud-config/multi/multi__serving_plain_text.html.
Also note the resolvePlaceholders query parameter that defaults to true.
We are using this for keeping our logback.xml configuration in the config server by setting logging.config: http://<configserver_url/*/*/master/logback.xml?resolvePlaceholders=false

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.

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

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?

Resources