- 浏览: 832882 次
- 性别:
- 来自: 厦门
文章分类
- 全部博客 (363)
- 2010年3月 (3)
- 2010年4月 (3)
- Java (116)
- ExtJs (22)
- EJB3.0 (11)
- JQuery (28)
- SqlServer (5)
- Oracle (17)
- hibernate (21)
- struts2 (14)
- php (10)
- JavaScript (11)
- jbpm (6)
- spring (24)
- lucene (2)
- ibatis (7)
- C# (8)
- mysql (11)
- json (3)
- webservice (3)
- 设计模式 (1)
- jdbc (1)
- servlet (2)
- ice (6)
- 日常软件问题 (2)
- 生活 (2)
- iphone (1)
- rest (3)
- ruby (2)
- linux (13)
- quartz (1)
- poi (1)
- redis (13)
- memcached (4)
- nosql (2)
- tomcat调优 (1)
- 项目管理 (0)
最新评论
-
天使建站:
jquery里和数组相关的操作 附带具体的实例 ...
jquery对象数组 -
Cy0941:
$('#formId').form('submit',...) ...
easyui的form表单提交处理 -
shmily2038:
swifth 写道楼主,,你的命令写错啦,,[root@ser ...
centos直接yum安装nginx -
swifth:
楼主,,你的命令写错啦,,[root@server ~]# y ...
centos直接yum安装nginx -
随遇而安DXX:
...
REST
这是两个javabean对象
package com.beanutils;
import java.util.*;
public class Person ...{
private String name;
private String sex;
private int age;
private Address address;
private List goods;
private Map contact;
public Person() ...{
}
public Person(String name) ...{
this.name = name;
}
public Person(String name,String sex) ...{
this.name = name;
this.sex = sex;
}
public String getName() ...{
return this.name;
}
public void setName(String name) ...{
this.name = name;
}
public String getSex() ...{
return this.sex;
}
public void setSex(String sex) ...{
this.sex = sex;
}
public int getAge() ...{
return this.age;
}
public void setAge(int age) ...{
this.age = age;
}
public Address getAddress() ...{
return this.address;
}
public void setAddress(Address address) ...{
this.address = address;
}
public List getGoods() ...{
return this.goods;
}
public void setGoods(List goods) ...{
this.goods = goods;
}
public Map getContact() ...{
return this.contact;
}
public void setContact(Map contact) ...{
this.contact = contact;
}
public void sayHello() ...{
System.out.println("Hello World!");
}
public void f(String str) ...{
System.out.println("Person.f()..." + str);
}
public String toString() ...{
return "Person.toString()...";
}
}=========================================================================================
package com.beanutils;
public class Address ...{
private String province;
private String city;
private String street;
private String zipcode;
public String getProvince() ...{
return this.province;
}
public void setProvince(String province) ...{
this.province = province;
}
public String getCity() ...{
return this.city;
}
public void setCity(String city) ...{
this.city = city;
}
public String getStreet() ...{
return this.street;
}
public void setStreet(String street) ...{
this.street = street;
}
public String getZipcode() ...{
return this.zipcode;
}
public void setZipcode(String zipcode) ...{
this.zipcode = zipcode;
}
}============================================================================
一个测试类
package com.beanutils;
import java.util.*;
import org.apache.commons.beanutils.*;
public class BeanUtilsTest ...{
public static void main(String[] args) throws Exception ...{
Address address = new Address();
address.setProvince("hunan");
address.setCity("changsha");
address.setStreet("wuyilu");
address.setZipcode("410000");
List goods = new ArrayList();
goods.add("sporting");
goods.add("singing");
goods.add("dancing");
Map contact = new HashMap();
contact.put("student","Tom");
contact.put("teacher","Lucy");
Person person = new Person();
person.setName("Mike");
person.setSex("male");
person.setAge(25);
person.setAddress(address);
person.setGoods(goods);
person.setContact(contact);
//BeanUtilsTest.cloneBean(person);
//BeanUtilsTest.copyProperties(person);
//BeanUtilsTest.propertyUtilsCopyProperties(person);
//BeanUtilsTest.copyProperty(person);
//BeanUtilsTest.describe(person);
//BeanUtilsTest.propertyUtilsDescribe(person);
//BeanUtilsTest.getArrayProperty(person);
//BeanUtilsTest.getIndexedProperty(person);
//BeanUtilsTest.getMappedProperty(person);
//BeanUtilsTest.getNestedProperty(person);
//BeanUtilsTest.populate(person);
//得到一个bean中指定属性的值,相当于getter
// System.out.println(BeanUtils.getProperty(person,"name"));
// //修改一个bean中指定属性的值,相当于setter
// BeanUtils.setProperty(person,"name","Tom");
// System.out.println(BeanUtils.getProperty(person,"name"));
// System.out.println("--------------------------------------");
//
BeanUtilsTest.invokeMethod();
// System.out.println("--------------------------------------");
// BeanUtilsTest.getAccessibleMethod();
// System.out.println("--------------------------------------");
// BeanUtilsTest.invokeConstructor();
// System.out.println("--------------------------------------");
BeanUtilsTest.getAccessibleConstructor();
}
//BeanUtils.cloneBean()--克隆目标bean
public static void cloneBean(Person person) throws Exception ...{
Person p = (Person)BeanUtils.cloneBean(person);
System.out.println(p.getAddress().getCity());
System.out.println(person.getAddress().getCity());
p.getAddress().setCity("hengyang");
System.out.println("---------------------------");
System.out.println(p.getAddress().getCity());
System.out.println(person.getAddress().getCity());
System.out.println(p.getAddress() == person.getAddress());
}
//BeanUtils.copyProperties()--拷贝目标bean中属性的值,能进行类型转换
public static void copyProperties(Person person) throws Exception ...{
Person p = new Person();
BeanUtils.copyProperties(p,person);
System.out.println(p.getAddress().getCity());
System.out.println(person.getAddress().getCity());
p.getAddress().setCity("hengyang");
System.out.println("---------------------------");
System.out.println(p.getAddress().getCity());
System.out.println(person.getAddress().getCity());
System.out.println(p.getAddress() == person.getAddress());
}
//PropertyUtils.copyProperties()--拷贝目标bean中属性的值,不能进行类型转换
public static void propertyUtilsCopyProperties(Person person) throws Exception ...{
Person p = new Person();
PropertyUtils.copyProperties(p,person);
System.out.println(p.getAddress().getCity());
System.out.println(person.getAddress().getCity());
p.getAddress().setCity("hengyang");
System.out.println("---------------------------");
System.out.println(p.getAddress().getCity());
System.out.println(person.getAddress().getCity());
System.out.println(p.getAddress() == person.getAddress());
}
//BeanUtils.copyProperty()--拷贝一个值给目标Bean的一个属性,能进行类型转换
public static void copyProperty(Person person) throws Exception ...{
Person p = new Person();
BeanUtils.copyProperty(p,"age","12");
System.out.println(p.getAge());
}
//BeanUtils.describe()--得到一个bean的所有描述,返回的是一个map
public static void describe(Person person) throws Exception ...{
Map map = BeanUtils.describe(person);
Set set = map.keySet();
for(Iterator it = set.iterator(); it.hasNext(); ) ...{
Object o = it.next();
System.out.println(o + ": " + map.get(o));
}
System.out.println("--------------------------------------");
// Collection c = map.values();
// for(Object o : c) {
// System.out.println(o);
// }
}
//PropertyUtils.describe()--得到一个bean的所有描述,返回的是一个map
public static void propertyUtilsDescribe(Person person) throws Exception ...{
Map map = PropertyUtils.describe(person);
Set set = map.keySet();
for(Iterator it = set.iterator(); it.hasNext(); ) ...{
Object o = it.next();
System.out.println(o + ": " + map.get(o));
}
System.out.println("--------------------------------------");
// Collection c = map.values();
// for(Object o : c) {
// System.out.println(o);
// }
// System.out.println("----------------------------");
}
//BeanUtils.populate()--使用一个map为bean赋值
public static void populate(Person person) throws Exception ...{
Map map = PropertyUtils.describe(person);
Person p = new Person();
BeanUtils.populate(p,map);
System.out.println(p.getName());
System.out.println(p.getSex());
System.out.println(p.getAge());
System.out.println(p.getAddress().getProvince());
System.out.println(p.getGoods().get(1));
System.out.println(p.getContact().get("student"));
System.out.println("--------------------------------------");
}
//BeanUtils.getArrayProperty()--将目标bean的一个属性的值转换成一个字符串数组
public static void getArrayProperty(Person person) throws Exception ...{
String[] array = BeanUtils.getArrayProperty(person,"goods");
for(String str : array) ...{
System.out.println(str);
}
}
//BeanUtils.getIndexedProperty()--可以检索目标bean中数组或集合类型的属性指定索引的值
public static void getIndexedProperty(Person person) throws Exception ...{
System.out.println(BeanUtils.getIndexedProperty(person,"goods[0]"));
System.out.println(BeanUtils.getIndexedProperty(person,"goods",1));
}
//BeanUtils.getMappedProperty()--可以检索目标bean中具有map类型的属性中对应键的值
public static void getMappedProperty(Person person) throws Exception ...{
System.out.println(BeanUtils.getMappedProperty(person,"contact(student)"));
System.out.println(BeanUtils.getMappedProperty(person,"contact","teacher"));
}
//BeanUtils.getNestedProperty()--得到目标bean中嵌套属性的值
public static void getNestedProperty(Person person) throws Exception ...{
System.out.println(BeanUtils.getNestedProperty(person,"address.province"));
}
//MethodUtils.invokeMethod()--动态调用方法
public static void invokeMethod() throws Exception ...{
//MethodUtils.invokeMethod(Object arg1,String arg2,Object[] arg3)
//arg1 一个bean的名称
//arg2 指定bean的方法名
//arg3 方法中的参数列表
MethodUtils.invokeMethod(new Person(),"sayHello",new Object[]...{});
//MethodUtils.invokeMethod(Object arg1,String arg2,Object arg3)
//arg1 一个bean的名称
//arg2 指定bean的方法名
//arg3 方法中的参数
MethodUtils.invokeExactMethod(new Person(),"f","hello");;
}
//MethodUtils.getAccessibleMethod()--得到方法
public static void getAccessibleMethod() throws Exception ...{
//MethodUtils.getAccessibleMethod(Class clazz,String methodName,Class[] parameterTypes)
//clazz 一个bean的类对象
//methodName bean中的方法名
//parameterTypes 方法中参数的类型列表
System.out.println(MethodUtils.getAccessibleMethod(Person.class,"f",new Class[]...{String.class}));
System.out.println(MethodUtils.getAccessibleMethod(Person.class,"f",String.class));
}
//ConstructorUtils.invokeConstructor()
public static void invokeConstructor() throws Exception ...{
//ConstructorUtils.invokeConstructor(Class clazz,Object[] args)
//clazz 一个bean的类对象
//args bean的构造函数的参数列表
Person p1 = (Person)ConstructorUtils.invokeConstructor(Person.class,new Object[]...{});
Person p2 = (Person)ConstructorUtils.invokeConstructor(Person.class,"Tom");
System.out.println(p2.getName());
}
//ConstructorUtils.getAccessibleConstructor()
public static void getAccessibleConstructor() throws Exception ...{
//ConstructorUtils.getAccessibleConstructor(Class clazz,Class[] parameterTypes)
//clazz 一个bean的类对象
//parameterTypes bean的构造函数的参数类型列表
System.out.println(ConstructorUtils.getAccessibleConstructor(Person.class,String.class));
System.out.println(ConstructorUtils.getAccessibleConstructor(Person.class,new Class[]...{String.class,String.class}));
}
}===========================================================================
package com.beanutils;
import java.util.*;
import org.apache.commons.beanutils.*;
public class BasicDynaBeanTest ...{
public static void main(String[] args) throws Exception ...{
//创建一个动态属性数组
DynaProperty[] properties = new DynaProperty[] ...{
new DynaProperty("name",String.class),
new DynaProperty("sex",String.class),
new DynaProperty("age",int.class),
new DynaProperty("address",Map.class)
};
//创建一个BasicDynaClass的实例
//第一个参数设置动态bean的名称
//第二个参数设置BasicDynaBean的类对象
//第三个参数设置一个动态属性数组
BasicDynaClass dynaClass = new BasicDynaClass("customer",BasicDynaBean.class,properties);
//通过dynaClass得到动态bean的一个实例
DynaBean customer = dynaClass.newInstance();
//给属性设值
Map map = new HashMap();
map.put("province","hunan");
map.put("city","changsha");
map.put("street","wuyilu");
map.put("zipcode","410000");
customer.set("name","Tom");
customer.set("sex","male");
customer.set("age",25);
customer.set("address",map);
System.out.println("name: " + customer.get("name"));
System.out.println("sex: " + customer.get("sex"));
System.out.println("age: " + customer.get("age"));
System.out.println("address: " + ((Map)customer.get("address")).get("province")
+ " " + ((Map)customer.get("address")).get("city")
+ " " + ((Map)customer.get("address")).get("street")
+ " " + ((Map)customer.get("address")).get("zipcode"));
}
}========================================================================
package com.beanutils;
import org.apache.commons.beanutils.*;
public class ConvertUtilsTest ...{
public static void main(String[] args) ...{
Person person = new Person();
//将任意的实例转变为String,用法非常简单相当与调用toString()方法
System.out.println(ConvertUtils.convert(person));
String str ="123456";
String ary = "45789";
//int ss = Integer.parseInt(str);
Integer s =(Integer)ConvertUtils.convert(str,Integer.class);
String[] values = ...{"123","456","789","aafs"};
Object o = ConvertUtils.convert(values,Integer.class);
System.out.println(o);
}
}================================================================================
package com.beanutils;
import java.util.*;
import org.apache.commons.beanutils.*;
public class LazyDynaBeanTest ...{
public static void main(String[] args) ...{
//创建一个LazyDynaBean的实例,它就和普通的bean一样
LazyDynaBean customer = new LazyDynaBean();
customer.set("name","Tom");
customer.set("sex","male");
customer.set("age",new Integer(25));
//以map方式给属性赋值,第一个参数是属性名,第二个参数是键,第三个参数是值
customer.set("address","province","hunan");
//如果属性是一个数组或集合,可以以索引方式赋值
//第一个参数是数组名或集合名,第二个参数是索引,第三个参数是数组和集合中存的值
customer.set("orders",0,"order001");
customer.set("orders",1,"order002");
System.out.println("name: " + customer.get("name"));
System.out.println("sex: " + customer.get("sex"));
System.out.println("age: " + customer.get("age"));
System.out.println("address: " + ((Map)customer.get("address")).get("province"));
System.out.println("orders: " + customer.get("orders",0) + " " + customer.get("orders",1));
}
}
package com.beanutils;
import java.util.*;
public class Person ...{
private String name;
private String sex;
private int age;
private Address address;
private List goods;
private Map contact;
public Person() ...{
}
public Person(String name) ...{
this.name = name;
}
public Person(String name,String sex) ...{
this.name = name;
this.sex = sex;
}
public String getName() ...{
return this.name;
}
public void setName(String name) ...{
this.name = name;
}
public String getSex() ...{
return this.sex;
}
public void setSex(String sex) ...{
this.sex = sex;
}
public int getAge() ...{
return this.age;
}
public void setAge(int age) ...{
this.age = age;
}
public Address getAddress() ...{
return this.address;
}
public void setAddress(Address address) ...{
this.address = address;
}
public List getGoods() ...{
return this.goods;
}
public void setGoods(List goods) ...{
this.goods = goods;
}
public Map getContact() ...{
return this.contact;
}
public void setContact(Map contact) ...{
this.contact = contact;
}
public void sayHello() ...{
System.out.println("Hello World!");
}
public void f(String str) ...{
System.out.println("Person.f()..." + str);
}
public String toString() ...{
return "Person.toString()...";
}
}=========================================================================================
package com.beanutils;
public class Address ...{
private String province;
private String city;
private String street;
private String zipcode;
public String getProvince() ...{
return this.province;
}
public void setProvince(String province) ...{
this.province = province;
}
public String getCity() ...{
return this.city;
}
public void setCity(String city) ...{
this.city = city;
}
public String getStreet() ...{
return this.street;
}
public void setStreet(String street) ...{
this.street = street;
}
public String getZipcode() ...{
return this.zipcode;
}
public void setZipcode(String zipcode) ...{
this.zipcode = zipcode;
}
}============================================================================
一个测试类
package com.beanutils;
import java.util.*;
import org.apache.commons.beanutils.*;
public class BeanUtilsTest ...{
public static void main(String[] args) throws Exception ...{
Address address = new Address();
address.setProvince("hunan");
address.setCity("changsha");
address.setStreet("wuyilu");
address.setZipcode("410000");
List goods = new ArrayList();
goods.add("sporting");
goods.add("singing");
goods.add("dancing");
Map contact = new HashMap();
contact.put("student","Tom");
contact.put("teacher","Lucy");
Person person = new Person();
person.setName("Mike");
person.setSex("male");
person.setAge(25);
person.setAddress(address);
person.setGoods(goods);
person.setContact(contact);
//BeanUtilsTest.cloneBean(person);
//BeanUtilsTest.copyProperties(person);
//BeanUtilsTest.propertyUtilsCopyProperties(person);
//BeanUtilsTest.copyProperty(person);
//BeanUtilsTest.describe(person);
//BeanUtilsTest.propertyUtilsDescribe(person);
//BeanUtilsTest.getArrayProperty(person);
//BeanUtilsTest.getIndexedProperty(person);
//BeanUtilsTest.getMappedProperty(person);
//BeanUtilsTest.getNestedProperty(person);
//BeanUtilsTest.populate(person);
//得到一个bean中指定属性的值,相当于getter
// System.out.println(BeanUtils.getProperty(person,"name"));
// //修改一个bean中指定属性的值,相当于setter
// BeanUtils.setProperty(person,"name","Tom");
// System.out.println(BeanUtils.getProperty(person,"name"));
// System.out.println("--------------------------------------");
//
BeanUtilsTest.invokeMethod();
// System.out.println("--------------------------------------");
// BeanUtilsTest.getAccessibleMethod();
// System.out.println("--------------------------------------");
// BeanUtilsTest.invokeConstructor();
// System.out.println("--------------------------------------");
BeanUtilsTest.getAccessibleConstructor();
}
//BeanUtils.cloneBean()--克隆目标bean
public static void cloneBean(Person person) throws Exception ...{
Person p = (Person)BeanUtils.cloneBean(person);
System.out.println(p.getAddress().getCity());
System.out.println(person.getAddress().getCity());
p.getAddress().setCity("hengyang");
System.out.println("---------------------------");
System.out.println(p.getAddress().getCity());
System.out.println(person.getAddress().getCity());
System.out.println(p.getAddress() == person.getAddress());
}
//BeanUtils.copyProperties()--拷贝目标bean中属性的值,能进行类型转换
public static void copyProperties(Person person) throws Exception ...{
Person p = new Person();
BeanUtils.copyProperties(p,person);
System.out.println(p.getAddress().getCity());
System.out.println(person.getAddress().getCity());
p.getAddress().setCity("hengyang");
System.out.println("---------------------------");
System.out.println(p.getAddress().getCity());
System.out.println(person.getAddress().getCity());
System.out.println(p.getAddress() == person.getAddress());
}
//PropertyUtils.copyProperties()--拷贝目标bean中属性的值,不能进行类型转换
public static void propertyUtilsCopyProperties(Person person) throws Exception ...{
Person p = new Person();
PropertyUtils.copyProperties(p,person);
System.out.println(p.getAddress().getCity());
System.out.println(person.getAddress().getCity());
p.getAddress().setCity("hengyang");
System.out.println("---------------------------");
System.out.println(p.getAddress().getCity());
System.out.println(person.getAddress().getCity());
System.out.println(p.getAddress() == person.getAddress());
}
//BeanUtils.copyProperty()--拷贝一个值给目标Bean的一个属性,能进行类型转换
public static void copyProperty(Person person) throws Exception ...{
Person p = new Person();
BeanUtils.copyProperty(p,"age","12");
System.out.println(p.getAge());
}
//BeanUtils.describe()--得到一个bean的所有描述,返回的是一个map
public static void describe(Person person) throws Exception ...{
Map map = BeanUtils.describe(person);
Set set = map.keySet();
for(Iterator it = set.iterator(); it.hasNext(); ) ...{
Object o = it.next();
System.out.println(o + ": " + map.get(o));
}
System.out.println("--------------------------------------");
// Collection c = map.values();
// for(Object o : c) {
// System.out.println(o);
// }
}
//PropertyUtils.describe()--得到一个bean的所有描述,返回的是一个map
public static void propertyUtilsDescribe(Person person) throws Exception ...{
Map map = PropertyUtils.describe(person);
Set set = map.keySet();
for(Iterator it = set.iterator(); it.hasNext(); ) ...{
Object o = it.next();
System.out.println(o + ": " + map.get(o));
}
System.out.println("--------------------------------------");
// Collection c = map.values();
// for(Object o : c) {
// System.out.println(o);
// }
// System.out.println("----------------------------");
}
//BeanUtils.populate()--使用一个map为bean赋值
public static void populate(Person person) throws Exception ...{
Map map = PropertyUtils.describe(person);
Person p = new Person();
BeanUtils.populate(p,map);
System.out.println(p.getName());
System.out.println(p.getSex());
System.out.println(p.getAge());
System.out.println(p.getAddress().getProvince());
System.out.println(p.getGoods().get(1));
System.out.println(p.getContact().get("student"));
System.out.println("--------------------------------------");
}
//BeanUtils.getArrayProperty()--将目标bean的一个属性的值转换成一个字符串数组
public static void getArrayProperty(Person person) throws Exception ...{
String[] array = BeanUtils.getArrayProperty(person,"goods");
for(String str : array) ...{
System.out.println(str);
}
}
//BeanUtils.getIndexedProperty()--可以检索目标bean中数组或集合类型的属性指定索引的值
public static void getIndexedProperty(Person person) throws Exception ...{
System.out.println(BeanUtils.getIndexedProperty(person,"goods[0]"));
System.out.println(BeanUtils.getIndexedProperty(person,"goods",1));
}
//BeanUtils.getMappedProperty()--可以检索目标bean中具有map类型的属性中对应键的值
public static void getMappedProperty(Person person) throws Exception ...{
System.out.println(BeanUtils.getMappedProperty(person,"contact(student)"));
System.out.println(BeanUtils.getMappedProperty(person,"contact","teacher"));
}
//BeanUtils.getNestedProperty()--得到目标bean中嵌套属性的值
public static void getNestedProperty(Person person) throws Exception ...{
System.out.println(BeanUtils.getNestedProperty(person,"address.province"));
}
//MethodUtils.invokeMethod()--动态调用方法
public static void invokeMethod() throws Exception ...{
//MethodUtils.invokeMethod(Object arg1,String arg2,Object[] arg3)
//arg1 一个bean的名称
//arg2 指定bean的方法名
//arg3 方法中的参数列表
MethodUtils.invokeMethod(new Person(),"sayHello",new Object[]...{});
//MethodUtils.invokeMethod(Object arg1,String arg2,Object arg3)
//arg1 一个bean的名称
//arg2 指定bean的方法名
//arg3 方法中的参数
MethodUtils.invokeExactMethod(new Person(),"f","hello");;
}
//MethodUtils.getAccessibleMethod()--得到方法
public static void getAccessibleMethod() throws Exception ...{
//MethodUtils.getAccessibleMethod(Class clazz,String methodName,Class[] parameterTypes)
//clazz 一个bean的类对象
//methodName bean中的方法名
//parameterTypes 方法中参数的类型列表
System.out.println(MethodUtils.getAccessibleMethod(Person.class,"f",new Class[]...{String.class}));
System.out.println(MethodUtils.getAccessibleMethod(Person.class,"f",String.class));
}
//ConstructorUtils.invokeConstructor()
public static void invokeConstructor() throws Exception ...{
//ConstructorUtils.invokeConstructor(Class clazz,Object[] args)
//clazz 一个bean的类对象
//args bean的构造函数的参数列表
Person p1 = (Person)ConstructorUtils.invokeConstructor(Person.class,new Object[]...{});
Person p2 = (Person)ConstructorUtils.invokeConstructor(Person.class,"Tom");
System.out.println(p2.getName());
}
//ConstructorUtils.getAccessibleConstructor()
public static void getAccessibleConstructor() throws Exception ...{
//ConstructorUtils.getAccessibleConstructor(Class clazz,Class[] parameterTypes)
//clazz 一个bean的类对象
//parameterTypes bean的构造函数的参数类型列表
System.out.println(ConstructorUtils.getAccessibleConstructor(Person.class,String.class));
System.out.println(ConstructorUtils.getAccessibleConstructor(Person.class,new Class[]...{String.class,String.class}));
}
}===========================================================================
package com.beanutils;
import java.util.*;
import org.apache.commons.beanutils.*;
public class BasicDynaBeanTest ...{
public static void main(String[] args) throws Exception ...{
//创建一个动态属性数组
DynaProperty[] properties = new DynaProperty[] ...{
new DynaProperty("name",String.class),
new DynaProperty("sex",String.class),
new DynaProperty("age",int.class),
new DynaProperty("address",Map.class)
};
//创建一个BasicDynaClass的实例
//第一个参数设置动态bean的名称
//第二个参数设置BasicDynaBean的类对象
//第三个参数设置一个动态属性数组
BasicDynaClass dynaClass = new BasicDynaClass("customer",BasicDynaBean.class,properties);
//通过dynaClass得到动态bean的一个实例
DynaBean customer = dynaClass.newInstance();
//给属性设值
Map map = new HashMap();
map.put("province","hunan");
map.put("city","changsha");
map.put("street","wuyilu");
map.put("zipcode","410000");
customer.set("name","Tom");
customer.set("sex","male");
customer.set("age",25);
customer.set("address",map);
System.out.println("name: " + customer.get("name"));
System.out.println("sex: " + customer.get("sex"));
System.out.println("age: " + customer.get("age"));
System.out.println("address: " + ((Map)customer.get("address")).get("province")
+ " " + ((Map)customer.get("address")).get("city")
+ " " + ((Map)customer.get("address")).get("street")
+ " " + ((Map)customer.get("address")).get("zipcode"));
}
}========================================================================
package com.beanutils;
import org.apache.commons.beanutils.*;
public class ConvertUtilsTest ...{
public static void main(String[] args) ...{
Person person = new Person();
//将任意的实例转变为String,用法非常简单相当与调用toString()方法
System.out.println(ConvertUtils.convert(person));
String str ="123456";
String ary = "45789";
//int ss = Integer.parseInt(str);
Integer s =(Integer)ConvertUtils.convert(str,Integer.class);
String[] values = ...{"123","456","789","aafs"};
Object o = ConvertUtils.convert(values,Integer.class);
System.out.println(o);
}
}================================================================================
package com.beanutils;
import java.util.*;
import org.apache.commons.beanutils.*;
public class LazyDynaBeanTest ...{
public static void main(String[] args) ...{
//创建一个LazyDynaBean的实例,它就和普通的bean一样
LazyDynaBean customer = new LazyDynaBean();
customer.set("name","Tom");
customer.set("sex","male");
customer.set("age",new Integer(25));
//以map方式给属性赋值,第一个参数是属性名,第二个参数是键,第三个参数是值
customer.set("address","province","hunan");
//如果属性是一个数组或集合,可以以索引方式赋值
//第一个参数是数组名或集合名,第二个参数是索引,第三个参数是数组和集合中存的值
customer.set("orders",0,"order001");
customer.set("orders",1,"order002");
System.out.println("name: " + customer.get("name"));
System.out.println("sex: " + customer.get("sex"));
System.out.println("age: " + customer.get("age"));
System.out.println("address: " + ((Map)customer.get("address")).get("province"));
System.out.println("orders: " + customer.get("orders",0) + " " + customer.get("orders",1));
}
}
发表评论
-
Jackson2.x通用工具类
2014-11-03 11:38 4904import java.io.IOException; i ... -
面试题
2013-02-27 09:04 1707从1加到100(考虑减少循环次数)使用数学公式 首先要知道 ... -
单例延迟实例化
2013-01-23 08:55 13271.如果出于性能的考虑而需要对实例域使用延迟初始化,就使用双 ... -
java中重载与重写的区别
2013-01-21 10:03 925首先我们来讲讲:重载(Overloading) ( ... -
自定义标签
2012-12-15 12:58 997package com.fsti.tag; import ... -
JAXB格式化beanToXml
2012-12-14 15:03 1274context = JAXBContext.n ... -
面向对象的特征有哪些方面?
2012-12-13 09:36 1007计算机软件系统是现实 ... -
java nio缓冲器
2012-12-05 17:03 3603缓冲器仅仅是一个" 多功能 " 的数组。可 ... -
面向对象三大特性一句话概括
2012-12-04 15:58 1553封装可以隐藏实现细节,使得代码模块化; 继承可以扩 ... -
Java序列化高级认识
2012-12-04 09:13 1065将 Java 对象序列化为二进制文件的 Java 序列化技术是 ... -
面向接口编程——提升系统多态性和可扩展性
2012-12-03 14:10 1284接口的本质 接口,在表面上是由几个没有主体代码的方 ... -
面向对象之多态
2012-11-23 19:22 874多态性(polymorphisn)是允许你将父对象设置成为和一 ... -
我对"秒杀"在技术性上的一些看法
2012-11-22 11:31 1010秒杀,是指电子商务 ... -
项目编码
2012-11-16 13:59 1156看两个项目所用的编码是否一样 Java的乱码问题: ... -
SVN错误:Attempted to lock an already-locked dir
2012-11-07 09:08 1027出现这个问题后使用“ ... -
面向对象的三个基本特征
2012-11-04 08:31 1087面向对象的三个基本特征是:封装、继承、多态。 封装 封装最 ... -
预编译防sql注入
2012-11-03 20:23 1972prepareStatement会先初始化SQL,先把这个SQ ... -
Java类与对象的初始化
2012-10-19 09:33 896Java类与对象的初始化 面试的时候,经常会遇到这样的笔试题 ... -
java工具DateUtil
2012-10-12 14:42 1052//一年内的周一 public final class Da ... -
JSP自定义标签实现过程
2012-10-03 09:34 896本文将通过自定义标签显示日期为例,简单谈谈JSP自定义标签 ...
相关推荐
Java中的Apache Commons Betwixt库提供了一个简单的方法来映射Java Bean对象和XML文档之间的数据,使得在处理数据交换时能轻松地实现bean到XML的转换和XML到bean的反转换。本篇文章将深入探讨如何使用Commons ...
Apache Commons 是一系列小型实用工具库,为Java开发者提供了丰富的功能扩展。这些库简化了常见的编程任务,提高了开发效率。在给定的压缩包文件中,包含的三个jar包分别是: 1. **Apache Commons BeanUtils** - `...
commons-beanutils-bean-collections.jar commons-beanutils-bean-collections.jar
commons-beanutils-bean-collections-1.7.0.jar
commons-beanutils-1.8.0 jar包: 1.commons-beanutils-1.8.0.jar 2.commons-beanutils-1.8.0-javadoc.jar ...4.commons-beanutils-bean-collections-1.8.0.jar 5.commons-beanutils-core-1.8.0.jar
commons-beanutils-bean-collections-1.8.0.jar
commons-beanutils-bean-collections-1.8.3.jar
这里提到的三个JAR文件——`commons-beanutils.jar`、`commons-collections-3.1.jar`和`commons-pool-1.2.jar`,都是Apache Commons项目的一部分,分别涉及Bean操作、集合操作和对象池化。 **1. `commons-beanutils...
Apache Commons BeanUtils是Java开发中一个非常重要的工具库,它为开发者提供了大量便捷的JavaBean操作功能。在本文中,我们将深入探讨`commons-beanutils-1.9.4`这个版本,了解其核心功能、使用场景以及如何在项目...
标题提到的"commons的各种jar包"指的是Apache Commons项目下的不同模块,这些模块各自专注于特定的领域,如日志、Bean操作、集合处理以及语言工具等。下面我们将逐一探讨这些模块及其提供的功能。 1. **Apache ...
Commons Collections和Apache BeanUtils是Java开发中常用的两个库,它们为开发者提供了丰富的工具类和功能,使得处理集合对象和Bean属性变得更加便捷。这两个库在Java Web开发中扮演着重要角色,尤其是在构建MVC框架...
`commons-beanutils` 是Apache软件基金会开发的一个Java库,主要提供了一系列用于操作Java Bean的工具函数。这个库简化了对Java对象属性的访问,使得开发者可以方便地进行Bean对象的属性设置、获取以及拷贝等工作,...
5. **commons-beanutils-bean-collections.jar**:这是BeanUtils模块的一个扩展,增加了对Java 5的`java.util.collections`接口的支持,方便在Bean和集合间进行操作。 6. **commons-fileupload.jar**:Apache ...
Apache Commons BeanUtils是Java开发中的一个非常重要的工具库,它为开发者提供了便利的方式来操作JavaBeans。这个库的主要目标是简化JavaBean属性的访问,通过提供一套简单易用的API,使得开发者无需直接调用getter...
### Apache Commons包的用法详解 #### 一、概述 Apache Commons是一系列为Java开发者提供的开源组件库,旨在提供各种工具来简化日常编程任务。它包括了许多实用的类和工具,帮助开发人员处理常见的编程问题,减少...
标签"commons-bean"表明这个库是Apache Commons项目的一部分,这是一个提供各种通用工具函数和组件的集合。"maven"则说明了该库与Maven的兼容性,意味着开发人员可以方便地通过Maven依赖管理来集成BeanUtils。 在...
commons-beanutils-bean-collections-1.8.2.jar commons-beanutils-core-1.8.2.jar commons-codec-1.3.jar commons-collections-3.2.1.jar commons-collections-testframework-3.2.1.jar commons-dbcp-1.2.1.jar ...
7. **commons-beanutils-bean-collections-1.8.0.jar**:这个模块是BeanUtils的扩展,专注于处理Java集合和JavaBeans之间的交互,提供了将JavaBean属性映射到集合以及从集合中复制属性到JavaBean的能力。 这些...
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <!-- 其他配置属性 --> </bean> ``` 总的来说,Apache Commons DBCP和Commons Pool在Java应用中扮演了重要的角色,尤其是对于...
例如,在Spring配置文件中,可以通过 `<bean>` 元素来配置 `BasicDataSource`,并将其注入到其他需要数据库连接的bean中。 总的来说,Apache Commons Pool 和 Commons DBCP 是Java开发中必不可少的工具,它们为高效...