Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specify default property value as NULL in Spring

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?

like image 951
Ondrej Bozek Avatar asked May 24 '13 12:05

Ondrej Bozek


People also ask

Can a default value be null?

If no default value is declared explicitly, the default value is the null value. This usually makes sense because a null value can be considered to represent unknown data. In a table definition, default values are listed after the column data type.

How do I set default value in spring?

To set a default value for primitive types such as boolean and int, we use the literal value: @Value("${some. key:true}") private boolean booleanWithDefaultValue; @Value("${some.

What is @value annotation in spring?

Spring @Value annotation is used to assign default values to variables and method arguments. We can read spring environment variables as well as system variables using @Value annotation. Spring @Value annotation also supports SpEL.


1 Answers

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

like image 173
Anton Kirillov Avatar answered Oct 11 '22 03:10

Anton Kirillov