日期:2014-05-16  浏览次数:20646 次

apache commons介绍
目前先转此文章,需要的时候再各个研究一下。



Apache Commons包含了很多开源的工具,用于解决平时编程经常会遇到的问题,减少重复劳动。我选了一些比较常用的项目做简单介绍。文中用了很多网上现成的东西,我只是做了一个汇总整理。

  一、Commons BeanUtils

  http://jakarta.apache.org/commons/beanutils/index.html

  说明:针对Bean的一个工具集。由于Bean往往是有一堆get和set组成,所以BeanUtils也是在此基础上进行一些包装。

  使用示例:功能有很多,网站上有详细介绍。一个比较常用的功能是Bean Copy,也就是copy bean的属性。如果做分层架构开发的话就会用到,比如从PO(Persistent Object)拷贝数据到VO(Value Object)。

  传统方法如下:

  //得到TeacherForm

  TeacherForm teacherForm=(TeacherForm)form;

  //构造Teacher对象

  Teacher teacher=new Teacher();

  //赋值
  teacher.setName(teacherForm.getName());
  teacher.setAge(teacherForm.getAge());
  teacher.setGender(teacherForm.getGender());
  teacher.setMajor(teacherForm.getMajor());
  teacher.setDepartment(teacherForm.getDepartment());

  //持久化Teacher对象到数据库
  HibernateDAO= ;
  HibernateDAO.save(teacher);

  使用BeanUtils后,代码就大大改观了,如下所示:

  //得到TeacherForm
  TeacherForm teacherForm=(TeacherForm)form;
  //构造Teacher对象
  Teacher teacher=new Teacher();

  //赋值
  BeanUtils.copyProperties(teacher,teacherForm);

  //持久化Teacher对象到数据库
  HibernateDAO= ;
  HibernateDAO.save(teacher);

  二、Commons CLI

  http://jakarta.apache.org/commons/cli/index.html

  说明:这是一个处理命令的工具。比如main方法输入的string[]需要解析。你可以预先定义好参数的规则,然后就可以调用CLI来解析。

  使用示例:

  // create Options object
  Options options = new Options();
  // add t option, option is the command parameter, false indicates that
  // this parameter is not required.

  options.addOption(“t”, false, “display current time”);
  options.addOption("c", true, "country code");

  CommandLineParser parser = new PosixParser();
  CommandLine cmd = parser.parse( options, args);

  if(cmd.hasOption("t")) {
  // print the date and time
  }else {
  // print the date
  }

  // get c option value
  String countryCode = cmd.getOptionValue("c");

  if(countryCode == null) {
  // print default date
  }else {
  // print date for country specified by countryCode
  }

  三、Commons Codec

  http://jakarta.apache.org/commons/codec/index.html

  说明:这个工具是用来编码和解码的,包括Base64,URL,Soundx等等。用这个工具的人应该很清楚这些,我就不多介绍了。

  四、Commons Collections

  http://jakarta.apache.org/commons/collections/

  说明:你可以把这个工具看成是java.util的扩展。

  使用示例:举一个简单的例子

  OrderedMap map = new LinkedMap();
  map.put("FIVE", "5");
  map.put("SIX", "6");
  map.put("SEVEN", "7");
  map.firstKey(); // returns "FIVE"
  map.nextKey("FIVE"); // returns "SIX"
  map.nextKey("SIX"); // returns "SEVEN"


  五、Commons Configuration

  http://jakarta.apache.org/commons/configuration/

  说明:这个工具是用来帮助处理配置文件的,支持很多种存储方式

  1. Properties files
  2. XML documents
  3. Property list files (.plist)
  4. JNDI
  5. JDBC Datasource
  6. System properties
  7. Applet parameters
  8. Servlet parameters

  使用示例:举一个Properties的简单例子

  # usergui.properties, definining the GUI,
  colors.background = #FFFFFF
  colors.foreground = #000080
  window.width = 500
  window.height = 300

  PropertiesConfiguration config = new PropertiesConfiguration("usergui.properties");
  config.setProperty("colors.background", "#000000);
  config.save();

  config.save("usergui.backup.properties);//save a copy
  Integer integer = config.getInteger("window.width");

  六、Commons DBCP

  http://jakarta.apache.org/commons/dbcp/

  例子:

  import java.sql.*;

  import com.gwnet.games.antiLord.uti