这一篇文章主要是讲一下关于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>>一书第三章
分享到:
相关推荐
<artifactId>commons-fileupload</artifactId> <version>1.3.2</version> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.5</version> </...
commons-attributes-2.2-src.zip<br>commons-attributes-2.2.zip<br>commons-beanutils-1.8.0-BETA-src.zip<br>commons-beanutils-1.8.0-BETA.zip<br>commons-betwixt-0.8-src.zip<br>commons-betwixt-0.8.zip<br>...
commons-attributes-2.2-src.zip<br>commons-attributes-2.2.zip<br>commons-beanutils-1.8.0-BETA-src.zip<br>commons-beanutils-1.8.0-BETA.zip<br>commons-betwixt-0.8-src.zip<br>commons-betwixt-0.8.zip<br>...
commons-attributes-2.2-src.zip<br>commons-attributes-2.2.zip<br>commons-beanutils-1.8.0-BETA-src.zip<br>commons-beanutils-1.8.0-BETA.zip<br>commons-betwixt-0.8-src.zip<br>commons-betwixt-0.8.zip<br>...
commons-attributes-2.2-src.zip<br>commons-attributes-2.2.zip<br>commons-beanutils-1.8.0-BETA-src.zip<br>commons-beanutils-1.8.0-BETA.zip<br>commons-betwixt-0.8-src.zip<br>commons-betwixt-0.8.zip<br>...
commons-attributes-2.2-src.zip<br>commons-attributes-2.2.zip<br>commons-beanutils-1.8.0-BETA-src.zip<br>commons-beanutils-1.8.0-BETA.zip<br>commons-betwixt-0.8-src.zip<br>commons-betwixt-0.8.zip<br>...
添加依赖: <dependency> <groupId>com.itextpdf</groupId> <artifactId>itext7-core... <groupId>org.apache.commons</groupId> <artifactId>commons-io</artifactId> <version>1.3.2</version> </dependency>
commons-attributes-2.2-src.zip<br>commons-attributes-2.2.zip<br>commons-beanutils-1.8.0-BETA-src.zip<br>commons-beanutils-1.8.0-BETA.zip<br>commons-betwixt-0.8-src.zip<br>commons-betwixt-0.8.zip<br>...
commons-attributes-2.2-src.zip<br>commons-attributes-2.2.zip<br>commons-beanutils-1.8.0-BETA-src.zip<br>commons-beanutils-1.8.0-BETA.zip<br>commons-betwixt-0.8-src.zip<br>commons-betwixt-0.8.zip<br>...
<artifactId>commons-httpclient</artifactId> <version>3.0</version> </dependency> <dependency> <groupId>commons-lang</groupId> <artifactId>commons-lang</artifactId> <version>2.6</version> </...
<version>${commons-lang3.version}</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> <dependency> <groupId>org.spring...
<artifactId>commons-collections4</artifactId> <version>4.1</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.17</version> </...
struts-1.3.8 包(<br><br>antlr-2.7.2.jar<br>bsf-2.3.0.jar<br>commons-beanutils-1.7.0.jar<br>commons-chain-1.1.jar<br>commons-digester-1.8.jar<br>commons-fileupload-1.1.1.jar<br>commons-io-1.1.jar<br>...
<artifactId>standard</artifactId> <version>1.1.2</version> <artifactId>jstl</artifactId> ... <artifactId>commons-io</artifactId> <version>2.11.0</version> <artifactId>javax.servlet-api</artifactId>
<artifactId>commons-io</artifactId> <version>2.11.0</version> <artifactId>mybatis</artifactId> <version>3.5.5</version> <artifactId>mysql-connector-java</artifactId> <version>8.0.28</version> ...
其中包含:shale-core.jar<br>commons-beanutils.jar<br>commons-chain.jar<br>commons-codec.jar<br>commons-collections.jar<br>commons-digester.jar<br>commons-el.jar<br>commons-fileupload.jar<br>commons-...
<module>cloud-api-commons</module> <module>cloud-eureka-server7001</module> <module>cloud-eureka-server7002</module> <module>cloud-comsumer-order80</module> <module>cloud-provider-payment8001</...
collections-2.1.1.jar<br>commons-logging-1.0.4.jar<br>dom4j-1.6.1.jar<br>ehcache-1.1.jar<br>hibernate3.jar<br>jaas.jar<br>jaxen-1.1-beta-7.jar<br>jdbc2_0-stdext.jar<br>jta.jar<br>log4j-1.2.11.jar<br>...
api.jar<br>jxl.jar<br>itext-1.3.jar<br>poi-2.5.1.jar<br>cos.jar<br>velocity-1.5.jar<br>velocity-tools-view-1.3.jar<br>commons-codec.jar<br>antlr.jar<br>commons-beanutils.jar<br>commons-digester.jar<br...
ssm在maven项目管理下的pom文件,jar包依赖 <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> ... <artifactId>commons-codec</artifactId> <version>1.10</ver