当前位置: 首页>>代码示例>>Java>>正文


Java BeanUtils.setProperty方法代码示例

本文整理汇总了Java中org.apache.commons.beanutils.BeanUtils.setProperty方法的典型用法代码示例。如果您正苦于以下问题:Java BeanUtils.setProperty方法的具体用法?Java BeanUtils.setProperty怎么用?Java BeanUtils.setProperty使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在org.apache.commons.beanutils.BeanUtils的用法示例。


在下文中一共展示了BeanUtils.setProperty方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: transformMapListToBeanList

import org.apache.commons.beanutils.BeanUtils; //导入方法依赖的package包/类
/**
 * 将所有记录包装成T类对象,并将所有T类对象放入一个数组中并返回,传入的T类的定义必须符合JavaBean规范,
 * 因为本函数是调用T类对象的setter器来给对象赋值的
 *
 * @param clazz          T类的类对象
 * @param listProperties 所有记录
 * @return 返回所有被实例化的对象的数组,若没有对象,返回一个空数组
 * @throws InstantiationException
 * @throws IllegalAccessException
 * @throws InvocationTargetException
 */
private static <T> List<T> transformMapListToBeanList(Class<T> clazz, List<Map<String, Object>> listProperties)
        throws InstantiationException, IllegalAccessException, InvocationTargetException {
    // 存放被实例化的T类对象
    T bean = null;
    // 存放所有被实例化的对象
    List<T> list = new ArrayList<T>();
    // 将实例化的对象放入List数组中
    if (listProperties.size() != 0) {

        Iterator<Map<String, Object>> iter = listProperties.iterator();
        while (iter.hasNext()) {
            // 实例化T类对象
            bean = clazz.newInstance();
            // 遍历每条记录的字段,并给T类实例化对象属性赋值
            for (Map.Entry<String, Object> entry : iter.next().entrySet()) {
                BeanUtils.setProperty(bean, entry.getKey(), entry.getValue());
            }
            // 将赋过值的T类对象放入数组中
            list.add(bean);
        }
    }
    return list;
}
 
开发者ID:FlyingHe,项目名称:UtilsMaven,代码行数:35,代码来源:DBUtils.java

示例2: setProperty

import org.apache.commons.beanutils.BeanUtils; //导入方法依赖的package包/类
/**
 *
 * @param retval
 * @param key
 * @param value
 * @throws IllegalAccessException
 * @throws InvocationTargetException
 */
private void setProperty(T retval, String key, Object value) throws IllegalAccessException, InvocationTargetException {
    String property = key2property(key);

    for (final Field f : this.type.getDeclaredFields()) {
        final SerializedName annotation = f.getAnnotation(SerializedName.class);

        if (annotation != null && property.equalsIgnoreCase(annotation.value())) {
            property = f.getName();
            break;
        }
    }

    if (propertyExists(retval, property)) {
        BeanUtils.setProperty(retval, property, value);
    } else {
        LOG.warn(retval.getClass() + " does not support public setter for existing property [" + property + "]");
    }
}
 
开发者ID:Sybit-Education,项目名称:airtable.java,代码行数:27,代码来源:Table.java

示例3: setProperties

import org.apache.commons.beanutils.BeanUtils; //导入方法依赖的package包/类
public static void setProperties(Object obj, Map map){
	Field[] fields = obj.getClass().getDeclaredFields();
	for(Field field : fields) {
		Column column = field.getAnnotation(Column.class);
		String dbColumnname = null;
		if ("".equals(column.name()))
			dbColumnname = field.getName();
		else dbColumnname = column.name();

		try {
			Object value = map.get(dbColumnname);
			if (value != null) {
				BeanUtils.setProperty(obj, field.getName(), value);
			}
		} catch (Exception e) {
			log.catching(e);
		}
	}
}
 
开发者ID:jambo-framework,项目名称:jambo2,代码行数:20,代码来源:BaseVO.java

示例4: convertWithMethod

import org.apache.commons.beanutils.BeanUtils; //导入方法依赖的package包/类
private static <T> T convertWithMethod(Row row, Class<T> clazz) throws IllegalAccessException, InstantiationException, ParseException, InvocationTargetException {
    Field[] fields = new Field[]{};
    Class temp = clazz;
    while(temp != null){
        fields = ArrayUtils.addAll(temp.getDeclaredFields(),fields);
        temp = temp.getSuperclass();
    }
    Object object = clazz.newInstance();
    for (Field field : fields) {
        ExcelCellField filedExcelAnnotation = getAnnotationCellFiled(field.getAnnotations());
        if (filedExcelAnnotation == null) {
            continue;
        }
        Class<?> fieldType = field.getType();
        Integer index = filedExcelAnnotation.index();
        String propertyName = field.getName();
        Object value = getValue(fieldType, row.getCell(index), filedExcelAnnotation);
        if (value != null) {
            BeanUtils.setProperty(object, propertyName, value);
        }
    }
    return (T) object;
}
 
开发者ID:dengxiangjun,项目名称:OfficeAutomation,代码行数:24,代码来源:RowConvert.java

示例5: injectValue

import org.apache.commons.beanutils.BeanUtils; //导入方法依赖的package包/类
protected void injectValue(Object bean, PropertyDescriptor pd) {
    Method writeMethod = pd.getWriteMethod();
    Value valueAnnotate = writeMethod.getAnnotation(Value.class);
    if (valueAnnotate == null) {
        return;
    }
    String value = valueAnnotate.value();
    if (value.startsWith("${") && value.endsWith("}")) {
        value = value.substring(2, value.length() - 1);
        value = getProperty(value);
    }
    try {
        BeanUtils.setProperty(bean, pd.getName(), value);
    } catch (Exception e) {
        throw new InjectBeanException("Could not inject value: " + writeMethod + "; nested exception is " + e, e);
    }
}
 
开发者ID:devefx,项目名称:validator-web,代码行数:18,代码来源:DefaultContainer.java

示例6: createBeanFromRequest

import org.apache.commons.beanutils.BeanUtils; //导入方法依赖的package包/类
public static <T> T createBeanFromRequest(NaviHttpRequest request, Class<? extends T> clazz) throws NaviSystemException {
    try {
        T bean = clazz.newInstance();
        Field[] fields = bean.getClass().getDeclaredFields();

        for (Field field : fields) {
            if (request.getParameter(field.getName()) != null) {
                String val = request.getParameter(field.getName());
                if (val == null) {
                    continue;
                }

                Object obj = val;
                if (field.getType().equals(JSONArray.class) || field.getType().equals(JSONObject.class)) {
                    obj = JSON.parse(val);
                }

                BeanUtils.setProperty(bean, field.getName(), obj);
            }
        }

        return bean;
    } catch (Exception e) {
        throw new NaviSystemException("system, error, " + e.getMessage(), NaviError.SYSERROR);
    }
}
 
开发者ID:sunguangran,项目名称:navi,代码行数:27,代码来源:HttpRequestUtil.java

示例7: convert

import org.apache.commons.beanutils.BeanUtils; //导入方法依赖的package包/类
public static BatteryInfoReq convert(String str) {
    if (StringUtils.isBlank(str)) {
        return null;
    }
    BatteryInfoReq req = new BatteryInfoReq();
    String[] arr = StringUtils.split(str, "&");
    for (int i = 0; i < arr.length; i++) {
        String[] pair = StringUtils.split(arr[i], "=");
        try {
            BeanUtils.setProperty(req, pair[0], pair[1]);
        } catch (Exception e) {
            logger.error(ExceptionUtils.getStackTrace(e));
        }
    }

    return req;

}
 
开发者ID:booleguo,项目名称:sam-elle,代码行数:19,代码来源:BtyDataConverter.java

示例8: toBean

import org.apache.commons.beanutils.BeanUtils; //导入方法依赖的package包/类
/***
 * 将JSON文本反序列化为主从关系的实体
 * @param 泛型T 代表主实体类型
 * @param 泛型D 代表从实体类型
 * @param jsonString JSON文本
 * @param mainClass 主实体类型
 * @param detailName 从实体类在主实体类中的属性名称
 * @param detailClass 从实体类型
 * @return
 */
public static <T, D> T toBean(String jsonString, Class<T> mainClass,
        String detailName, Class<D> detailClass)
{
    JSONObject jsonObject = JSONObject.fromObject(jsonString);
    JSONArray jsonArray = (JSONArray) jsonObject.get(detailName);

    T mainEntity = JSONUtils.toBean(jsonObject, mainClass);
    List<D> detailList = JSONUtils.toList(jsonArray, detailClass);

    try
    {
        BeanUtils.setProperty(mainEntity, detailName, detailList);
    }
    catch (Exception ex)
    {
        throw new RuntimeException("主从关系JSON反序列化实体失败!");
    }

    return mainEntity;
}
 
开发者ID:wanliyang10010,项目名称:Shop,代码行数:31,代码来源:JSONUtils.java

示例9: delete

import org.apache.commons.beanutils.BeanUtils; //导入方法依赖的package包/类
@Override
@Transactional
public void delete(T entity)  {
		try {
			BeanUtils.setProperty(entity, "deleted", true);
			BeanUtils.setProperty(entity, "dateUpdated", LocalDateTime.now());
			Account account = SecurityContextUtil.getUserAccount();
			if(account!=null)
					BeanUtils.setProperty(entity, "userId", account.getAccountId());

			entityManager.merge(entity);
		} catch (Exception e) {
			entityManager.detach(entity);
			entityManager.flush();
		} 
}
 
开发者ID:hserv,项目名称:coordinated-entry,代码行数:17,代码来源:BaseRepositoryImpl.java

示例10: setField

import org.apache.commons.beanutils.BeanUtils; //导入方法依赖的package包/类
public void setField(NamedNodeMap namedNodeMap, String fieldName) {
	Attr attr = (Attr) namedNodeMap.getNamedItem(fieldName);
	if (attr != null) {
		boolean isDefaultValue = !attr.getSpecified();
		boolean success = setDefaultFlag(fieldName, isDefaultValue);
		if (success) {

			// Json.print(baseInfo, "baseInfo");
			Object value = this.toBaseInfoValue(fieldName, attr.getNodeValue());
			try {
				BeanUtils.setProperty(baseInfo, fieldName, value);
			}
			catch (Exception e) {
				throw new RuntimeException(e.getMessage(), e);
			}
		}
	}
}
 
开发者ID:tanhaichao,项目名称:leopard,代码行数:19,代码来源:BaseInfoBuilder.java

示例11: toBean

import org.apache.commons.beanutils.BeanUtils; //导入方法依赖的package包/类
/***
 * 将JSON文本反序列化为主从关系的实体
 * 
 * @param <T>泛型T 代表主实体类型
 * @param <D1>泛型D1 代表从实体类型
 * @param <D2>泛型D2 代表从实体类型
 * @param jsonString
 *            JSON文本
 * @param mainClass
 *            主实体类型
 * @param detailName1
 *            从实体类在主实体类中的属性
 * @param detailClass1
 *            从实体类型
 * @param detailName2
 *            从实体类在主实体类中的属性
 * @param detailClass2
 *            从实体类型
 * @return
 */
public static <T, D1, D2> T toBean(String jsonString, Class<T> mainClass,
		String detailName1, Class<D1> detailClass1, String detailName2,
		Class<D2> detailClass2) {
	JSONObject jsonObject = JSONObject.fromObject(jsonString);
	JSONArray jsonArray1 = (JSONArray) jsonObject.get(detailName1);
	JSONArray jsonArray2 = (JSONArray) jsonObject.get(detailName2);

	T mainEntity = JSONHelper.toBean(jsonObject, mainClass);
	List<D1> detailList1 = JSONHelper.toList(jsonArray1, detailClass1);
	List<D2> detailList2 = JSONHelper.toList(jsonArray2, detailClass2);

	try {
		BeanUtils.setProperty(mainEntity, detailName1, detailList1);
		BeanUtils.setProperty(mainEntity, detailName2, detailList2);
	} catch (Exception ex) {
		throw new RuntimeException("主从关系JSON反序列化实体失败!");
	}

	return mainEntity;
}
 
开发者ID:haizicq,项目名称:osframe,代码行数:41,代码来源:JSONHelper.java

示例12: HeaderToSimpleBean

import org.apache.commons.beanutils.BeanUtils; //导入方法依赖的package包/类
/**
 * 转换请求中的header中的参数为简单对象(只有对象中的简单类型会被赋值),参数值为数组的只取第一个
 *
 * @param request 请求对象
 * @param claz    目标对象类型类
 * @param <T>     目标对象类型
 * @return 目标对象
 * @throws IllegalAccessException
 * @throws InstantiationException
 */
