java - Spring - 仅当值不为空时才设置属性

标签 java spring javabeans

在使用Spring时,是否可以只在传递的值不为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。

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

我有 bean 我需要实例化并传递给我使用的第 3 方。这个 bean 有许多不同类型的属性(目前有 12 个)(intbooleanString 等)
每个属性都有一个默认值——我不知道它是什么,除非它成为问题,否则我宁愿不需要知道。 我正在寻找的是来自 Spring 能力的通用解决方案 - 目前我唯一的解决方案是基于反射的。

配置

<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。 我一生中已经不止一次这样做了——所以为我需要的所有可能的类型(主要是原语和一些枚举)实现它并不是一个主要问题——但我希望存在一个更智能的解决方案。

最佳答案

您可以将 SpEL 与占位符和占位符机制的默认值一起使用,如下所示:

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

关于java - Spring - 仅当值不为空时才设置属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9347929/

相关文章:

java - GSON - 1 个 parent ,多个 child

spring - 如何让 Gradle Spring 依赖插件工作?

Java Bean 数组列表

java - Bean 被初始化但注入(inject)抛出 npe

Java Bean 不被识别

java - 这个链表反向实现是如何工作的呢?

java - android:按钮onClick(),无法判断是否发生了任何事情

java - 如何在两个数据集选项(如 DB)之间使用类似的减法来设计 Redis 搜索/扫描算法

java - 如何在 Spring 中将 xml bean 配置文件导入到 @configuration 类?

java - 如何记录Springframework中方法所花费的时间?