设为首页 收藏本站
查看: 553|回复: 0

[经验分享] apache-commons 工具包学习

[复制链接]

尚未签到

发表于 2017-1-2 06:08:12 | 显示全部楼层 |阅读模式
  Commons是apache下属的开源子项目,专门提供一些通用的工具类。
  这些方法用起来后,程序看起来更加简单,专业。
  Commons的主要类结构:


Attributes


Runtime API to metadata attributes such as doclet tags.

BeanUtils


Easy-to-use wrappers around the Java reflection and introspection APIs.

Betwixt


Services for mapping JavaBeans to XML documents, and vice versa.

Chain


Chain of Responsibility pattern implemention.


CLI


Command Line arguments parser.

Codec


General encoding/decoding algorithms (for example phonetic, base64, URL).

Collections


Extends or augments the Java Collections Framework.

Compress


Defines an API for working with tar, zip and bzip2 files.

Configuration


Reading of configuration/preferences files in various formats.

Daemon


Alternative invocation mechanism for unix-daemon-like java code.

DBCP


Database connection pooling services.

DbUtils


JDBC helper library.

Digester


XML-to-Java-object mapping utility.

Discovery


Tools for locating resources by mapping service/reference names to resource names.

EL


Interpreter for the Expression Language defined by the JSP 2.0 specification.

Email


Library for sending e-mail from Java.

Exec


API for dealing with external process execution and environment management in Java.

FileUpload


File upload capability for your servlets and web applications.

IO


Collection of I/O utilities.

JCI


Java Compiler Interface

Jelly


XML based scripting and processing engine.

Jexl


Expression language which extends the Expression Language of the JSTL.

JXPath


Utilities for manipulating Java Beans using the XPath syntax.

Lang


Provides extra functionality for classes in java.lang.

Launcher


Cross platform Java application launcher.

Logging


Wrapper around a variety of logging API implementations.

Math


Lightweight, self-contained mathematics and statistics components.

Modeler


Mechanisms to create Model MBeans compatible with JMX specification.

Net


Collection of network utilities and protocol implementations.

Pool


Generic object pooling component.

Primitives


Smaller, faster and easier to work with types supporting Java primitive types.

Proxy


Library for creating dynamic proxies.

Sanselan


A pure-Java image library.

SCXML


An implementation of the State Chart XML specification aimed at creating and maintaining a Java SCXML engine. It is capable of executing a state machine defined using a SCXML document, and abstracts out the environment interfaces.

Transaction


Implementations for multi level locks, transactional collections and transactional file access.

Validator


Framework to define validators and validation rules in an xml file.

VFS


Virtual File System component for treating files, FTP, SMB, ZIP and such like as a single logical file system.
  



  Lang


  Lang
Provides extra functionality for classes in java.lang.
  org.apache.commons.lang.builder.
ToStringBuilder:

  提供object重写toString()的工具类.
  ApplicationToStringStyle.APP_TO_STRING_STYLE提供toString的style。
  Fruit中重写了toString方法。

public abstract class ApplicationToStringStyle extends ToStringStyle {
private static final long serialVersionUID = 1L;
public static final ApplicationToStringStyle APP_TO_STRING_STYLE = new CustomToStringStyle();
/**
* Constructor
*
* Use the static constant rather than instantiating
*/
private static final class CustomToStringStyle extends ApplicationToStringStyle {
private static final long serialVersionUID = -4289672936337739486L;
public CustomToStringStyle() {
super();
this.setUseClassName(true);
this.setUseIdentityHashCode(false);
this.setContentStart(SystemUtils.LINE_SEPARATOR + "[");
this.setFieldSeparator(SystemUtils.LINE_SEPARATOR + "  ");
this.setFieldSeparatorAtStart(true);
this.setContentEnd(SystemUtils.LINE_SEPARATOR + "]");
this.setUseShortClassName(true);
}
/**
* Override the append() method to skip fields that start with "_".  In this
* application, Axis generates some fields using this prefix that we don't want to include
* in the log files.  
*/
@Override
public void append(StringBuffer buffer, String fieldName, Object value, Boolean fullDetail) {
if (fieldName != null && !fieldName.startsWith("_")) {
super.append(buffer, fieldName, value, fullDetail);
}
}
/**
* Ensure Singleton after serialization
*
* @return the singleton
*/
private Object readResolve() {
return ApplicationToStringStyle.APP_TO_STRING_STYLE;
}
}   
}
 
public class Fruit {
private String name = "apple";
private String size = "3";
private String price = "$40";
public String toString() {
return ReflectionToStringBuilder.toString(this, ApplicationToStringStyle.APP_TO_STRING_STYLE);
}
}
  
 



  HttpClient


  The Commons HttpClient project used to be a part of Commons, but is now part of
Apache HttpComponents
-
see Jakarta Commons
HttpClient

  用于http有关的连接,包括ssl等。
  Notice that we go
through the entire process regardless of whether the server returned an error or
not. This is important because HTTP 1.1 allows multiple requests
to use the same
connection by simply sending the requests one after the other.

Obviously, if we
don't read the entire response to the first request, the left over data will get
in the way of the second response. HttpClient
tries to handle this but
to avoid problems it is important to always release the connection.
  基本http get访问


package commons.httpclient;
import java.io.IOException;
import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
public class Test {
public static void main(String[] args) throws HttpException, IOException {
//1. Create an instance of HttpClient.
HttpClient client = new HttpClient();
//2.
//Create an instance of one of the methods (GetMethod in this case).
//The URL to connect to is passed in to the the method constructor.
GetMethod method = new GetMethod("http://www.baidu.com/");
method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
new DefaultHttpMethodRetryHandler(3, false));
//3.Tell HttpClient to execute the method.
int statusCode = client.executeMethod(method);
if (statusCode != HttpStatus.SC_OK) {
System.err.println("Method failed: " + method.getStatusLine());
}
//4.Read the response.
String reponse = method.getResponseBodyAsString();
//5.Release the connection.
method.releaseConnection();
//6.Deal with the response.
System.out.println(reponse);
}
}

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-322469-1-1.html 上篇帖子: apache与django的整合 下篇帖子: apache转发 js 跨域
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表