Spring - 仅当值不为null时才设置属性 [英] Spring - set a property only if the value is not null

查看:163
本文介绍了Spring - 仅当值不为null时才设置属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Spring时,是否可以仅在传递的值不为null时设置属性?

When using Spring, is it possible to set a property only if the value passed is not null?

示例:

<bean name="myBean" class="some.Type">
   <property name="abc" value="${some.param}"/>
</bean>

我正在寻找的行为是:

some.Type myBean = new some.Type();
if (${some.param} != null) myBean.setAbc(${some.param});

我需要这个的原因是因为 abc 有一个默认值,我不想用 null 覆盖。
我正在创建的Bean不在我的源代码控制之下 - 所以我无法改变它的行为。 (另外, abc 为此目的可能是原始的,所以无论如何我都不能用null设置它。

The reason I need this is since abc has a default value which I don't want to override with a null. And the Bean I am creating is not under my source control - so I cannot change its behavior. (Also, abc for this purpose might be a primitive, so I can't set it with a null anyway.

编辑:

根据答案,我认为我的问题需要澄清。


According to the answers I think my question requires clarification.

我有需要的bean实例化并传递给我使用的第三方。这个bean有许多不同类型的属性(当前12个)( int boolean 字符串等)

每个属性都有一个默认值 - 我不知道它是什么,除非需要知道,否则这是一个问题。
我正在寻找的是一个来自Spring的能力的通用解决方案 - 目前我唯一的解决方案是基于反射。

I have bean I need to instantiate and pass to 3rd party I use. This bean has many properties (12 currently) of various types (int, boolean, String, etc.)
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.

配置

<bean id="myBean" class="some.TypeWrapper">
   <property name="properties">
     <map>
         <entry key="abc" value="${some.value}"/>
         <entry key="xyz" value="${some.other.value}"/>
         ...
      </map>
   </property>
</bean>

代码

public class TypeWrapper
{
    private Type innerBean;

    public TypeWrapper()
    {
        this.innerBean = new Type();
    }

    public void setProperties(Map<String,String> properties)
    {
        if (properties != null)
        {
            for (Entry<String, Object> 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))
        {
          ...
        }
        ...
    }
}

真正的困难是为所有可能的值类型实现 convertToValue
我一生中不止一次这样做 - 所以对于我需要的所有可能的类型(主要是原语和一些枚举)实现它并不是一个主要问题 - 但我希望存在更智能的解决方案。

The real "difficulty" is in implementing convertToValue 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.

推荐答案

您可以使用 SpEL 以及占位符机制的占位符和默认值如下所示:

You can use SpEL and placeholder and default value for placeholder mechanisms together as following:

<bean name="myBean" class="some.Type">
    <property name="abc" value="${some.param:#{null}}"/>
</bean>

这篇关于Spring - 仅当值不为null时才设置属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