Imar
Imar

Reputation: 589

Spring SpEL - set a property only if the value is not null

I'm using Spring and I want to set a property only if the value passed is not null?

I tried this example but it doesn't work. I want to add the property only if it is not null else not add it. I don't want to add a default value. Thanks for your help

<util:properties id="mailProperties" location="classpath:mail.properties"/>
  <property name="javaMailProperties">
        <props>
            <prop key="mail.smtp.starttls.enable">#{mailProperties['mail.smtp.starttls.enable']:''}</prop>             
           </props>
    </property>

Upvotes: 1

Views: 3477

Answers (2)

Imar
Imar

Reputation: 589

#{mailProperties['mail.smtp.starttls.enable']==null ? false: mailProperties['mail.smtp.starttls.enable']}

Here is an example To set a default value: if value is null I set it to false else I set it to its value.

Upvotes: 1

Lucas Oliveira
Lucas Oliveira

Reputation: 883

I belive you want something like this

${mail.smtp.starttls.enable:defaultValue}

For more details: Is there a way to specify a default property value in Spring XML?

https://jira.spring.io/browse/SPR-4785

Upvotes: 0

Related Questions