Search code examples
javaspringspring-bootyamlspring-config

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'}")

Solution

  • 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.