http://www.blogjava.net/sean/articles/Jakarta_Commons_Notes.html
[Jakarta Commons笔记] 开篇
[Jakarta Commons笔记] Commons Lang
[Jakarta Commons笔记] org.apache.commons.lang
[Jakarta Commons笔记] 代码范例 - ArrayUtils
[Jakarta Commons笔记] 代码范例 - StringUtils
[Jakarta Commons笔记] org.apache.commons.lang.builder
[Jakarta Commons笔记] org.apache.commons.lang.math
[Jakarta Commons笔记] org.apache.commons.lang.time
[Jakarta Commons笔记] Commons BeanUtils
[Jakarta Commons笔记] 代码范例 - BeanUtils
[Jakarta Commons笔记] Commons Collections
[Jakarta Commons笔记] Commons Collections - Bag组
[Jakarta Commons笔记] Commons Collections - Buffer组
[Jakarta Commons笔记] Commons Collections - Map组
[Jakarta Commons笔记] Commons Collections - Collection组
[Jakarta Commons笔记] Commons Collections - Comparator组
[Jakarta Commons笔记] Commons Collections - Predicate组
[Jakarta Commons笔记] Commons Collections - Transformer组
[Jakarta Commons笔记] Commons Collections - Closure组
[Jakarta Commons笔记] Commons Collections - Iterator组
[Jakarta Commons笔记] 结语
1)org.apache.commons.lang包
这个包提供了一些有用的包含static方法的Util类。除了6个Exception类和2个已经deprecated的数字类之外,commons.lang包共包含了17个实用的类:
ArrayUtils – 用于对数组的操作,如添加、查找、删除、子数组、倒序、元素类型转换等;
BitField – 用于操作位元,提供了一些方便而安全的方法;
BooleanUtils – 用于操作和转换boolean或者Boolean及相应的数组;
CharEncoding – 包含了Java环境支持的字符编码,提供是否支持某种编码的判断;
CharRange – 用于设定字符范围并做相应检查;
CharSet – 用于设定一组字符作为范围并做相应检查;
CharSetUtils – 用于操作CharSet;
CharUtils – 用于操作char值和Character对象;
ClassUtils – 用于对Java类的操作,不使用反射;
ObjectUtils – 用于操作Java对象,提供null安全的访问和其他一些功能;
RandomStringUtils – 用于生成随机的字符串;(验证码)
SerializationUtils – 用于处理对象序列化,提供比一般Java序列化更高级的处理能力;
StringEscapeUtils – 用于正确处理转义字符,产生正确的Java、JavaScript、HTML、XML和SQL代码;
StringUtils – 处理String的核心类,提供了相当多的功能;(判断字符串是否为空等:isBlank(String str)、isNumeric(String str)、reverse(String str)等)
SystemUtils – 在java.lang.System基础上提供更方便的访问,如用户路径、Java版本、时区、操作系统等判断;
Validate – 提供验证的操作,有点类似assert断言;
WordUtils – 用于处理单词大小写、换行等。
StringUtils:
(1) StringUtils.center(" demoNormalJavaBeans ", 40, "=");
显示结果:========= demoNormalJavaBeans ==========
解释:参数1显示在中间,左右用"="号包裹,输出总长度为40。
(2) Object[] rawOutput1 = new Object[] { "The city of customer ", name,"'s first address is ", city, "." };
System.out.println(StringUtils.join(rawOutput1));
显示结果:The city of customer John Smith's first address is Los Angeles.
解释:将数组元素连接起来。
2) org.apache.commons.configuration 配置包
Configuration parameters may be loaded from the following sources: 处理的配置文件类型:
- Properties files
- XML documents
- Windows INI files
- Property list files (plist)
- JNDI
- JDBC Datasource
- System properties
- Applet parameters
- Servlet parameters
Different configuration sources can be mixed using a ConfigurationFactory
and a CompositeConfiguration
. Additional sources of configuration parameters can be created by using custom configuration objects. This customization can be achieved by extending AbstractConfiguration
or AbstractFileConfiguration
.
The full Javadoc API documentation is available here.
3)org.apache.commons.beanutils.BeanUtils配置包
关键方法的解释和运用:
(1) public static void copyProperties(Object dest, Object orig):
Copy property values from the origin bean to the destination bean for all cases where the property names are the same.
eg:BeanUtils.copyProperties(user1,user2);
(2) public static void copyProperty(Object bean, String name, Object value):
Copy the specified property value to the specified destination bean, performing any type conversion that is required.
eg:BeanUtils.copyProperty(student,"stuName","leh"); //赋值给student实体stuName属性值为"leh";
(3) public static String getProperty(Object bean, String name):
Return the value of the specified property of the specified bean, no matter which property reference format is used, as a String.(返回带格式的属性值,也可以不带格式,不带格式相当于下面的getSimpleProperty()方法)
eg: String cityPattern = "addresses[0].city";
String city = (String) PropertyUtils.getProperty(customer, cityPattern);
//customer对象中有Address类型,后者有city属性。
(4) public static void setProperty(Object bean, String name, Object value):
(4) public static String getSimpleProperty(Object bean, String name):
eg:BeanUtils.getProperty(student,"stuName"); //得到student实体的stuName属性的值。stuName为student的基本类型;
(5) public static void populate(Object bean, Map properties):
Populate the JavaBeans properties of the specified bean, based on the specified name/value pairs.
eg:
Map userMap =hashMap();
userMap.put("stuName","leh");
……
BeanUtils.populate(student,userMap);//将userMap中的键的值赋值给同student中与map的键同名的属性。同名则复制,不同则不赋值。