A simple way to implement Java program configuration using JAXB

Source: Internet
Author: User
Tags bind java format

The customization of the program is an important feature, and most programs provide a way to customize the configuration, providing a way for users to configure the software according to their personal preferences and operating environment. The configuration of the program is a variety of ways, generally use the Windows format INI configuration file or registry, Java format Properties file, with the spread of XML files in the open program, there are more and more configuration began to use XML file, such as Java The deployment description file in EE.

We know that there are a lot of XML processing in Java, the DOM of XML organization, the Sax method, the Apache, and the many implementations that sun officials offer, as well as the excellent library support of many other third parties, such as Jdom, and dom4j. Using the lower-level approach to XML read, transform, and validate and save, you need to write a lot of code to implement it, but we now have a new option, JAXB is Java architecture for XML Binding, XML is a new specification in the Java language that can be easily converted from XML files to Java classes and from Java classes to XML files.

Using JAXB requires downloading the sun's official implementation class library, which can be downloaded from Sun's GlassFish website. The steps for developing using JAXB are as follows:

1, the definition of XML file schema, used to define the format of the XML file specification.

2, run Xjc.bat or xjc.sh script to generate Java objects defined in the schema.

3. Use the marshal static method of the Javax.xml.bind.JAXB class to generate XML files from Java objects.

4, the implementation of the Javax.xml.bind.JAXB class Unmarshal static method loading Java objects from the XML file.

It is believed that the use of JAXB is a simple and convenient approach in general program configuration, whether it is to load a custom configuration or to save a configuration. The processing of configuration parameters into ordinary Java object operations, for the loading or saving of parameters, and JAXB to achieve, so it is extremely easy.

Other ways in which the Java language implements configuration:

1, starting from jdk1.4, Java provides the Java.util.prefs package, including some configuration implementation, including the property file, as well as the Windows registry, read and write. Like the logging function provided by sun, few people use it.

2, Apache common configuration components also provide some of the configuration of processing, provide a variety of configuration support, property files, XML files, databases, and Jndi methods, but for small programs, there is a certain degree of complexity, A reference to several other Third-party jar libraries is also introduced.

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.