Introduction to the use of Apache Commons toolset

Source: Internet
Author: User

Http://developer.51cto.com/art/201511/496849.htm


The Apache Commons contains a number of open source tools for solving problems that are often encountered in programming and reducing duplication of effort. I chose some more commonly used items to do a brief introduction. The article used a lot of online ready-made things, I just did a summary of the finishing.

First, Commons beanutils

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

Description: A tool set for the bean. Since beans tend to have a bunch of get and set compositions, Beanutils also makes some packaging on this basis.

Use examples: There are many features, detailed on the Web site. A more common feature is bean copy, which is the property of the copy bean. It would be used if you were developing a layered architecture, such as copying data from PO (persistent object) to Vo (Value object).

The traditional methods are as follows:

Get Teacherform teacherform teacherform= (teacherform) Form;    Construct Teacher Object Teacher teacher=new Teacher ();  Assigned Value Teacher.setname (Teacherform.getname ());  Teacher.setage (Teacherform.getage ());  Teacher.setgender (Teacherform.getgender ());  Teacher.setmajor (Teacherform.getmajor ());    Teacher.setdepartment (Teacherform.getdepartment ());  Persisting teacher object to database hibernatedao=; Hibernatedao.save (teacher);

With Beanutils, the code has changed dramatically, as follows:

Get Teacherform teacherform teacherform= (teacherform) Form;    Construct Teacher Object Teacher teacher=new Teacher ();    The assigned value beanutils.copyproperties (teacher,teacherform);  Persisting teacher object to database hibernatedao=; Hibernatedao.save (teacher);

Ii. Commons CLI

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

Description: This is a tool for processing commands. For example, the main method input string[] needs parsing. You can define the rules of the parameters in advance, and then you can invoke the CLI to resolve them.

Use examples:

 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 } 

Third, Commons Codec

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

Description: This tool is used to encode and decode, including Base64,url,soundx and so on. People with this tool should be very clear about this, I will not introduce more.

Iv. Commons Collections

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

Description: You can think of this tool as a java.util extension.

Use example: To give a simple example

Orderedmap map = new Linkedmap ();  Map.put ("FIVE", "5");  Map.put ("SIX", "6"); Map.put ("SEVEN", "7");

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.