Spring - 仅当值不为空时才设置属性
使用Spring时,是否可以仅当传递的值不为空时才设置属性?
示例:
<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 个)(int
、boolean
、String
等)
每个属性都有一个默认值 - 我不知道它是什么,并且宁愿不需要知道,除非它成为一个问题。 我正在寻找的是一个来自 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
。 我一生中不止一次这样做过 - 因此,为我需要的所有可能类型(主要是原语和一些枚举)实现它并不是一个主要问题 - 但我希望存在更智能的解决方案。
When using Spring, is it possible to set a property only if the value passed is not null?
Example:
<bean name="myBean" class="some.Type">
<property name="abc" value="${some.param}"/>
</bean>
The behavior I'm looking for is:
some.Type myBean = new some.Type();
if (${some.param} != null) myBean.setAbc(${some.param});
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.
EDIT:
According to the answers I think my question requires clarification.
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.
Configuration
<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>
Code
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))
{
...
}
...
}
}
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您可以将
SpEL
与占位符和占位符机制的默认值一起使用,如下所示:You can use
SpEL
and placeholder and default value for placeholder mechanisms together as following:为了解决你的问题,你必须使用SEL(Spring表达式语言)。
通过这个功能(在SPring 3.0中添加)你可以像其他动态语言一样编写你的条件。对于您的上下文,答案是:
有关详细信息,请参阅(本教程介绍了在上下文文件中使用 SEL 的内容):
http://static.springsource.org/spring/docs/3.0.5。 RELEASE/reference/expressions.html
For solve your problem, you have to use SEL(Spring Expression Language).
By this feature (added in SPring 3.0) you can such as other dynamic language writing your condition. For your context, answer is:
for more information see(this tutorial says what use SEL in context file):
http://static.springsource.org/spring/docs/3.0.5.RELEASE/reference/expressions.html
您可以在 Spring 框架的属性配置器中使用默认值概念,如下所示:
您可以通过这种方法设置默认值。通过此上下文配置,如果
some.param
键存在,则其值在abc
属性中设置,如果不存在,则设置your-default-value
在abc
属性中。注意:这种方法的另一个好处是:“在 POJO 编程模型中,更好的方法是类的成员没有任何默认值,并且默认值从类外注入。”
You can use default value concept in property configurer in Spring framework as following:
you can set default value by this approach. By this context config if
some.param
key exist so its value set inabc
property and if don't existyour-default-value
set inabc
property.Note: Another benefit of this approah is:"In POJO programming model better approzh is member of class don't have any default value,and default value injected from out of class."
您可以创建一个实用程序类,它将充当
some.Type
的 Factory 类,并将逻辑包装在那里例如:
并在 XML 上使用此 Factory 方法配置 bean 创建
You can create a Utility class that will act as a Factory class for
some.Type
, and wrap the logic thereFor Example :
and on XML configure the bean creation using this Factory method
这看起来像是 基于Java的容器配置。您将能够执行在 XML 配置中所做的操作,但可以使用 Java 的所有功能。
This looks like a job for Java-based container configuration. You'll be able to do what you do in your XML config, but with all the power of Java.
我已经让它与以下代码片段一起工作:
I've got it working with following snippet: