RPM: Several ways Java reads a configuration file

Source: Internet
Author: User

Transferred from: http://www.iteye.com/topic/56496

In the real work, we often need to save some system configuration information, we generally choose the configuration file to complete, this article according to the author's work in the use of the method of reading the configuration file a small summary, the main description is spring read the configuration file method.

I. Reading an XML configuration file

(i) Create a new Java bean (Hellobean.java)

Java code

    1. Package chb.demo.vo;
    2. public class Hellobean {
    3. Private String HelloWorld;
    4. Public String Gethelloworld () {
    5. return helloWorld;
    6. }
    7. public void Sethelloworld (String helloWorld) {
    8. This.helloworld = HelloWorld;
    9. }
    10. }

(ii) construct a configuration file (Beanconfig.xml)

XML code

    1. <!---->xml version= "1.0" encoding= "UTF-8"?>
    2. <!---->>
    3. <beans>
    4. <bean id= "Hellobean" class= "Chb.demo.vo.HelloBean" >
    5. <property name= "HelloWorld" >
    6. <value>Hello!chb!value>
    7. Property>
    8. Bean>
    9. Beans>

(iii) reading an XML file

1. Using Classpathxmlapplicationcontext

Java code

    1. ApplicationContext context = new Classpathxmlapplicationcontext ("Beanconfig.xml");
    2. Hellobean Hellobean = (Hellobean) context.getbean ("Hellobean");
    3. System.out.println (Hellobean.gethelloworld ());

2. Read with Filesystemresource

Java code

    1. Resource rs = new Filesystemresource ("D:/software/tomcat/webapps/springwebdemo/web-inf/classes/beanconfig.xml");
    2. Beanfactory factory = new Xmlbeanfactory (RS);
    3. Hellobean Hellobean = (Hellobean) factory.getbean ("Hellobean"); \
    4. System.out.println (Hellobean.gethelloworld ());

It is important to note that, with Filesystemresource, the configuration file must be placed in the project direct directory, or an absolute path, or an exception will be thrown that cannot find the file.

Two. Read the properties configuration file

Here are two techniques: using spring to read the properties file and to read with Java.util.Properties

(i) using spring to read the properties file

We also use the Hellobean.java file above to construct the following beanconfig.properties file:

Properties Code

    1. Hellobean.class=chb.demo.vo.hellobean
    2. hellobean.helloworld=hello!chb!

The "Hellobean" name in the properties file is the Bean's alias setting, and the. Class is used to specify the source of the class.

Then use Org.springframework.beans.factory.support.PropertiesBeanDefinitionReader to read the properties file

Java code

    1. Beandefinitionregistry reg = new Defaultlistablebeanfactory ();
    2. Propertiesbeandefinitionreader reader = new Propertiesbeandefinitionreader (reg);
    3. Reader.loadbeandefinitions (New Classpathresource ("Beanconfig.properties"));
    4. Beanfactory factory = (beanfactory) reg;
    5. Hellobean Hellobean = (Hellobean) factory.getbean ("Hellobean");
    6. System.out.println (Hellobean.gethelloworld ());

(ii) using Java.util.Properties to read the properties file

For example, we construct a ipconfig.properties to hold the server IP address and port, such as:

Properties Code

    1. ip=192.168.0.1
    2. port=8080

Then we can use the following procedure to obtain the server configuration information:

Java code

    1. InputStream InputStream = This.getclass (). getClassLoader (). getResourceAsStream ("ipconfig.properties");
    2. Properties P = new properties ();
    3. try {
    4. P.load (InputStream);
    5. } catch (IOException E1) {
    6. E1.printstacktrace ();
    7. }
    8. System.out.println ("IP:" +p.getproperty ("IP") + ", Port:" +p.getproperty ("Port");

This article only introduces a few simple operations, the wrong place I hope you have a lot of advice

RPM: Several ways Java reads a configuration file

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.