Note that there are some explanatory texts on larger screens.

plurals
  1. POSpring - set a property only if the value is not null
    text
    copied!<p>When using Spring, is it possible to set a property only if the value passed is not null?</p> <p>Example:</p> <pre><code>&lt;bean name="myBean" class="some.Type"&gt; &lt;property name="abc" value="${some.param}"/&gt; &lt;/bean&gt; </code></pre> <p>The behavior I'm looking for is:</p> <pre><code>some.Type myBean = new some.Type(); if (${some.param} != null) myBean.setAbc(${some.param}); </code></pre> <p>The reason I need this is since <code>abc</code> has a default value which I don't want to override with a <code>null</code>. And the Bean I am creating is not under my source control - so I cannot change its behavior. (Also, <code>abc</code> for this purpose might be a primitive, so I can't set it with a null anyway.</p> <p><strong>EDIT:</strong><br> According to the answers I think my question requires clarification.</p> <p>I have bean I need to instantiate and pass to 3rd party I use. This bean has many properties (12 currently) of various types (<code>int</code>, <code>boolean</code>, <code>String</code>, etc.)<br> Each property has a default value - I don't know what it is and would prefer not needing to know unless it becomes an issue. What I'm looking for is a generic solution that comes from Spring's abilities - currently the only solution I have is a reflection based.</p> <p><strong>Configuration</strong></p> <pre><code>&lt;bean id="myBean" class="some.TypeWrapper"&gt; &lt;property name="properties"&gt; &lt;map&gt; &lt;entry key="abc" value="${some.value}"/&gt; &lt;entry key="xyz" value="${some.other.value}"/&gt; ... &lt;/map&gt; &lt;/property&gt; &lt;/bean&gt; </code></pre> <p><strong>Code</strong></p> <pre><code>public class TypeWrapper { private Type innerBean; public TypeWrapper() { this.innerBean = new Type(); } public void setProperties(Map&lt;String,String&gt; properties) { if (properties != null) { for (Entry&lt;String, Object&gt; entry : properties.entrySet()) { String propertyName = entry.getKey(); Object propertyValue = entry.getValue(); setValue(propertyName, propertyValue); } } } private void setValue(String propertyName, Object propertyValue) { if (propertyValue != null) { Method method = getSetter(propertyName); Object value = convertToValue(propertyValue, method.getParameterTypes()[0]); method.invoke(innerBean, value); } } private Method getSetter(String propertyName) { // Assume a valid bean, add a "set" at the beginning and toUpper the 1st character. // Scan the list of methods for a method with the same name, assume it is a "valid setter" (i.e. single argument) ... } private Object convertToValue(String valueAsString, Class type) { // Check the type for all supported types and convert accordingly if (type.equals(Integer.TYPE)) { ... } else if (type.equals(Integer.TYPE)) { ... } ... } } </code></pre> <p>The real "difficulty" is in implementing <code>convertToValue</code> for all possible value types. I have done this more than once in my life - so it is not a major issue to implement it for all possible types that I need (mostly primitives and a few enums) - but I hoped a more intelligent solution existed.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload