반응형

BeanUtils
map에 있는 값을 bean에 넣거나  , bean에 있는 값을 map에 넣거나   , request에 있는 값을 bean에 넣거나
동적으로 변하는 bean을 분석해야 할때 정말 편하고 쉽게 쓸 수 있는 모듈.
객제의 정보, 속성 값 읽기, 속성 값 쓰기 등의 기능을 제공.

eclipse에서 테스트 할 땐 commons-beanutils.jar, commons-beanutils-bean-collections.jar,  commons-beanutils-core.jar, commons-logging-1.1.jar, junit-3.8.1.jar 등을 빌드패스에 넣어야 함.





// bean 복제
public static Object cloneBean(Object bean)

// orig에서 dest로 복제, 동일 속성명이 존재해야합니다.
public static void copyProperties(Object dest, Object orig)

// orig의 property를 dest로 복제
public static void copyProperty(Object bean, String name, Object value)

// Return the value of the specified array property of the specified bean, as a String array.
public static String[] getArrayProperty(Object bean, String name)

// 배열값 가져오기, property name을 'name[0]' 이런 식으로 주어야 한다. 규칙임
public static String getIndexedProperty(Object bean, String name)

// 배열값 가져오기 index는 몇번째
public static String getIndexedProperty(Object bean, String name, int index)

// mapped property 가져오기, property name을 'name(0)' 이런 식으로 주어야 한다.
public static String getMappedProperty(Object bean, String name)
// mapped property 가져오기
public static String getMappedProperty(Object bean, String name, String key)

public static String getNestedProperty(Object bean, String name)

// bean에서 값 가져오기
public static String getProperty(Object bean, String name)

public static String getSimpleProperty(Object bean, String name)

// bean에서 해당 name의 property에 value를 convert해서 넣는다.
public static void setProperty(Object bean, String name, Object value)

/*
 * bean 있는 값을 key, value로 map에 넣어 줍니다. 가장 많이 쓰이는 메소드 중 하나
 */
public static Map describe(Object bean)

/*
 * map에 있는 값을 bean에 넣어 줍니다. 가장 많이 쓰이는 메소드 중 하나
 */
public static void populate(Object bean, Map properties)



describe메소드를 이용해서 객체의 property와 value, type등을 쉽게 알아낼 수 있음.

Map map = BeanUtils.describe(bean);
Set set = map.keySet();
Iterator it = set.iterator();
while (it.hasNext()) {
    String key = (String)it.next();

    if ("class".equals(key)) {
        continue;
    }
    Object value = map.get(key);
    properties.put(key, value);
}

Posted by 1010