Skip to content
Advertisement

How to prevent Spring from performing variable replacement

I have a configuration property that needs to be sent to another system as part of a post body.

lets say for example if have an application.properties file with

my.property.raw=${parameter:some-identifier}

I also have a @ConfigurationProperties annotated class that might look like this

@ConfigurationProperties(prefix = "my.property")
public class Properties {
    private String raw;
    
    // getters and such
}

The problem i have is that Spring will see ${parameter:some-identifier} and immediately assumes i want it to be injected with a property names “parameter” and if its not there, put “some-identifier”. i have tried to set the property to ${parameter:${parameter:some-identifier}} but Spring seems to have accounted for recursive property injection and it still comes out as “some-identifier”.

I know i can write some code around that and replace the “:” with something else and change it back after the fact but i’m trying to make this work without any code change.

So in summation, Spring boot sees this as a configuration property ${parameter:some-identifier} upon injection into the ConfigurationProperties annotated class, the value ends up being some-identifier. What i want to happen is the value in the Properties class after Spring does its variable replacement process is ${parameter:some-identifier} as this injectable format of a value is intended for a down stream system that is expecting that format. Preferably, without changing any code. Maybe there is some special formatting i can use to tell spring to ignore this specific property.

Thanks for your time! and yes i realize this is probably the opposite of what people normally want from Spring.

Advertisement

Answer

As stated by @Kayaman this is indeed a duplicate of Escape property reference in Spring property file

The solution i pulled from there, with some modification, was this:

${dollar:$}{parameter:some-identifier}

https://stackoverflow.com/a/48897422/4621716

I did this because i also don’t have control over the process that is generating that application.properties beyond changing existing values.

I hope this helps and i wish i could give @Kayaman credit for pointing me in the right direction but i guess i’ll accept my own answer. Thanks.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement