【问题标题】:Specify default property value as NULL in Spring在 Spring 中将默认属性值指定为 NULL
【发布时间】:2013-05-20 01:33:47
【问题描述】:

我想在 Spring XML 配置文件中定义默认属性值。我希望这个默认值是null

类似这样的:

...
<ctx:property-placeholder location="file://${configuration.location}" 
                          ignore-unresolvable="true" order="2" 
                          properties-ref="defaultConfiguration"/>

<util:properties id="defaultConfiguration">
    <prop key="email.username" >
        <null />
    </prop>  
    <prop key="email.password">
        <null />
    </prop>  
</util:properties>
...

这不起作用。是否可以在 Spring XML 配置中为属性定义 null 默认值?

【问题讨论】:

  • 您回答了自己的问题。使用
  • 这样不行:Invalid content starting with element 'null'. No child element is expected at this point.
  • 我认为你做不到。也许删除这些属性就足够了。如果未定义属性,Java Properties 将返回 null。
  • 如果属性文件中缺少属性并且我没有定义默认值,当我尝试引用这个缺少的属性时会出现问题:Could not resolve placeholder 'email.username' in string value "${email.username}"
  • 您可以使用 ${email.username:null} 将 null 设置为默认值,尽管这可能会给您字符串“null”。或者,如果你使用@Value,你可以添加@Autowired(required = false),那么任何缺少的属性都将被设置为null。

标签: spring properties null default-value


【解决方案1】:

最好这样使用Spring EL

<property name="password" value="${email.password:#{null}}"/>

它检查是否指定了email.password,否则将其设置为null(不是"null" String)

【讨论】:

  • 完美!这是否引入了对 spring-el 的依赖?
  • 太棒了!对于那些不知道这也适用于 Spring Annotations 的人。 @Value("${email.password:#{null}}") 私有字符串密码;
【解决方案2】:

看看PropertyPlaceholderConfigurer#setNullValue(String)

它指出:

默认情况下,没有定义这样的空值。这意味着除非您明确映射相应的值,否则无法将 null 表示为属性值

所以只需定义字符串“null”来映射您的 PropertyPlaceholderConfigurer 中的 null 值:

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="nullValue" value="null"/>
    <property name="location" value="testing.properties"/>
</bean>

现在您可以在属性文件中使用它了:

db.connectionCustomizerClass=null
db.idleConnectionTestPeriod=21600

【讨论】:

  • 这也意味着你将不能使用字符串“null”
  • 老实说,谁设置了明确的"null" 字符串而不是null
  • Hibernate 属性 db.connectionCustomizerClass:我想设置 com.mchange.v2.c3p0.example.InitSqlConnectionCustomizer 或 null。设置是通过 Maven 过滤配置的。
【解决方案3】:

您可以尝试使用 Spring EL。

<prop key="email.username">#{null}</prop>

【讨论】:

  • 这不起作用。 prop 没有 value 属性。我认为如果有解决方案,必须使用 prop 以外的其他东西。
  • 标签之间可以放置EL,修改anwser。
  • 这样会抛出 NPE Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'defaultConfiguration': Initialization of bean failed; nested exception is java.lang.NullPointerException
【解决方案4】:

看来您可以执行以下操作:

@Value("${some.value:null}")
private String someValue;

@Bean
public static PropertySourcesPlaceholderConfigurer propertyConfig() {
    PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer = new PropertySourcesPlaceholderConfigurer();
    propertySourcesPlaceholderConfigurer.setNullValue("null");
    return propertySourcesPlaceholderConfigurer;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-01-31
    • 1970-01-01
    • 2012-12-15
    • 2010-10-16
    • 1970-01-01
    • 1970-01-01
    • 2020-04-07
    • 2014-12-31
    相关资源
    最近更新 更多