How Spring reads the contents of the properties file

Source: Internet
Author: User

Http://hi.baidu.com/alizv/blog/item/d8cb2af4094662dbf3d38539.html

In the real work, we often need to save some system configuration information, people generally choose the configuration file to complete, this article according to my work to read the properties configuration file a small summary, the main description is spring read the configuration file method.
Read the configuration file with spring, the most typical is the connection to the database, here is an example:
File jdbc.properties:
-------------------------------------------------------------------------------------
Driverclassname Com.mysql.jdbc.Driver
URL Jdbc:mysql://localhost:3306/test
Username Root
Password 1234
------------------------------------------------------------------------------------
Introduce spring's related jar package, which is configured in Applicationcontext.xml:
-------------------------------------------------------------------------------------
<?xml version= "1.0" encoding= "UTF-8"?>
<! DOCTYPE beans Public "-//spring//dtd bean//en" "Http://www.springframework.org/dtd/spring-beans.dtd" >

<beans>
<bean id= "Propertyconfigurer" class= "Org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" >
<property name= "Location" >
<value>src/jdbc.properties</value>
</property>
</bean>

<bean id= "DataSource" class= "Org.springframework.jdbc.datasource.DriverManagerDataSource" >
<property name= "Driverclassname" >
<value>${driverClassName}</value>
</property>
<property name= "url" >
<value>${url}</value>
</property>
<property name= "username" >
<value>${username}</value>
</property>
<property name= "Password" >
<value>${password}</value>
</property>
</bean>

<bean id= "DAO" class= "Com.zh.model.DataDAO" >
<property name= "DataSource" >
<ref local= "DataSource"/>
</property>
</bean>

</beans>
-----------------------------------------------------------------------------------------
Datadao.java

Package Com.zh.model;

Import Javax.sql.DataSource;

public class Datadao {
Private DataSource DataSource;

Public DataSource Getdatasource () {
return datasource;
}

public void Setdatasource (DataSource DataSource) {
This.datasource = DataSource;
}

}
------------------------------------------------------------------------------------
Test if the connection is successful, Test.java
Package com.zh.logic;

Import java.sql.Connection;

Import Org.springframework.context.ApplicationContext;
Import Org.springframework.context.support.FileSystemXmlApplicationContext;

Import Com.zh.model.DataDAO;

public class Test {
public static void Main (String [] args) {
try{
string[] Path = {"Src/applicationcontext.xml"};
ApplicationContext CTX = new Filesystemxmlapplicationcontext (path);

Datadao DAO = (Datadao) ctx.getbean ("DAO");
Connection con = Dao.getdatasource (). getconnection ();
System.out.println (con.isclosed ());
System.out.print (Dao.getname ());
}catch (Exception ex) {
Ex.printstacktrace ();
}
}
}
-------------------------------------------------------------------------------------
2. Use the Java.util.Properties class to read
For example, we construct a ipconfig.properties to hold the server IP address and port, such as:
ip=192.168.0.1
port=8080
--------------------------------------------------------------------------------------
Then we can use the following procedure to obtain the server configuration information:
InputStream InputStream = This.getclass (). getClassLoader (). getResourceAsStream ("ipconfig.properties");
Properties P = new properties ();
try{
P.load (InputStream);
} catch (IOException E1) {
E1.printstacktrace ();
}
System.out.println ("IP:" +p.getproperty ("IP") + ", Port:" +p.getproperty ("Port");
--------------------------------------------------------------------------------------
The above describes the content of reading properties, in reality we may want to modify the contents of the file, the following see how to modify the properties of the content, the file or the above:
Package com.zh.logic;

Import Java.io.FileInputStream;
Import Java.io.FileOutputStream;
Import Java.io.InputStream;
Import java.util.Properties;

public class Testread {

public void Read () {
try {
InputStream in = This.getclass (). getClassLoader (). getResourceAsStream ("config/host.properties");
Properties P = new properties ();
P.load (in);
P.list (System.out);

System.out.println (p.getproperty ("IP") + "," +p.getproperty ("username") + "," +p.getproperty ("pwd"));
} catch (Exception e) {
E.printstacktrace ();
}
}

public void Update (String path) {
try{
Properties P = new properties ();
FileInputStream in = new FileInputStream (path);
P.load (in);
FileOutputStream out = new FileOutputStream (path);

P.setproperty ("IP", "1234567");
P.store (out, "IP Update");
P.save (out, "IP updated");
}catch (Exception ex) {
Ex.printstacktrace ();
}
}
public static void Main (string[] args) {
Testread td = New Testread ();
Td.read ();
Td.update ("Config/host.properties");
Td.read ();
}
}
You can see that changes have been taken before and after the changes, and there are a few things to note:
FileInputStream in = new FileInputStream (path);
P.load (in);
FileOutputStream out = new FileOutputStream (path);
is P.load (in), to be written on fileoutputstream out = new FileOutputStream (path), before, otherwise, the modified file content becomes ip=1234567, and port=8080 this sentence is overwritten; What would you like to think about yourself?

The above describes the two ways to read the properties file, I hope to be helpful to everyone ...

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.