Darius X.
Darius X.

Reputation: 2937

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

Upvotes: 28

Views: 28983

Answers (3)

Magnus
Magnus

Reputation: 8300

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.

Upvotes: 26

freemanpolys
freemanpolys

Reputation: 1998

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.

Upvotes: 1

Oleg
Oleg

Reputation: 6314

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.

Upvotes: 5

Related Questions