---
title: "应用Apache BeanUtils  set  多层次的 property value"
author: "bruce008"
date: "2011-09-26T17:39:29Z"
tags: []
categories: ["企业架构"]
slug: "apache-beanutils-set-property-value"
draft: false
lastmod: "2011-09-26T17:39:29Z"
id: 1180844
url: "https://bruce008.iteye.com/blog/1180844"
---

我们知道  如果想获取一个JavaBean  的某一个 propety 时 最方便的方法是 调用 在 Apache BeanUtil  包中 的BeanUtils ， 或者 PropertyUtilsBean  类实例的  getProperty 方法。 其中property name 可以是 多层嵌套的。  比如一个 类 Component  里面有一个 DataRange  的属性 dataRange。 这个DataRange 的类型里面又有 EffectiveDate 的 beginDate 属性。   我们可以通过    

 

     BeanUtils.geProperty(bean,   "dateRange.beginDate")   来获取。  

 

但是如果我们需要设置 一个多层的属性值 dateRange.beginDate， 并且这个dateRange 属性空的话，  就会产生如下异常：

 

  java.lang.IllegalArgumentException: No bean specified

at org.apache.commons.beanutils.PropertyUtilsBean.getPropertyDescriptor(PropertyUtilsBean.java:883)

at org.apache.commons.beanutils.BeanUtilsBean.setProperty(BeanUtilsBean.java:935)

at org.apache.commons.beanutils.BeanUtils.setProperty(BeanUtils.java:456)

 

 

 

 OK， 现在的办法就是按照层次从上往下 先判断 property 是否为空， 如果为空 调用 默认建构函数实例化一个空的property value。 针对最后一层属性，  采用参数为String 的建构函数实例化该属性。代码如下：

 

```
	public void setNestedPropertyValue(Object bean, String propertyName, String value) {
		PropertyUtilsBean beanUtil = new PropertyUtilsBean();
		String[] propertyLevels = propertyName.split("\\.");
		String propertyNameWithParent = "";
		for (int i = 0; i < propertyLevels.length; i++) {
			String p = propertyLevels[i];
			propertyNameWithParent = (propertyNameWithParent.equals("") ? p
					: propertyNameWithParent + "." + p);

			try {
				if (i < (propertyLevels.length - 1) && 
						beanUtil.getProperty(bean, propertyNameWithParent) != null) {
					continue;
				}
				Class pType = beanUtil.getPropertyType(bean,
						propertyNameWithParent);
				if (i < (propertyLevels.length - 1)) {
					BeanUtils.setProperty(bean, propertyNameWithParent, pType
							.getConstructor().newInstance());
				} else {
					Constructor<String> constructor = pType
							.getConstructor(String.class);
					BeanUtils.setProperty(bean, propertyNameWithParent,
							constructor.newInstance(value));
				}
			} catch (Exception e) {
				throw new RuntimeException("error to set property value ", e);
			}
		}
	}
```

 

现在这个方法只是一个最粗的实现，  进一步的想法是参照 Spring MVC  中的  binding 。 Spring MVC binding 所做的事情就是将   request 中 String 类型的 参数 convert 成  各种 java 类型的对象或 对象的属性。