日期:2014-05-16  浏览次数:20869 次

apache-beanutils 记录一个很诡异的问题
public class MyBeanUtils
    extends org.apache.commons.beanutils.BeanUtils {

public static void copyBeanNotNull2Bean(Object databean,Object tobean)throws Exception
  {
	  PropertyDescriptor origDescriptors[] =
          PropertyUtils.getPropertyDescriptors(databean);
      for (int i = 0; i < origDescriptors.length; i++) {
          String name = origDescriptors[i].getName();
//          String type = origDescriptors[i].getPropertyType().toString();
          if ("class".equals(name)) {
              continue; // No point in trying to set an object's class
          }
          if (PropertyUtils.isReadable(databean, name) &&
              PropertyUtils.isWriteable(tobean, name)) {
              try {
                  Object value = PropertyUtils.getSimpleProperty(databean, name);
                  if(value!=null){
                	    copyProperty(tobean, name, value);
                  }
              }
              catch (java.lang.IllegalArgumentException ie) {
                  logger.error(ie,ie); // Should not happen
              }
              catch (Exception e) {
                  logger.error(e,e); // Should not happen
              }

          }
      }
  }
}

?

使用这段代码进行javabean的复制,并且srcBean为null的字段,不进行复制。

?

这段代码测试环境是正常的,在生产环境下,运行一段时间后(大概有1个月的样子),就会出现很诡异的问题,javaBean里面Integer,Long等类型的 复制后字段变成null了(源javaBean和目标javaBean该字段都不不为空的情况下)。

?

正在持续监测中...

?

注:copyProperty 方法是继承于org.apache.commons.beanutils.BeanUtils