How to get Spring property with embedded property variable as a literal - spring

I currently have a Spring application.properties with 2 properties defined as follows:
validator.url=http://location.com/${domain}/${family}
query.validator.url=${validator.url}
Currently my application resolves query.validator.url to be ${validator.url}
is there anyway that I can have it resolve to the same value as validator.url which is http://location.com/${domain}/${family}
Note: ${domain} and ${family} don't resolve, they are handled in code.

Try the following:
validator.url=http://location.com/#{'$'}{domain}/#{'$'}{family}
query.validator.url=${validator.url}
Both validator.url and query.validator.url will resolve to http://location.com/${domain}/${family}
Alternatively, you can create a property for the $, which you can then use inside validator.url, e.g.:
var=$
validator.url=http://location.com/${var}{domain}/${var}{family}
There's an issue regarding this on Spring's JIRA

Related

Observation on the priority order of application properties in Spring Boot

When using Spring Cloud config server, I have observed the below behavior. Please let me know, if my hypothesis is correct regarding the behavior.
If the application-${env}.yaml/properties have the server.port property set, I cannot override the value, even by passing -Dserver.port
If I do not inherit the property defined in the spring cloud config, then I will be able to provide the value inside the application.yaml/property of the application
If the property is defined inside the application's application.property/yaml, I can override the value from the command line by passing the -Dserver.port option.
Is my assumptions right based on the above behavior.
Yes, spring cloud config value can't overwrite by default . We can change to override with property pring.cloud.config.allowOverride=true
https://cloud.spring.io/spring-cloud-static/spring-cloud.html#overriding-bootstrap-properties

Custom application property to be supplied to spring boot app through cmd line

I was wondering if we can supply a custom attribute (a key to be in application.properties file), I know for sure that -Dserver.port=8080 works, and overrides the property value, but server.port is a spring boot's expected property value.
How about something other than that, for example a jdbc connection string or service name? does -Ddb.service.name=dbservice work?
Yes, any property can be set via system property. You can use -D or -- notation. There are also a variety of property sources Spring Boot uses:
https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html

How to get access and customize Freemarker's Configuration object in spring-boot 2.x?

How to get access and customize Freemarker's Configuration object in spring-boot 2.x?
It allows to set square bracket syntax as default option with something like:
Configuration#setTagSyntax(Configuration.SQUARE_BRACKET_TAG_SYNTAX)
which is not possible with any of spring.freemarker.* configuration property.
Also, it should be possible to introduce default imports having access to direct configuration of this object.
It's possible to set any FreeMarker configuration settings with spring.freemarker.settings.<settingName>, like spring.freemarker.settings.tagSyntax = square_bracket. See the JavaDoc of Configuration.setSetting(String, String) for more (https://freemarker.apache.org/docs/api/freemarker/core/Configurable.html#setSetting-java.lang.String-java.lang.String-). This is the method to which Spring delegates the assignments under spring.freemarker.settings; Spring itself doesn't know what "settings" exist or how to parse them.

Mule Expression component not able to fetch value from properties file

I want to fetch values dynamically from properties so I have implemented one poc. In that poc I have declared one object with value in mule expression component. After that I am fetching the value key from properties file. It is showing exceptions while testing the application.
Exception MSG: Root Exception stack trace: unresolvable property or identifier: $
EX-1:
flowVars.deptCode=21432143;
property3=${flowVars.deptCode};
EX-2:
property3=${21432143};
In the above two examples ex-2 has worked fine and ex-1 has failed .
Please let me know if anyone have clarity on that.
Thanks,
Praveen
Mule is using Spring Properties which can be kept in a seperate properties file and then retrieved/used in your application via ${propertyName}.
A property placeholder is used to define where you keep those properties.
Ex 1 is not possible because properties are not aware at all of any variables or properties inside of your Mule application.
Another issue is that those files will be loaded when the application is started.
If you change the value of a property a restart of your application is needed, so your approach isn't going to work.
More info in the docs here:
https://docs.mulesoft.com/mule-user-guide/v/3.8/configuring-properties
You can use dataweave script to dynamically read values from property file
#[dw("p(flowVars.deptCode)")]

Variables in property files in spring boot

Can I use a propertie inside a application.properties?
sample:
myLevel=ERROR
logging.level.org.springframework=$myLevel
logging.level.org.apache.catalina=$myLevel
tks
You might use property placeholders:
The values in application.properties are filtered through the existing Environment when they are used so you can refer back to previously defined values (e.g. from System properties).
For your case:
myLevel=ERROR
logging.level.org.springframework=${myLevel}
logging.level.org.apache.catalina=${myLevel}
See also:
Spring Boot - Placeholders in properties

Resources