【问题标题】:Spring SpEL - set a property only if the value is not nullSpring SpEL - 仅当值不为空时才设置属性
【发布时间】:2016-02-29 19:51:43
【问题描述】:

我正在使用 Spring,并且我只想在传递的值不为 null 的情况下设置属性?

我试过这个例子,但它不起作用。 我只想添加属性,如果它不为空,否则不要添加它。 我不想添加默认值。 感谢您的帮助

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

【问题讨论】:

标签: java spring spring-el


【解决方案1】:
#{mailProperties['mail.smtp.starttls.enable']==null ? false: mailProperties['mail.smtp.starttls.enable']}

这里是一个示例 设置默认值:如果值为 null,我将其设置为 false,否则我将其设置为它的值。

【讨论】:

    【解决方案2】:

    我相信你想要这样的东西

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

    更多详情: Is there a way to specify a default property value in Spring XML?

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

    【讨论】:

      猜你喜欢
      • 2012-03-10
      • 2016-08-29
      • 1970-01-01
      • 2018-01-15
      • 2021-02-25
      • 1970-01-01
      • 2013-12-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多