public static <T> T HeaderToSimpleBean(HttpServletRequest request, Class<T> claz) throws IllegalAccessException, InstantiationException {
    Enumeration it = request.getHeaderNames();
    T object = claz.newInstance();
    while (it.hasMoreElements()) {
        String hKey = (String) it.nextElement();
        String hValue = request.getHeader(hKey);
        if (StringUtils.isNotBlank(hKey) && StringUtils.isNotBlank(hValue)) {
            try {
                BeanUtils.setProperty(object, hKey, hValue);
            } catch (Exception e) {
            }
        }
    }
    return object;
}
 
开发者ID:yangshuai0711,项目名称:dingding-app-server,代码行数:26,代码来源:WebUtil.java

示例13: setProperty

import org.apache.commons.beanutils.BeanUtils; //导入方法依赖的package包/类
private void setProperty(Object object, String propertyPath, Object value) {
    try {
        if (log.isTraceEnabled()) {
            log.trace("Applying property [{}] value [{}] on object of type [{}]",
                    new Object[]{propertyPath, value, object.getClass().getName()});
        }
        BeanUtils.setProperty(object, propertyPath, value);
    } catch (Exception e) {
        String msg = "Unable to set property '" + propertyPath + "' with value [" + value + "] on object " +
                "of type " + (object != null ? object.getClass().getName() : null) + ".  If " +
                "'" + value + "' is a reference to another (previously defined) object, prefix it with " +
                "'" + OBJECT_REFERENCE_BEGIN_TOKEN + "' to indicate that the referenced " +
                "object should be used as the actual value.  " +
                "For example, " + OBJECT_REFERENCE_BEGIN_TOKEN + value;
        throw new ConfigurationException(msg, e);
    }
}
 
开发者ID:xuegongzi,项目名称:rabbitframework,代码行数:18,代码来源:ReflectionBuilder.java

示例14: setFieldValue

import org.apache.commons.beanutils.BeanUtils; //导入方法依赖的package包/类
public static boolean setFieldValue(Object obj, Field field, Object value){
	if(null == obj || null == field){
		return false;
	}
	try{
		if(field.isAccessible()){
			//可访问属性
			field.set(obj, value);
			BeanUtils.setProperty(obj, field.getName(), value);
		}else{
			//不可访问属性
			field.setAccessible(true);
			field.set(obj, value);
			BeanUtils.setProperty(obj, field.getName(), value);
			field.setAccessible(false);
		}
	}catch(Exception e){
		e.printStackTrace();
		return false;
	}
	return true;
}
 
开发者ID:anylineorg,项目名称:anyline,代码行数:23,代码来源:BeanUtil.java

示例15: convertToType

import org.apache.commons.beanutils.BeanUtils; //导入方法依赖的package包/类
/**
 * Converts the input object into a <code>Map</code> object.
 *  
 * @param <T> Target type of the conversion.
 * @param type Data type to which this value should be converted.
 * @param value TThe input value to be converted.
 * @return The converted <code>Map</code>
 * @throws Throwable if an error occurs converting to the specified type 
 */
@Override
protected <T> T convertToType(Class<T> type, Object value) throws Throwable {

    if(mapClass == null) {
        throw new IllegalAccessException("mapClass is not initialized by setListClass().");
    }
    
    final Class<T> sourceType = (Class<T>) value.getClass();
    Map<String, Object> returnMap = new HashMap<>();
        
    if(value instanceof LinkedTreeMap){
        for (String key : ((LinkedTreeMap<String, Object>) value).keySet()) {
            Object instanz = this.mapClass.newInstance();
            Object val = ((LinkedTreeMap) value).get(key);   
            BeanUtils.setProperty(instanz,"name",key); 
            for (String key2 : ((LinkedTreeMap<String, Object>) val).keySet()) {
                Object val2 = ((LinkedTreeMap) val).get(key2);            
                BeanUtils.setProperty(instanz,key2,val2);                                
            }           
            returnMap = toClassMap(sourceType,instanz,returnMap);
        }         
        return (T) returnMap;
    }
    
    if(value instanceof String){
        return (T) toStringMap(sourceType,value.toString(),returnMap);
    }
    
    final String stringValue = value.toString().trim();
    if (stringValue.length() == 0) {
        return handleMissing(type);
    }
        
    return (T) toStringMap(sourceType,stringValue,returnMap);
}
 
开发者ID:Sybit-Education,项目名称:airtable.java,代码行数:45,代码来源:MapConverter.java


注:本文中的org.apache.commons.beanutils.BeanUtils.setProperty方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。