Java Properties Class Read configuration file

Source: Internet
Author: User

1.JAVA Properties class, in the Java.util package, the specific class is java.util.properties.
The properties class inherits from the Hashtable class and implements the map interface, and it also holds the property set in the form of a key-value pair. However, the properties have a special place, that is, its keys and values are string types.
The 2.properties class is a collection class that fills KeyValue key-value pairs. Loads the information in the. properties file into the Properties object.
3. Load the information in the. properties file into the Properties object.
. The Main method in properties

(1) Read Data Island Properties object Total, Load (InputStream instream)

This method can load a property list into the Properties class object from the file input stream corresponding to the. Properties property file. As in the following code:

Properties Pro = new properties ();
FileInputStream in = new FileInputStream ("A.properties");
Pro.load (in);
In.close ();
4. You can also export the information stored in the Properties object to a file. Using the Store method of properties
(2) Store (OutputStream out, String comments)

This method saves the property list of the Properties class object to the output stream. As in the following code:

FileOutputStream ofile = new FileOutputStream (file, "a.properties");
Pro.store (ofile, "Comment");
Ofile.close ();

If comments is not empty, the first row of the saved properties file is #comments, which represents the comment information, and if blank, no comment information.

The comment information is followed by the current save time information for the property file.
(3) Save the data in the Properties object to an XML file, using the Storetoxml () method of the properties.


How does 4.Spring read the configuration file?
In the Java Web project, we often save some system configuration parameters, database user name password, encoding format, etc. to the XML or properties file. Then spring starts by reading the data in these configuration files with the ${}dolllar character, which is injected into the bean, such as the generation of the database connection pool bean.
The spring framework has a propertyplaceholderconfigurer class, which is designed to read the configuration file, The classpath is org.springframework.beans.factory.config.PropertyPlaceholderConfigurer.
So how do we read the properties file in spring?

1. First. We'll start with the spring configuration file. Defines a class that specifically reads the properties file.
Cases:
<bean id= "Propertyconfigurer" class= "Org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" >
<property name= "Locations" >
<list>
<value>classpath:jdbc.properties</value>
<!--If you have more than one profile, just continue adding it here--
</list>
</property>
</bean>

Why use locations here (there is also a location)
is because. Generally speaking. Inside of our project. There may be more than one configuration file.
Even if there is only one. The new words to be added in the future. Just add a value tag below.
Without having to re-modify too much. Of course Performance, this is the configuration of the current server. is Kiko negligible).

Then we can fill in the specific configuration information in the Jdbc.properties file.

<!--configuring C3P0 data Sources--
<bean id= "DataSource" class= "Com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method= "Close" >
<property name= "Driverclass" >
<value>${jdbc.driverClassName}</value>
</property>
<property name= "Jdbcurl" >
<value>${jdbc.url}</value>
</property>
<property name= "User" >
<value>${jdbc.username}</value>
</property>
<property name= "Password" >
<value>${jdbc.password}</value>
</property>
</bean>

The information written by the Jdbc.properties file.

Jdbc.driverclassname=com.mysql.jdbc.driver
Jdbc.url=jdbc:mysql://localhost:3306/test
Jdbc.username=root
Jdbc.password=root

Java Properties Class Read configuration file

Related Article

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.