BeanUtils是一种很实用的封装好的javaBean, 是apache机构下的一个组件, 这里初略地讲述一下BeanUtils的实用性,以代码来讲述.
User.java
- package com.deng;
- /**
- * @author: 邓永胜
- * @date: Jan 19, 2010 - 10:27:57 PM
- */
- public class User {
- private String username;
- private String password;
- private Long userId;
- private Profile profile;
- public String getUsername() {
- return username;
- }
- public void setUsername(String username) {
- this.username = username;
- }
- public String getPassword() {
- return password;
- }
- public void setPassword(String password) {
- this.password = password;
- }
- public Long getUserId() {
- return userId;
- }
- public void setUserId(Long userId) {
- this.userId = userId;
- }
- public Profile getProfile() {
- return profile;
- }
- public void setProfile(Profile profile) {
- this.profile = profile;
- }
- }
Profile.java
- package com.deng;
- import java.util.Date;
- import java.util.Map;
- /**
- * @author: 邓永胜
- * @date: Jan 19, 2010 - 10:28:22 PM
- */
- public class Profile {
- private String email ;
- private Date birthDate;
- private Address[] address;
- private Map<String, String> phone;
- public String getEmail() {
- return email;
- }
- public void setEmail(String email) {
- this.email = email;
- }
- public Date getBirthDate() {
- return birthDate;
- }
- public void setBirthDate(Date birthDate) {
- this.birthDate = birthDate;
- }
- public Address[] getAddress() {
- return address;
- }
- public void setAddress(Address[] address) {
- this.address = address;
- }
- public Map<String, String> getPhone() {
- return phone;
- }
- public void setPhone(Map<String, String> phone) {
- this.phone = phone;
- }
- }
Address.java
- package com.deng;
- /**
- * @author: 邓永胜
- * @date: Jan 19, 2010 - 10:32:11 PM
- */
- public class Address {
- private String country;
- private String city;
- private String addr;
- private String postcode;
- public Address(String country, String city, String addr, String postcode) {
- this.country = country;
- this.city = city;
- this.addr = addr;
- this.postcode = postcode;
- }
- public Address() {
- }
- public String getCountry() {
- return country;
- }
- public void setCountry(String country) {
- this.country = country;
- }
- public String getCity() {
- return city;
- }
- public void setCity(String city) {
- this.city = city;
- }
- public String getAddr() {
- return addr;
- }
- public void setAddr(String addr) {
- this.addr = addr;
- }
- public String getPostcode() {
- return postcode;
- }
- public void setPostcode(String postcode) {
- this.postcode = postcode;
- }
- }
BeanUtilExample.java
- package com.deng;
- import java.lang.reflect.InvocationTargetException;
- import java.util.GregorianCalendar;
- import java.util.HashMap;
- import java.util.Map;
- import org.apache.commons.beanutils.*;
- /**
- * @author: 邓永胜
- * @date: Jan 19, 2010 - 10:51:40 PM
- */
- public class BeanUtilExample {
- /**
- * @param args
- */
- public User prepareDate() {
- Profile profile = new Profile();
- profile.setEmail("dys0411q1@126.com");
- profile.setBirthDate(new GregorianCalendar(2046, 11, 11).getTime());
- Map<String, String> phone = new HashMap<String, String>();
- phone.put("home", "6861543");
- phone.put("office", "123456");
- phone.put("mobile", "15920054876");
- profile.setPhone(phone);
- Address address1 = new Address("中国", "北京", "天安门一路", "1001");
- Address address2 = new Address("中国", "深圳", "深南大道", "1002");
- Address[] address = { address1, address2 };
- profile.setAddress(address);
- User user = new User();
- user.setUserId(new Long(123456789));
- user.setUsername("deng");
- user.setPassword("123456");
- user.setProfile(profile);
- return user;
- }
- public static void main(String[] args) {
- BeanUtilExample example = new BeanUtilExample();
- User user = example.prepareDate();
- try {
- System.out.println(BeanUtils.getProperty(user, "userId")); // 输出user类的userId的值
- System.out.println(PropertyUtils.getProperty(user, "userId"));
- System.out.println(BeanUtils.getProperty(user, "username"));
- System.out.println(BeanUtils.getProperty(user, "password"));
- System.out.println(BeanUtils.getProperty(user, "profile"));
- System.out.println(BeanUtils.getProperty(user, "profile.email"));
- System.out.println(BeanUtils.getProperty(user, "profile.birthDate"));
- //phone的输出格式:{mobile=15920054876, office=123456, home=6861543}
- System.out.println(BeanUtils.getProperty(user, "profile.phone"));
- System.out.println(BeanUtils.getProperty(user, "profile.phone(mobile)"));
- System.out.println(BeanUtils.getProperty(user, "profile.phone(office)"));
- System.out.println(BeanUtils.getProperty(user, "profile.phone(home)"));
- System.out.println(BeanUtils.getProperty(user, "profile.address[0].country"));
- System.out.println(BeanUtils.getProperty(user, "profile.address[0].city"));
- System.out.println(BeanUtils.getProperty(user, "profile.address[0].postcode"));
- User user2 = new User();
- BeanUtils.copyProperties(user2, user);
- System.out.println(BeanUtils.getProperty(user2, "userId")); // 输出user类的userId的值
- System.out.println(PropertyUtils.getProperty(user2, "userId"));
- System.out.println(BeanUtils.getProperty(user2, "username"));
- System.out.println(BeanUtils.getProperty(user2, "password"));
- System.out.println(BeanUtils.getProperty(user2, "profile"));
- System.out.println(BeanUtils.getProperty(user2, "profile.email"));
- System.out.println(BeanUtils.getProperty(user2, "profile.birthDate"));
- //phone的输出格式:{mobile=15920054876, office=123456, home=6861543}
- System.out.println(BeanUtils.getProperty(user2, "profile.phone"));
- System.out.println(BeanUtils.getProperty(user2, "profile.phone(mobile)"));
- System.out.println(BeanUtils.getProperty(user2, "profile.phone(office)"));
- System.out.println(BeanUtils.getProperty(user2, "profile.phone(home)"));
- System.out.println(BeanUtils.getProperty(user2, "profile.address[0].country"));
- System.out.println(BeanUtils.getProperty(user2, "profile.address[0].city"));
- System.out.println(BeanUtils.getProperty(user2, "profile.address[0].postcode"));
- } catch (IllegalAccessException e) {
- e.printStackTrace();
- } catch (InvocationTargetException e) {
- e.printStackTrace();
- } catch (NoSuchMethodException e) {
- e.printStackTrace();
- }
- }
- }
BeanUtils的一个很重要的用法是可以逐连读取各个类的属性值以及内嵌的属性值,这里我所理解的内嵌的属性值是指一个类中的属性是另一个类, 比如类User,它包括了Profile这个属性类, Profile这个类的其中一个属性值为Address, 这个Address又是一个类, 如果想要取得Address类里面的某一个属性值时, 在传统的方法中是不能达到的. 这里的所有类及其属性都设置了值
User user = new User();
user.getProfile().getAddress(), 这里只能取得Address这个类,并不能导航到下面的属性值, 而BeanUtils可以做到
下面列举一下LazyDynaBean 的用法
LazyDynaBeanExample.java
- package com.deng;
- import java.lang.reflect.InvocationTargetException;
- import org.apache.commons.beanutils.BeanUtils;
- import org.apache.commons.beanutils.LazyDynaBean;
- /**
- * @author: 邓永胜
- * @date: Jan 19, 2010 - 11:45:17 PM
- */
- public class LazyDynaBeanExample {
- public static void main(String[] args) {
- LazyDynaBean person = new LazyDynaBean();
- person.set("name", "deng");
- person.set("password", "123");
- System.out.println(person.get("name"));
- System.out.println(person.get("password"));
- try {
- System.out.println(BeanUtils.getProperty(person, "name"));
- System.out.println(BeanUtils.getProperty(person, "password"));
- } catch (IllegalAccessException e) {
- e.printStackTrace();
- } catch (InvocationTargetException e) {
- e.printStackTrace();
- } catch (NoSuchMethodException e) {
- e.printStackTrace();
- }
- }
- }
可以动态地生成类,并设置值和取得值