commons-beanutils使用介绍
来源:互联网 发布:动画生成软件 编辑:程序博客网 时间:2024/06/06 01:17
commons-beanutils
是Apache开源组织提供的用于操作JAVA BEAN的工具包。使用commons-beanutils
,我们可以很方便的对bean对象的属性进行操作。今天为大家介绍一下该包的常用方法。
在介绍常用类之前,我们先来 编写一个用于测试的BEAN类:
package com.gujin.entity;public class UserInfo{ private String userId; private String userName; public UserInfo() { } public UserInfo(String userId, String userName) { this.userId = userId; this.userName = userName; } public String getUserId() { return userId; } public void setUserId(String userId) { this.userId = userId; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } @Override public String toString() { return String.format("{userId:%s,userName:%s}", userId, userName); }}
MethodUtils
MethodUtils
通过反射访问对象的方法并且执行方法。
方法摘要:
使用示例:
package com.gujin.beanutils;import java.lang.reflect.Method;import org.apache.commons.beanutils.MethodUtils;import org.junit.Test;import com.gujin.entity.UserInfo;public class MethodUtilsTest{ @Test public void test() throws Exception { UserInfo userInfo = new UserInfo(); // 通过方法名和参数类型获得可访问方法 Method method = MethodUtils.getAccessibleMethod(UserInfo.class, "setUserId", String.class); method.invoke(userInfo, "jianggujin"); // 可以直接通过invokeMethod执行方法 MethodUtils.invokeMethod(userInfo, "setUserName", "蒋固金"); System.out.println(userInfo); }}
运行结果:
{userId:jianggujin,userName:蒋固金}
ConstructorUtils
ConstructorUtils
通过反射提供了构造方法相关的便捷操作方法。
方法摘要:
使用示例:
package com.gujin.beanutils;import java.lang.reflect.Constructor;import org.apache.commons.beanutils.ConstructorUtils;import org.junit.Test;import com.gujin.entity.UserInfo;public class ConstructorUtilsTest{ @Test public void test() throws Exception { Constructor<UserInfo> constructor = ConstructorUtils .getAccessibleConstructor(UserInfo.class, new Class[] { String.class, String.class }); System.out.println(constructor.newInstance("jianggujin", "蒋固金")); // 更简洁的写法 UserInfo userInfo = ConstructorUtils.invokeConstructor(UserInfo.class, new String[] { "jianggujin", "蒋固金" }); System.out.println(userInfo); }}
运行结果:
{userId:jianggujin,userName:蒋固金}
{userId:jianggujin,userName:蒋固金}
PropertyUtils
PropertyUtils
通过反射提供了对象属性的便捷操作方法。
方法摘要:
使用示例:
package com.gujin.beanutils;import org.apache.commons.beanutils.PropertyUtils;import org.junit.Test;import com.gujin.entity.UserInfo;public class PropertyUtilsTest{ @Test public void test() throws Exception { UserInfo userInfo = new UserInfo("jianggujin", "蒋固金"); UserInfo copyed = new UserInfo(); PropertyUtils.copyProperties(copyed, userInfo); System.out.println(copyed); System.out.println(PropertyUtils.describe(userInfo)); PropertyUtils.setProperty(userInfo, "userId", "gjjiang"); System.out.println(userInfo); }}
运行结果:
{userId:jianggujin,userName:蒋固金}
{userId=jianggujin, class=class com.gujin.entity.UserInfo, userName=蒋固金}
{userId:gjjiang,userName:蒋固金}
BeanUtils
BeanUtils
通过反射提供了Bean对象的便捷操作方法。
方法摘要:
使用示例:
package com.gujin.beanutils;import java.util.HashMap;import org.apache.commons.beanutils.BeanUtils;import org.junit.Test;import com.gujin.entity.UserInfo;public class BeanUtilsTest{ @Test public void test() throws Exception { UserInfo userInfo = new UserInfo(); HashMap<String, String> properties = new HashMap<String, String>(); properties.put("userId", "jianggujin"); properties.put("userName", "蒋固金"); BeanUtils.populate(userInfo, properties); System.out.println(userInfo); }}
运行结果:
{userId:jianggujin,userName:蒋固金}
ConvertUtils
ConvertUtils
提供了数据类型相互转换的方法。
方法摘要:
方法摘要:
使用示例:
package com.gujin.beanutils;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Date;import org.apache.commons.beanutils.ConvertUtils;import org.apache.commons.beanutils.Converter;import org.junit.Test;public class ConvertUtilsTest{ @Test public void test() throws Exception { ConvertUtils.register(new Converter() { @Override public <T> T convert(Class<T> arg0, Object arg1) { try { return (T) new SimpleDateFormat("yyyy-MM-dd HH:mm:ss") .parse((String) arg1); } catch (ParseException e) { return null; } } }, Date.class); System.out.println(ConvertUtils .convert("2016-04-09 12:41:00", Date.class)); }}
运行结果:
Sat Apr 09 12:41:00 CST 2016
0 0
- commons-beanutils使用介绍
- commons-beanutils使用介绍
- Apache Commons BeanUtils组件介绍使用
- Jar包commons-beanutils使用介绍
- 第六十篇:commons-beanutils使用介绍
- commons - BeanUtils 使用
- commons-beanutils使用
- commons-beanutils使用
- Apache Commons beanutils使用
- apache.commons.beanutils.BeanUtils的使用
- org.apache.commons.beanutils.BeanUtils使用示例
- commons-beanutils包的使用
- Commons BeanUtils的使用
- apache Commons-beanutils的使用
- java中commons-beanutils的介绍
- Java中commons-beanutils的介绍
- Commons-Beanutils
- commons-beanutils
- 《统计学习方法》笔记(8):AdaBoost算法
- 设计模式-单态模式
- STL vector
- 【POJ 3306】【素数筛选】Dirichlet's Theorem on Arithmetic Progressions
- vim使用
- commons-beanutils使用介绍
- 94. Binary Tree Inorder Traversal
- iOS开发UICollectionView瀑布流效果
- Linux ls -l命令的实现
- 一种记忆用户使用习惯的方法及系统
- 【HDU 2955】【背包问题】Robberies
- 【Shiro + Spring MVC整合】教程
- OC中weak/assign/strong
- ifconfig命令