Specify default property value as NULL in Spring

半世苍凉 提交于 2019-12-04 22:16:02

It is better to use Spring EL in such way

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

it checks whether email.password is specified and sets it to null (not "null" String) otherwise

have a look at PropertyPlaceholderConfigurer#setNullValue(String)

It states that:

By default, no such null value is defined. This means that there is no way to express null as a property value unless you explictly map a corresponding value

So just define the string "null" to map the null value in your PropertyPlaceholderConfigurer:

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

Now you can use it in your properties files:

db.connectionCustomizerClass=null
db.idleConnectionTestPeriod=21600

You can try use Spring EL.

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

It appears you can do the following:

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

and

@Bean
public static PropertySourcesPlaceholderConfigurer propertyConfig() {
    PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer = new PropertySourcesPlaceholderConfigurer();
    propertySourcesPlaceholderConfigurer.setNullValue("null");
    return propertySourcesPlaceholderConfigurer;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!