Don Rhummy
Don Rhummy

Reputation: 25870

Can @ConditionalOnProperty use a nested property value?

I want to do something like:

@ConditionalOnProperty("${${appname}.someVal}")
@Controller
public class MyController {...}

Where my yaml would have:

appname: myapp
myapp:
  someVal: true

How would I do this?

EDIT: This does not work:

@Value("#{'${appname}.someVal'}")

Upvotes: 0

Views: 827

Answers (1)

Maciej Walkowiak
Maciej Walkowiak

Reputation: 12932

To evaluate SpEL, use @ConditionalOnExpression instead of @ConditionalOnProperty.

Does not look very elegant, but here it is:

@ConditionalOnExpression("#{environment.getProperty('${appname}.someVal') == 'true'}")

environment references a bean that implements org.springframework.core.env.Environment which is available in every Spring Boot application out of the box.

Upvotes: 2

Related Questions