About one of Apache Commons configuration

Source: Internet
Author: User
Tags configuration settings

1 Introduction

Commons Configuration The Software class library provides a common configuration interface that enables Java the application reads the configuration file from a variety of sources. the Commons Configuration provides a simple type of access and a multi-semantic config parameter demonstrated by the following code:

Double double = config.getdouble ("number");

Integer integer = Config.getinteger ("number");

Configuration parameters can be loaded from the following sources:

    • Properties File

    • XML Document

    • Property list File

    • Jndi

    • JDBC DataSource

    • System Configuration

    • Applet Parameters

    • Servlet Parameters

The configuration object is created using the config build. Different configuration sources can be used in combination with Combinedconfigurationbuilder and combinedconfiguration . Additional configuration parameter sources can be created using custom configuration objects that extend Abstractconfiguration or abstracthierarchicalconfiguration.

2 Using a Configuration object

Commons Config allows you to access configuration properties from a variety of different sources. Whether they are stored in a properties file,an XML document, or a JNDI tree, they can be accessed in the same way as a generic Configuration interface.

other advantages of the Commons configuration are the ability to mix heterogeneous provisioning sources and handle them like simple logical configuration. This article describes the different configurations and how to use them together.

2.1 Configuration Source

There are currently a large number different sources for the Configuration object. However, only the Configuration object and a specific type, such as xmlconfiguration or jndiconfiguration, are used. You avoid the mechanism of actually retrieving configuration values. These sources include:

  • Propertiesconfiguration load configuration values from the properties file.

  • Xmlconfiguration gets The value from the XML document.

  • Iniconfiguration load values from the Windows. ini file.

  • Propertylistconfiguration load value from openstep. plist file. xmlpropertylistconfiguration can also read the XML variants that Mac OS X uses .

  • Jndiconfiguration uses the keys in the JNDI tree to retrieve the configuration property values.

  • Baseconfiguration The method that fills the Configuration object in memory.

  • Hierarchicalconfiguration a configuration object that can handle complex structural data in memory .

  • SystemConfiguration uses the configuration of System properties.

  • Configurationconverter need a java.util.Properties or a Org.apache.commons.collections.ExtendedProperties and converts it to a Configuration object.

2.2 Hybrid Configuration Source

typically you want to provide a set of basic configuration values, but allow users to easily overwrite them in a specific environment. One way is to hardcode the default value into your code, but provide a property file to overwrite it. However, this is a very rigid matter. With compositeconfiguration You can provide many different ways to set up the configuration. You can do it manually:

compositeconfiguration config = new compositeconfiguration ();
Config.addconfiguration (New SystemConfiguration ());
Config.addconfiguration (New Propertiesconfiguration ("Application.properties"));

or by configurationfactory class:

Configurationfactory factory = new Configurationfactory ("config");

Configuration config = factory.getconfiguration ();

config . is the configuration descriptor for the above example, which specifies that the loaded Configuration object. The following is an example of a descriptor:

<?xml version= "1.0" encoding= "Iso-8859-1"?>

<configuration>
<system/>
<properties filename= "Application.properties"/>
</configuration>

This means that we are loading all the system properties as well as the application.properties configuration file. The order of precedence is from beginning to end. So, in the example above, if a property is not found in the system configuration, it will look in the properties file. This allows you to set default values in the configuration file and overwrite them with system properties.

2.3 Configuration Interface

all classes in the package represent the sharing of a single interface configuration sources for different types of configurations. This interface allows you to access and manipulate configuration properties in a common way.

the methods defined in the configuration interface can be divided into methods for querying data from configurations and modifying configuration objects. In fact, theConfiguration interface extends an underlying interface , Immutableconfiguration. all methods defined by immutableconfiguration can not change its state by reading the data object from the configuration. The configuration adds a method to configure the operation.

defined in Most of the methods in the Immutableconfiguration interface handle property retrieval for different data types. All of these methods require a key as a parameter to point to the desired property. The string value is explicitly dependent on the concrete Configuration implementation. They try to find specific properties and convert them to their target type by passing in the key, and the converted values are returned. All methods also have overloaded variants that allow you to specify a default value, and return a default value if the property is not found. The following out-of-the-box data types are supported:

    • BigDecimal

    • BigInteger

    • Boolean

    • Byte

    • Double

    • Float

    • Int

    • Long

    • Short

    • String

The names of these methods are preceded by a get with their data type. the GetString () method returns a string value, andgetInt () will work on the integer value. A property can have multiple values, so it can also query a list or array that contains all valid values. This is done using the getList () or GetArray () method.

In addition, there is a set of generic get methods that attempt to convert a request property value to a specified data type. The transformation also supports collections or array elements.

The subset () method is useful if the configuration settings are organized in a particular interface , and the application is only interested in parts of the structure. subset () passes in the key prefix string and returns a Configuration object that contains only the prefix at which the key begins.

The following methods can be used to manipulate properties or their values:

AddProperty ()

Add a new property to the configuration. If the property already exists, the other value is added to it (so it becomes a multivalued property).

Clearproperty ()

removes the specified property from the configuration.

SetProperty ()

overrides the value of the specified property. This is the same as deleting the property and then calling AddProperty () with the new property value .

Clear ()

clears the configuration.

2.4 Immutable Configuration

The Commons Configuration class Library provides most of the classes that implement the configuration interface, for example, they allow client code to change their internal state. For some use cases, this may not be ideal. For example, an application may want to protect and configure objects that do not receive sub-module control to complete the modification.

Conversion A immutableconfiguration is a simple way to configure a configuration object: Use only incoming configurations to the Configurationutils tool class. Unmodifiableconfiguration () method. This will produce an immutable configuration that contains the same data as the original configuration.

2.5 Threading Issues

When configuring from multithreaded access-it is read-only or manipulated-the question is whether Whether the Configuration implements thread safety. Immutable configurations are thread-safe because immutable objects can be safely shared across multiple threads. However, theImmutableconfiguration object is created by wrapping a mutable Configuration object configurationutils. So if the code holds a bottom -level Configuration Reference, it can always change.

Because concurrency is a complex topic, it will be explained later.


About one of Apache Commons configuration

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.