`
lighter
  • 浏览: 499705 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

妙用Commons良药 <五>

阅读更多
这一篇文章主要是讲一下关于Commons开源项目中其Commons BeanUtils的一些相关的用法.

1、复制Bean属性
如果你有两个相同的Bean的实例,并需要将其中的一个bean属性复制到另一个中去,调用PropertyUtils.copyProperties()即可,这一个方法经常在使用struts,hibernate等框架的时候常常用到.举例:
import org.apache.commons.beanutils.PropertyUtils;

Book book = new Book( );
book.setName( "Prelude to Foundation" );
book.setAuthorName( "Asimov" );
Book destinationBook = new Book( );
PropertyUtils.copyProperties( destinationBook, book );

请注意,该方法是将相同的引用对象赋给目的的bean,并没有克隆bean属性的值.


2、怎样使用Map来封装Bean
假如你需要将bean的属性传入Map
可使用BeanMap类封装任何Bean。该Map通过内省机制提供了对bean属性的访问,使得其看上去像是Map中成对的键与值.举例:
import java.util.*;
import org.apache.commons.beanutils.PropertyUtils;

// Create a Person and a Book bean instance
Person person = new Person( );
person.setName( "Some Dude" );

Book book = new Book( );
book.setName( "Some Silly Computer Book" );
book.setAuthor( person );

// Describe both beans with a Map
Map bookMap = PropertyUtils.describe( book );
Map authorMap = PropertyUtils.describe( bookMap.get("book") );
System.out.println( "Book Name: " + bookMap.get( "name" ) );
System.out.println( "Author Name: " + authorMap.get( "name" ) );

注意一点,如果你修改了BeanMap的时候,你也就修改了其内部的Bean.
还有常用的clear(),setBean(Object bean)等方法


3、可以根据Bean的属性来比较Beans
使用BeanComparator可根据bean属性来比较两个Bean.兴例如下:
import java.util.*;
import org.apache.commons.beanutils.BeanComparator;

Country country1 = new Country( );
country1.setName( "India" );
Country country2 = new Country( );
country2.setName( "Pakistan" );
Country country3 = new Country( );
country3.setName( "Afghanistan" );

// Create a List of Country objects
Country[] countryArray = new Country[] { country1, country2, country3 };
List countryList = Arrays.asList( countryArray );

// Sort countries by name
Comparator nameCompare = new BeanComparator( "name" );
Collections.sort( countryList, nameCompare );

System.out.println( "Sorted Countries:" );
Iterator countryIterator = countryList.iterator( );
while( countryIterator.hasNext( ) ) {
    Country country = (Country) countryIterator.next( );
    System.out.println( "\tCountry: " + country.getName( ) );
}

结果显示如下:
引用
Sorted Countries:
    Country: Afghanistan
    Country: India
    Country: Pakistan


注:文章中的代码均来之<<Jakarta  Commons Cookbook>>一书第三章
分享到:
评论
1 楼 kjj 2008-04-24  
还没看完,先赞一个

相关推荐

    Springboot集成Ueditor详细文档说明

    &lt;artifactId&gt;commons-fileupload&lt;/artifactId&gt; &lt;version&gt;1.3.2&lt;/version&gt; &lt;/dependency&gt; &lt;dependency&gt; &lt;groupId&gt;commons-io&lt;/groupId&gt; &lt;artifactId&gt;commons-io&lt;/artifactId&gt; &lt;version&gt;2.5&lt;/version&gt; &lt;/...

    Apache Commons 所有包最新版本 含SRC (6/7)

    commons-attributes-2.2-src.zip&lt;br&gt;commons-attributes-2.2.zip&lt;br&gt;commons-beanutils-1.8.0-BETA-src.zip&lt;br&gt;commons-beanutils-1.8.0-BETA.zip&lt;br&gt;commons-betwixt-0.8-src.zip&lt;br&gt;commons-betwixt-0.8.zip&lt;br&gt;...

    Apache Commons 所有包最新版本 含SRC (5/7)

    commons-attributes-2.2-src.zip&lt;br&gt;commons-attributes-2.2.zip&lt;br&gt;commons-beanutils-1.8.0-BETA-src.zip&lt;br&gt;commons-beanutils-1.8.0-BETA.zip&lt;br&gt;commons-betwixt-0.8-src.zip&lt;br&gt;commons-betwixt-0.8.zip&lt;br&gt;...

    Apache Commons 所有包最新版本 含SRC (1/7)

    commons-attributes-2.2-src.zip&lt;br&gt;commons-attributes-2.2.zip&lt;br&gt;commons-beanutils-1.8.0-BETA-src.zip&lt;br&gt;commons-beanutils-1.8.0-BETA.zip&lt;br&gt;commons-betwixt-0.8-src.zip&lt;br&gt;commons-betwixt-0.8.zip&lt;br&gt;...

    Apache Commons 所有包最新版本 含SRC (3/7)

    commons-attributes-2.2-src.zip&lt;br&gt;commons-attributes-2.2.zip&lt;br&gt;commons-beanutils-1.8.0-BETA-src.zip&lt;br&gt;commons-beanutils-1.8.0-BETA.zip&lt;br&gt;commons-betwixt-0.8-src.zip&lt;br&gt;commons-betwixt-0.8.zip&lt;br&gt;...

    Apache Commons 所有包最新版本 含SRC (7/7)

    commons-attributes-2.2-src.zip&lt;br&gt;commons-attributes-2.2.zip&lt;br&gt;commons-beanutils-1.8.0-BETA-src.zip&lt;br&gt;commons-beanutils-1.8.0-BETA.zip&lt;br&gt;commons-betwixt-0.8-src.zip&lt;br&gt;commons-betwixt-0.8.zip&lt;br&gt;...

    aspose-words-15.8.0-jdk16.jar.rar

    添加依赖: &lt;dependency&gt; &lt;groupId&gt;com.itextpdf&lt;/groupId&gt; &lt;artifactId&gt;itext7-core... &lt;groupId&gt;org.apache.commons&lt;/groupId&gt; &lt;artifactId&gt;commons-io&lt;/artifactId&gt; &lt;version&gt;1.3.2&lt;/version&gt; &lt;/dependency&gt;

    Apache Commons 所有包最新版本 含SRC (4/7)

    commons-attributes-2.2-src.zip&lt;br&gt;commons-attributes-2.2.zip&lt;br&gt;commons-beanutils-1.8.0-BETA-src.zip&lt;br&gt;commons-beanutils-1.8.0-BETA.zip&lt;br&gt;commons-betwixt-0.8-src.zip&lt;br&gt;commons-betwixt-0.8.zip&lt;br&gt;...

    Apache Commons 所有包最新版本 含SRC (2/7)

    commons-attributes-2.2-src.zip&lt;br&gt;commons-attributes-2.2.zip&lt;br&gt;commons-beanutils-1.8.0-BETA-src.zip&lt;br&gt;commons-beanutils-1.8.0-BETA.zip&lt;br&gt;commons-betwixt-0.8-src.zip&lt;br&gt;commons-betwixt-0.8.zip&lt;br&gt;...

    client发送短信验证码(云之讯)

    &lt;artifactId&gt;commons-httpclient&lt;/artifactId&gt; &lt;version&gt;3.0&lt;/version&gt; &lt;/dependency&gt; &lt;dependency&gt; &lt;groupId&gt;commons-lang&lt;/groupId&gt; &lt;artifactId&gt;commons-lang&lt;/artifactId&gt; &lt;version&gt;2.6&lt;/version&gt; &lt;/...

    基于springboot演示resuful api、mock请求、validate验证、异常捕捉、aop切面编程+源代码+文档说

    &lt;version&gt;${commons-lang3.version}&lt;/version&gt; &lt;/dependency&gt; &lt;dependency&gt; &lt;groupId&gt;org.projectlombok&lt;/groupId&gt; &lt;artifactId&gt;lombok&lt;/artifactId&gt; &lt;/dependency&gt; &lt;dependency&gt; &lt;groupId&gt;org.spring...

    java实现poi 在线预览,excel,word直接在页面显示,附带文件上传,多文件上传

    &lt;artifactId&gt;commons-collections4&lt;/artifactId&gt; &lt;version&gt;4.1&lt;/version&gt; &lt;/dependency&gt; &lt;dependency&gt; &lt;groupId&gt;org.apache.poi&lt;/groupId&gt; &lt;artifactId&gt;poi&lt;/artifactId&gt; &lt;version&gt;3.17&lt;/version&gt; &lt;/...

    struts框架jar包

    struts-1.3.8 包(&lt;br&gt;&lt;br&gt;antlr-2.7.2.jar&lt;br&gt;bsf-2.3.0.jar&lt;br&gt;commons-beanutils-1.7.0.jar&lt;br&gt;commons-chain-1.1.jar&lt;br&gt;commons-digester-1.8.jar&lt;br&gt;commons-fileupload-1.1.1.jar&lt;br&gt;commons-io-1.1.jar&lt;br&gt;...

    Java 课程设计 高校公寓管理系统

    &lt;artifactId&gt;standard&lt;/artifactId&gt; &lt;version&gt;1.1.2&lt;/version&gt; &lt;artifactId&gt;jstl&lt;/artifactId&gt; ... &lt;artifactId&gt;commons-io&lt;/artifactId&gt; &lt;version&gt;2.11.0&lt;/version&gt; &lt;artifactId&gt;javax.servlet-api&lt;/artifactId&gt;

    Java 登录注册案例实例

    &lt;artifactId&gt;commons-io&lt;/artifactId&gt; &lt;version&gt;2.11.0&lt;/version&gt; &lt;artifactId&gt;mybatis&lt;/artifactId&gt; &lt;version&gt;3.5.5&lt;/version&gt; &lt;artifactId&gt;mysql-connector-java&lt;/artifactId&gt; &lt;version&gt;8.0.28&lt;/version&gt; ...

    JSF与Shale开发用包

    其中包含:shale-core.jar&lt;br&gt;commons-beanutils.jar&lt;br&gt;commons-chain.jar&lt;br&gt;commons-codec.jar&lt;br&gt;commons-collections.jar&lt;br&gt;commons-digester.jar&lt;br&gt;commons-el.jar&lt;br&gt;commons-fileupload.jar&lt;br&gt;commons-...

    springcloud+springcloud alibaba完整学习项目

    &lt;module&gt;cloud-api-commons&lt;/module&gt; &lt;module&gt;cloud-eureka-server7001&lt;/module&gt; &lt;module&gt;cloud-eureka-server7002&lt;/module&gt; &lt;module&gt;cloud-comsumer-order80&lt;/module&gt; &lt;module&gt;cloud-provider-payment8001&lt;/...

    spring-hibernate-dwr实例

    collections-2.1.1.jar&lt;br&gt;commons-logging-1.0.4.jar&lt;br&gt;dom4j-1.6.1.jar&lt;br&gt;ehcache-1.1.jar&lt;br&gt;hibernate3.jar&lt;br&gt;jaas.jar&lt;br&gt;jaxen-1.1-beta-7.jar&lt;br&gt;jdbc2_0-stdext.jar&lt;br&gt;jta.jar&lt;br&gt;log4j-1.2.11.jar&lt;br&gt;...

    spring+struts+hibernate+dwr+jstl做的实例

    api.jar&lt;br&gt;jxl.jar&lt;br&gt;itext-1.3.jar&lt;br&gt;poi-2.5.1.jar&lt;br&gt;cos.jar&lt;br&gt;velocity-1.5.jar&lt;br&gt;velocity-tools-view-1.3.jar&lt;br&gt;commons-codec.jar&lt;br&gt;antlr.jar&lt;br&gt;commons-beanutils.jar&lt;br&gt;commons-digester.jar&lt;br...

    ssm的jar包

    ssm在maven项目管理下的pom文件,jar包依赖 &lt;dependency&gt; &lt;groupId&gt;javax.servlet&lt;/groupId&gt; &lt;artifactId&gt;javax.servlet-api&lt;/artifactId&gt; ... &lt;artifactId&gt;commons-codec&lt;/artifactId&gt; &lt;version&gt;1.10&lt;/ver

Global site tag (gtag.js) - Google Analytics