cglib中的两个工具类BeanMap,BeanCopier

         这两天看了下cglib的一些源码,发现BeanMap和BeanCopier在工作中应该可以用到,虽然已经apache和spring的相关工具类可以用了,但是多一个工具类也不嫌多嘛,况且BeanCopier的速度会更快一些。

          1.BeanMap本身是实现了Map的接口,将对象转成Map很方便使用

          2.不同对象的属性的COPY

          

import net.sf.cglib.beans.BeanCopier;
import net.sf.cglib.beans.BeanMap;


public class Test {

	public static void main(String[] args) {
		Test t = new Test();
		User u = t.new User();
		u.setAge(25);
		u.setName("mike");
          BeanMap  m = BeanMap.create(u);
          System.out.println(m.get("name")+","+m.get("age"));
        
		Person p = t.new Person();
                BeanCopier b = BeanCopier.create(User.class, Person.class, false);
         b.copy(u, p, null);
         System.out.println(p.toString());
	}
	public class Person{
		private String name;
		@Override
		public String toString() {
			return "Person [name=" + name + ", age=" + age + "]";
		}
		private int age;
		
		public String getName() {
			return name;
		}
		public void setName(String name) {
			this.name = name;
		}
		public int getAge() {
			return age;
		}
		public void setAge(int age) {
			this.age = age;
		}
	}
	public class User{
		private String name;
		private int age;
		
		public User(){
			
		}
		public String getName() {
			return name;
		}
		public void setName(String name) {
			this.name = name;
		}
		public int getAge() {
			return age;
		}
		public void setAge(int age) {
			this.age = age;
		}
	}

}

 

    结果:

    mike,25

    Person [name=mike, age=25]

  

   更多参考:

  http://czj4451.iteye.com/blog/1998750

  http://www.iteye.com/topic/799827

你可能感兴趣的:(BeanCopier)