In the first article of this series, we divide the components contained in the Commons project into five categories, introduced the web class and other classes. The second article Theory and XML class and packaging classes. This is the last one to explore the components of the tool class. Note that Commons itself does not perform this classification, and the classification is purely description and organization convenience. The tool class contains several components of BeanUtils, Logging, DBCP, Pool, and Validator. I. Beanutils ■ Overview: Tools for dynamic operation JavaBean. ■ Official resources: home page, binary, source code. ■ When applies: When you need to dynamically access JavaBean, you don't know what to compile. Accessor and Modifier. The dynamically accessed JavaBean must comply with the naming design specification defined by JavaBeans Specification. ■ Sample application: beanutilsdemo.java, applayer1bean.java, applayer2bean.java, subbean.java. The CLASSPATH must be included in COMMONS-beanUtils.jar, Commons-Logging.jar, and Commons-Collectes.jar. ■ Description: In a dynamic Java application design environment, we do not necessarily understand the various sets, GET methods of JavaBean. Even if the name of these methods is already known, write the setxxx or getxxx method for each attribute of Bean. Consider this situation: Several almost identical beans are passed from a layer of the application to another layer, will you call bean1.setxxx (bean2.getxxx ()) for each property? Although you can do this, it is not always doing this, because you can let Beanutils complete these cumbersome operations! Beanutils can help developers dynamically create, modify, and copy JavaBeans. BeanUtils can operate JavaBean: (1) of the following conditions: JavaBean must provide a constructor without parameters. (2) JavaBean's properties must be able to access and modify through Getxxx and Setxxx methods. ISXXX and SETXXX are also allowed for Boolean properties. JavaBean's properties can be read-only or only written, that is, allow only SET or GET methods for properties. (3) If there is no traditional naming method (ie, using GET and SET), you must use other ways to name JavaBean's Accessor and Modifier, then this must be declared by the BeanInfo class associated with JavaBean. Let's take a simple example. To get and set the simple properties of JavaBean, use PropertyUtils. GetSimpleProperty (Object Bean, String Name), and PropertyUtils. SetsImpleProperty (Object Bean, String Name, Object Value) method. As shown in the example below, the applayer1bean.java and applayer2bean.java define two JavaBeans for two tests.
PropertyUtils.SetSimpleProperty (App1bean, "INTPROP1",
NEW INTEGER (10));
System.err.Println ("App1Layerbean, Stringprop1:"
PropertyUtils.getsimpleproperty (app1bean,
"StringProp1")); Since we can get the properties of the bean by directly calling the bean method (App1Bean.getstringProp1 () or app1bean.setintprop1 (10)), why should I use setsimpleproperty, GetSimpleProperty method? This is because we don't necessarily know the name of the JavaBean property, so it is not necessarily known which methods to call to get / set the corresponding properties. The names of these attributes may come from other processs or external applications. Therefore, once the name of the JavaBean's attribute is made and saved it to a variable, you can pass the variable to PropertyUtils, no need to rely on other developers to pre-know the correct method name. So, if the attribute of JavaBean is not a simple data type, what should I do? For example, the attribute of JavaBean may be a collection or a MAP. In this case, we have to change PropertyUtils.GetIndexedProperty or PropertyUtils.getMappedProperty. For the collection class attribute value, we must specify an index value, specify the value of the value to be extracted or set in the collection; for the MAP class attribute, we must specify a key to indicate which value to extract. Here are two examples: PropertyUtils.setIndexedProperty
App1bean, "ListProp1 [1]", "New Strings 1");
System.err.println ("App1Layerbean, ListProp1 [1]:"
PropertyUtils.GetIndexedProperty (app1bean,
"ListProp1 [1]")); Please note that the index value is passed through square brackets for the indexed properties. For example, in the above example, we set the value of the index of javabean (app1bean) to a "new string 1", which takes the same value from the location of the index 1. There may be another way to achieve the same objective, namely the use of PropertyUtils.setIndexedProperty (Object bean, String name, int index, Object value) and PropertyUtils.getIndexedProperty (Object bean, String name, int index) method, the two Method in index value is passed as a method of parameter. For MAP class properties, there are similar methods, as long as the key (rather than index) is changed, or set the specified value. Finally, the property of the bean may be a bean. So how do you get or set those properties beans from the primary bean? Just use PropertyUtils.getnestedProperty (Object Bean, String Name) and Property (Object Bean, String Name, Object Value) method. An example is provided below.
// Access and set the nested properties
PropertyUtils.setnestedProperty
App1bean, "Subbean.StringProp",
"Information from Subbean, setting with setnestedProperty.");
System.err.println (PropertyUtils.getnestedProperty (app1bean,
"Subbean.StringProp")))))))))))))))))))) The above can be seen that the attribute of the dependent bean is accessed through a period symbol. The above ways described above can be used together, and the nesting depth is not limited. The two methods to be used are PropertyUtils.getProperty (Object Bean, String Name) and PropertyUtils.SetProperty (Object Bean, String Name, Object Value). For example: PropertyUtils.SetProperty (App1bean, "Subbean.listProp [0]", "Value of the Attribute") ;. This example is to access the nested bean objects and the canotic properties. BeanUTILS is often used to dynamically access request parameters in web applications. In fact, it is the inspiration of beanutils to trigger the Struts project to dynamically convert the request parameters into system javabean: Use the code to convert the user fill in the form into a map, where the name of the parameter becomes the key in the map, from the value of the parameters The data entered in the form is entered in the form, then converting these values into a system bean by a simple beanutils.populate call. Finally, BeanUtils provides a step from a step to another bean from a bean to another.
/ / Copy the data of app1bean to App2Bean
Beanutils.copyProperties (App2bean, App1bean); BeanUtils also has a practical method that has not been mentioned here. But don't worry, beanutils is one of the comprehensive components of documentation in Commons. It is recommended that the reader will read the relevant information of the remaining methods for the Javadoc documentation of the BeanUtils package. Second, Logging ■ Overview: A codebook that encapsulates many popular log tools and provides a unified log access interface. ■ Official resources: home page, binary, source code. ■ When applies: When your application requires more than one log tool, or when it is expected to have this requirement. ■ Example Application: LoggingDemo.java, Commons-logging.properties. It is required to include commons-logging.jar in ClassPath, sometimes log4j.jar. ■ Description: Logging enables us to debug and track the behavior and status of any time at any time. In any larger application, Logging is an integral part, so there are many third-party logging tools, which exempt from developers to write the Logging API. In fact, even if JDK has a structured Logging API. Since there is already so much choice (log4j, jdk, logkit, etc.), usually we can always find ready-to-developed APIs that are best suited for their application requirements. However, there may be an exceptional situation, such as a familiar Logging API that cannot be compatible with the current application, or because of some kind of hard regulations, or due to the application of the architecture. The case of the Commons project Logging component is to package the functionality of the log log into a set of standard APIs, but its underlying implementation can be arbitrarily modified and transformed. Developers use this API to perform commands of log information, by the API to deliver these commands to the appropriate underlying handle. Therefore, for developers, the Logging component is neutralized for any specific underlying implementation. If you are familiar with Log4j, you should not have any problems with the logging API using your COMMONS. Even if you are not familiar with log4j, just know that using Logging must import two classes, create a log's static instance, below shows the code for this section: import org.apache.commons.logging.log;
Import org.apache.commons.logging.logfactory;
Public class loggingdemo {
Private static log log = logfactory.getlog
LoggingdeMo.class;
// ...
} It is necessary to explain what happened when calling logfactory.getlog (). Calling this function will start a discovery process, that is, identify the implementation of the required underlying logging function, and the specific discovery process is listed below. Note that no matter how the underlying log tool is found, it must be a class that implements the log interface, and must be in the classpath. Commons Logging API provides support for the following underlying logging tools: JDK14Logger, Log4jlogger, LogkitLogger, NOOPLOGGER (directly discarding all log information), there is a SimpleLog. (1) Commons Logging first looks for a commons-logging.properties file in ClassPath. This attribute file must define the org.apache.commons.logging.log property, which should be the full qualified name implemented by any of the above log interfaces. (2) If the above step fails, Commons's logging then checks the system properties org.apache.commons.logging.log. (3) If you can't find the org.apache.commons.logging.log system properties, Logging then looks for the Log4J class in ClassPath. If found, Logging assumes that the application is log4j. However, the property of the log4j itself is still configured by log4j.properties file. ⑷ If the above lookups cannot find the appropriate logging API, but the application is running on JRE 1.4 or later, the JRE 1.4 logging feature is used by default. ⑸ Finally, if the above operation fails, the application will use the built-in SimpleLog. Simplelog outputs all log information directly to System.err. After obtaining an appropriate underlying logging tool, you can start the log information. As a standard API, the main benefit of the Commons Logging API is to establish an abstraction layer on the basis of the underlying log mechanism, convert the call to the log record command associated with the specific implementation by the abstraction layer. The sample program provided herein will output a prompt message telling you which underlying log tool is currently using. Try running this program in different environmental configurations, for example, running this program without specifying any properties, then JDK14Logger will be used by default; then specify system properties-Jorg.Apache.commons.logging.log = Org .apache.commons. logging.impla.SIMPLOG Runner, then the logging tool will be simplelog; finally, put the log4j class into the classpath, just set the log4j log4j.properties configuration file, you can get the log4jlogger output Information. LOG illustrates this. Struts is used by Jakarta Commons logging packages. It uses the priority is: log4j (4 read FOUR seems to be more meaningful, probably the meaning of logger for java, I listen to the year log si j, I feel very awkward, Haha), Java 1.4 logging api, Simple logging. The function is in turn weakened.
It is recommended to use Commons logging to use Commons logging in the business method called by Execute (), or you want to remove the thousand thousand system.out.println () You will feel that Commons logging is a good stuff. Its usage is: import org.apache.commons.logging.log; import org.apache.commons.logging.logfactory; private / protected static log log = logfactory.getlog (dispatchaction.class); if you use Dispatchction, Then you don't define the instance of the log, because it already has a protected Log instance, which can be used directly. How to use is: if (log.isinfoenabled ()) {log.info ("Some Information.");} Logging divides the message into 6 levels, debug, error, fatal, info, trace, warn. For example, you want to record a message, it is just to give the user a warning, you can use Warn. Why do you have a judgment before each log.info ()? Is it possible if INFO is not allowed if the LOG level is allowed? of course not. Its role is to improve efficiency. Third, POOL ■ Overview: The code library used to manage the object pool. ■ Official resources: home page, binary, source code. ■ When applies: When you need to manage an object instance pool. ■ Sample application: Poolomo.java and MyObjectFactory.java. There must be commons-pool.jar and commons-collections.jar in ClassPath.
■ Description: The pool component defines a set of interfaces for the object pool, and also provides several universal object pool implementations, as well as some fundamental classes that help developers create objects. For most developers, the object pool should not be a new concept. Perhaps many readers have used the database connection pool when accessing the database, and the concept of the object pool is actually similar. The object pool allows developers to create a set of objects in the buffer (create an object's operation can be done by the application's configuration file, or can be completed at the startup phase of the application), can be obtained quickly when the application needs to be used Respond. If the application no longer needs an object, it still returns an object to the buffer pool, and then extract from the buffer pool when you need to use the object next time. The pool component allows us to create an object (instance) pool, but do not limit we must use a specific implementation. The pool component itself provides several implementations, and we can also create your own implementation if necessary. The pool component contains three basic classes: ObjectPool, this is an interface for defining and maintaining the object pool; ObjectPoolFactory, is responsible for creating an instance of ObjectPool; there is a PoolableObjectFacotry, which defines a group of lives for the instances used within ObjectPool Cycle method. As indicated earlier, the Pool component contains several general implementations, one of which is genericObjectPool, below to see its usage by one instance. 1 Create a pooLableObjectFactory. This factory class definition object is created, dismantled, and verified. Import org.apache.commons.pool.poolableObjectFactory;
Public Class MyObjectFactory Implements
POOLABLEOBJECTFACTORY {
Private static int counter
/ / Return a new string
Public Object makeObject () {
Return String.Valueof (Counter );
}
Public void destroyObject (Object obj) {}
Public Boolean ValidateObject (Object Obj)
{Return True;}
Public void ActivateObject (Object Obj) {}
Public void passivateObject (Object obj) {}
} This example creates a pool of the String object of the serial number, and the verification operation (ValidateObject) always returns True. 2 Create a genericObjectPool, MaxActive, MaxIdle and other options using PoolableObjectFactory.
GenericObjectPool Pool = New GenericObjectPool
(New myObjectFactory ()); 3 from the object pool "Borrow" an object.
System.err.println ("Borrowed:" pool.borrowObject ()); 4 returns the object to the object pool.
Pool.ReturnObject ("0"); the status of the object pool can be known in a variety of methods, for example:
/ / How many objects have been activated (already borrowed)?
System.err.Println ("The number of objects of current activities:"
pool.getnumactive ()); PoolDemo.java provided later later provides a complete source code. Fourth, DBCP ■ Overview: Database connection pool. Based on the POOL component. ■ Official resources: home page, binary, source code. ■ When applies: When you need to access the relational database. ■ Sample application: dbcpdemo.java. Requires CLASSPATH must have commons-dbcp.jar, commons-pool.jar, and commons-collections.jar. Also be able to access the database, configure the JDBC driver suitable for the database. The sample application test is a MySQL database connection, the driver is MySQL JDBC Driver. Note that running this program requires the Nightly version of the binary file, and the current formal distribution lacks some essential classes. Finally, when running this sample program, you should ensure that the system properties have been set for the JDBC driver (-djdbc.drivers = com.mysql.jdbc.driver). ■ Description: On the basis of the POOL component, DBCP provides a database connection buffer pool mechanism. Compared to conventional connection pools, DBCP is a bit more complicated because its idea provides a universal system in the form of a pseudo JDBC driver. However, we have learned the basic knowledge of the Pool component, and now I understand that DBCP usage should be very simple. // ...
// 1 Create an instance of a GenericObjectPool class.
GenericObjectPool pool = new genericObjectPool (null);
// ...
// 2 Receive genericObjectPool when discussing the pool components in front
// Requirement There is a poolaableObjectFactory to create a need.
// The instance of the Object to buffer, for DBCP,
// This feature is now raised by PoolableConnectionFactory
// For supply, as shown in the following example:
DrivermanagerConnectionFactory CF =
New DriverManagerConnectionFactory (
"JDBC: MySQL: // Host / DB", "UserName", "Password");
POOLABLECONNECTIONFACTORY PCF =
New poolableconnectionfactory
CF, Pool, NULL, "Select * from mysql.db", false, true);
// ...
// 3 Now, we only need to create and register PoolingDriver:
New poolingdriver (). RegisterPool ("MyPool", Pool; Next, you can pick it up from this connection pool. Note that the default value of MaxActive, MaxIdle and other options when creating this connection pool, if necessary, you can customize these values when you create an instance of the GenericObjectPool class in front step 1. DBCPDemo.java provides a complete instance. V. Validator ■ Overview: A API that collects common user input verification functions. ■ Official resources: home page, binary, source code. ■ When applies: When JavaBean performs a regular verification operation. ■ Example App: ValidatorDemo.java, MyValidator.java, MyFORMBEAN.JAVA, VALIDATION.XML. Requires ClassPath must have commons-validator.jar, commons-beanutils.jar, commons-collections.jar, common-digester.jar, and commons-logging.jar. ■ Description: If you have developed a web application with Struts, you should have used the Validator package. The Validator package is extremely simple to simplify the user input data. However, Validator is not limited to web applications, which can also be easily used in other applications that use JavaBean. Validator allows the user input domain to define validation conditions, support error information internationalization, allowing a custom verifier, and the Validator package also provides some predefined validiarons. Verify rules and verification methods are defined with an XML file (you can define one or more XML files, but usually, it is well separated). The verification method file defines the validator to be used, specifying the Java classes that actually implement the validator (not required to implement certain specific interfaces, nor require these classes to be derived from specific classes, only need to define the method The definition of the declaration in the file is OK). Let's construct a custom verifier, its function is to check if a String property of the bean contains a specific character ("*"). Import org.apache.commons.validator. *;
Public class myvalidator {
Public Static Boolean ValidateContainSchar (
Object bean, field field) {
// First get the properties of the bean (ie a string value)
String Val = ValidatorUtil.getValueAsstring
Bean, Field.getProperty ());
/ / Returns true or false based on whether or not to contain "*" characters in the attribute.
Return ((Val.IndexOf ('*') == -1)? false: true);
}
} The ValidatorUtil class provides many utilities, such as ValidatorUtil.getValueAsstring to extract the properties of the bean and return a string. Now we have to declare the MyValidator verifier in the XML file.
ClassName = "MyValidator" method = "validateContainschar" Methodparams = "java.lang.object, Org.apache.commons.validator.field "/> global> It can be seen that the XML file details the feature of the verification method, including the input parameters of the method. Let's take a look at the steps using this verifier. 1 Join the verification rules we want to implement in the XML file above.
Containsstar Test ->
field>
form>
Formset> It can be seen that all verification rules declare within the Formset element. Formset elements are first declare that the form to verify, and the input domains to verify are listed within the form and their verification conditions. In this example, we want to verify the MyFormBean's Name property, check whether the attribute is able to verify through the ContainsStar (that is, whether the value of the Name property contains "*" characters). 2 Based on the XML file, create a validator instance and initialize.
// Load the verifier XML file
InputStream in = getClass (). GetresourceAsStream
("Validator.xml");
// Create a ValidatorResources
ValidatorResources Resources = New ValidatorResources ();
// Initialize the validator resources
ValidatorResourcesInitializer.Initialize (resources, in);
// Create Validator
Validator Validator = New Validator
(Resources, "MyFORMBEAN");
Validator.AddResource (Validator.bean_Key, Bean); 3 Verify Bean. The result of the verification is a ValidatorResults, which contains the results of each required verification to perform verification results according to their respective validation conditions.
// Perform verification
ValidatorResults Results = Validator.Validate (); 4 Processing ValidationResults.
// Verify the result Object ValidationResults may also contain the results of verifying other forms,
// We can extract their verification results separately for each property.
ValidatorResult Result = Results.getValidatorReSult
("Name");
// For each property, we can check the results of each verification condition separately.
//, for example, is the NAME attribute passed the Containsstar verification?
System.err.println
"Name property contains" * "characters test results:"
Result.issalid ("ContainsStar"); for each ValidationResult, we can query whether it passes a specific check. For example, in the above code, we use the Result.issalid ('ContainStart') expression to check the ValidatorResult instance of the Name property, see if it passes Containsstar verification. For web applications, Validator is a quite useful component that provides a set of predefined verifiers, greatly facilitating user input legality verification. A predefined verifier can be used (but not limited to) to check the range, data type, length, and email address and geographic location check. In addition, we can define the validator yourself and add it into the Validator framework. Conclusion: The third article (also the last article) Introduction to Jakarta Commons will end here. Although this series of articles only involve the basics of each component, I hope that they are already enough to start the next step in depth study. Please download this document from here: jakartacommons3code.zip. Relevant information
General Validation System - Commons-Validator 2003-11-12 Jakarta Commons: Claus Class and Components 2 2003-10-10 Jakarta Commons: Use Class and Components 1 2003-10-10