How make a conditional property in a YML file in Spring Boot?

Viewed 11696

I want to be able to have something like:

some-property: '#{${ENV_VAR_1:} == "someVal" ? boo + "someVal" : ${ENV_VAR_1}}'

Is something like this possible?

When I try this currently, I get the error:

Caused by: java.lang.IllegalArgumentException: Operand must not be null at org.springframework.util.Assert.notNull(Assert.java:198) ~[spring-core-5.1.3.RELEASE.jar:5.1.3.RELEASE] at org.springframework.expression.spel.ast.SpelNodeImpl.(SpelNodeImpl.java:77) ~[spring-expression-5.1.3.RELEASE.jar:5.1.3.RELEASE] at org.springframework.expression.spel.ast.Ternary.(Ternary.java:40) ~[spring-expression-5.1.3.RELEASE.jar:5.1.3.RELEASE] at org.springframework.expression.spel.standard.InternalSpelExpressionParser.eatExpression(InternalSpelExpressionParser.java:183) ~[spring-expression-5.1.3.RELEASE.jar:5.1.3.RELEASE] at org.springframework.expression.spel.standard.InternalSpelExpressionParser.doParseExpression(InternalSpelExpressionParser.java:131) ~[spring-expression-5.1.3.RELEASE.jar:5.1.3.RELEASE] at org.springframework.expression.spel.standard.SpelExpressionParser.doParseExpression(SpelExpressionParser.java:61) ~[spring-expression-5.1.3.RELEASE.jar:5.1.3.RELEASE] at org.springframework.expression.spel.standard.SpelExpressionParser.doParseExpression(SpelExpressionParser.java:33) ~[spring-expression-5.1.3.RELEASE.jar:5.1.3.RELEASE] at org.springframework.expression.common.TemplateAwareExpressionParser.parseExpressions(TemplateAwareExpressionParser.java:121) ~[spring-expression-5.1.3.RELEASE.jar:5.1.3.RELEASE] at org.springframework.expression.common.TemplateAwareExpressionParser.parseTemplate(TemplateAwareExpressionParser.java:62) ~[spring-expression-5.1.3.RELEASE.jar:5.1.3.RELEASE] at org.springframework.expression.common.TemplateAwareExpressionParser.parseExpression(TemplateAwareExpressionParser.java:49) ~[spring-expression-5.1.3.RELEASE.jar:5.1.3.RELEASE] at org.springframework.context.expression.StandardBeanExpressionResolver.evaluate(StandardBeanExpressionResolver.java:142) ~[spring-context-5.1.3.RELEASE.jar:5.1.3.RELEASE] ... 51 common frames omitted

4 Answers

you can also do like below using SPEL in YML -

some-property: '#{"${ENV_VAR_1:}".equals("someVal")?"boo" + "someVal":"${ENV_VAR_1:}"}'

I think you can use multi-profile instead of if/else. like this:

---
spring:
    profiles: VAR_1
some-property: boo
---
spring:
    profiles: VAR_2, VAR_3
some-property: foo

You're definitely on the right track; you're passing in a SPEL expression that looks like it's attempting to get evaluated.

It appears that one of the operands is null (hence the Operand must not be null).

You'll have to very this, but it looks like:

${ENV_VAR_1:}

Is evaluating to null. It is clearly parsing a ternary, which is part of your stacktrace, but one of those operands needs to have a value. I'd create a default for the ENV_VARs, like ${ENV_VAR_1:default} and see if the parsing works for you. Otherwise, check if boo is null and test with a static value for that.

In your application.yaml add an environment variable reference to your spring.profiles.active.

Then add profiles for each condition and a profile for the default case:

spring:
  profiles:
    active:
      - ${ENV_VAR_1:default}


---
spring:
  profiles: default

some-property: defaultValue

---
spring:
  profiles: env1

some-property: env1Value

---
spring:
  profiles: env2

some-property: env2Value  
Related