1

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>
Imar
  • 499
  • 6
  • 9
  • See the veeery closely related question http://stackoverflow.com/questions/9347929/spring-set-a-property-only-if-the-value-is-not-null. – ben3000 Nov 30 '15 at 02:56

2 Answers2

1
#{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.

Imar
  • 499
  • 6
  • 9
0

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

Community
  • 1
  • 1
Lucas Oliveira
  • 833
  • 1
  • 10
  • 24