38

I want to define default property value in Spring XML configuration file. I want this default value to be null.

Something like this:

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

This doesn't work. Is it even possible to define null default values for properties in Spring XML configuration?

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
Ondrej Bozek
  • 10,987
  • 7
  • 54
  • 70
  • You answered your own question. Use – ooops May 24 '13 at 12:39
  • It doesn't work this way: `Invalid content starting with element 'null'. No child element is expected at this point.` – Ondrej Bozek May 24 '13 at 12:42
  • i don't think you can do that. Maybe removing those properties will be enough. Java Properties returns null if a property is not defined. – soulcheck May 24 '13 at 12:53
  • If property is missing in the property file and I don't define default value, problem arises when I try to reference this missing property: `Could not resolve placeholder 'email.username' in string value "${email.username}"` – Ondrej Bozek May 24 '13 at 13:14
  • 3
    You could use ${email.username:null} to set null as the default, although that may give you the string "null". Or, if you are using @Value, you could add @Autowired(required = false), then any missing properties will be set to null. – Tom McIntyre May 24 '13 at 13:53
  • I think you cant define property as null and use property placeholders in XML. Properties will return null if property is not exists, and if you return null this will mean what Spring also treat it if property not exists. I think this is also related to https://jira.springsource.org/browse/SPR-7294 – YoK May 24 '13 at 14:11

4 Answers4

108

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

Anton Kirillov
  • 1,306
  • 1
  • 9
  • 12
4

You can try use Spring EL.

<prop key="email.username">#{null}</prop>
YoK
  • 1,606
  • 17
  • 23
  • This doesn't work. `prop` doesn't have `value` attribute. I think that if there is a solution, something else than `prop` has to be used. – Ondrej Bozek May 24 '13 at 12:53
  • You can place EL between tags, modified anwser. – YoK May 24 '13 at 13:00
  • 3
    This way it throws NPE `Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'defaultConfiguration': Initialization of bean failed; nested exception is java.lang.NullPointerException` – Ondrej Bozek May 24 '13 at 13:10
4

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
Andrei Cojocaru
  • 151
  • 1
  • 6
3

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;
}
EpicPandaForce
  • 79,669
  • 27
  • 256
  • 428