Several ways Java reads a configuration file

Source: Internet
Author: User

Several ways Java reads a configuration file

Original address: http://hbcui1984.iteye.com/blog/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
 Package Chb.demo.vo;       Public class Hellobean {    private  String helloWorld;        Public String Gethelloworld () {     return  helloWorld;    }        Public void Sethelloworld (String helloWorld) {     this. HelloWorld= helloWorld;    }   }     

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

XML code
<!---->xml version= "1.0" encoding= "UTF-8"?>  <!---->>  <beans>   class= "Chb.demo.vo.HelloBean" >    <property name= "HelloWorld" >     <value>Hello!chb!<value>     property >   beans>  beans

 

(iii) reading an XML file

1. Using the Classpathxmlapplicationcontextjava code
New Classpathxmlapplicationcontext ("Beanconfig.xml");    = (Hellobean) context.getbean ("Hellobean");   System.out.println (Hellobean.gethelloworld ());  

2. Read Java code with Filesystemresource
  New Filesystemresource ("D:/software/tomcat/webapps/springwebdemo/web-inf/classes/beanconfig.xml");      New Xmlbeanfactory (RS);      = (Hellobean) factory.getbean ("Hellobean"); \     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: use Spring to read the properties file and take advantage of Java.util.Properties read (i) Read properties using Spring File We also use the Hellobean.java file above to construct the following Beanconfig.properties file: Properties code
Hellobean. class=chb.demo.vo.HelloBean   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
New defaultlistablebeanfactory ();     New Propertiesbeandefinitionreader (reg);    Reader.loadbeandefinitions (new classpathresource ("Beanconfig.properties"));     = (beanfactory) reg;     = (Hellobean) factory.getbean ("Hellobean");    System.out.println (Hellobean.gethelloworld ());   

(ii) using Java.util.Properties to read a property file for example, we construct a ipconfig.properties to hold the server IP address and port, such as: Properties code
ip=192.168.0.1   Port=8080  

Then, we can use the following program to obtain the server configuration information: Java code
this. getclass (). getClassLoader (). getResourceAsStream ("ipconfig.properties");      New Properties ();      Try {      p.load (inputstream);      Catch (IOException E1) {      e1.printstacktrace ();     }   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

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.