Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Injection of null or an empty string as the default value of a property

Tags:

spring

Is it possible to inject a null or blank string as the default value that will be used in a spring file if the property is not specified? Currently the best I have is

<constructor-arg index="1" value="@{data.url:&quot;&quot;}"/>

which resolves to "" in code

like image 244
qwerty Avatar asked Dec 11 '12 09:12

qwerty


People also ask

Can you inject null and empty string values in string?

In Spring dependency injection, we can inject null and empty values. In XML configuration, null value is injected using <null> element.

Can you inject null and empty string values in Spring?

Answer. Yes, Spring allows injecting null or empty String values.

Is null the default value?

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.


1 Answers

Have you tried using SpEL? Something like this maybe:

<constructor-arg index="1" value="#{'${data.url}'==null?'':'${data.url}'}"/>

Update

I just remembered that there's an easier way (as nicely described here). Try:

<constructor-arg index="1" value="${data.url:''}"/>
like image 200
Marcel Stör Avatar answered Sep 28 '22 10:09

Marcel Stör