Properties file --Java & Spring

Properties file

What are the properties file?

Definition: a properties file extension, file attributes are based on key-value (key-value pairs) to save the contents of the file, such as: log4j.properties, db.properties like.

oracle.driverClassName=oracle.jdbc.driver.OracleDriver
oracle.dburl=jdbc:oracle:thin:@localhost:1521:orcl
oracle.username=dang
oracle.psw=yeshicheng

Scenario properties file?

The main effect of companies and application examples:
1. log file information, generally named the log4j.properties;
2 connection information database, generally named the db.properties;
configuration 3. Data sources (connection pool information)

How to read Java properties file?

The first step The first step reads the attribute file, creating a file object attribute [Properties properties = new Properties ();}
The second step converts the attribute file as a Resource [stream object in two ways: through a file. input byte stream;. 2 by reflection techniques. ]
The third step is to assign values to the properties file object There are two ways [assignment:. SetProperty (key, value) direct assignment and load (is) disposable loading assignment. ]
Step 4. Get the value of a property of a property file [getProperty (key)]

spring201909 Package; 
Import java.io.BufferedInputStream The; 
Import a java.io.FileInputStream; 
Import java.io.IOException; 
Import a java.io.InputStream; 
Import the java.util.Properties; 

public  class TestReadProperties {
 public  static  void main (String [] args) {
 the try {
 // the first step. the first step in creating a read attribute file attribute file object
 // underlying stored data and the read mode properties and Hashmap Hashtable as data
 // are key-value way to save and data is read by Key 
the properties properties = new new the properties (); 

// . the second step converts the attribute file as a stream resource Object
 // resource getResourceAsStream resource in the present example, the middle finger is the attribute file
 //Of course, in other examples may also be xml, txt files and other resources
 // BufferedInputStream is to improve the efficiency of read buffer input stream of bytes, is the input byte stream wrapper class
 // The first method: The attribute file find the file path attributes, and packaged as a file input stream of bytes
 // the InputStream iS =
 //     new new BufferedInputStream (new new the FileInputStream ( "config / properties / the db.properties"));
 // second method: by reflection, getResourceAsStream class object stream object resources to convert 
the InputStream iS = new new BufferedInputStream (TestReadProperties. class .getResourceAsStream (
 " /properties/db.properties " )); 

// . the third step is to assign the object attribute file
 // properties.setProperty ( key, value) direct assignment abnormal cumbersome and heavy workload
 //load equal to setProperty call this method to all key and value is db.properties stream object represents a file in again and again to save the properties file multiple times 
Properties.load ( IS );
 // The fourth step to obtain the attributes of a file. value of a property of 
the System. OUT .println (Properties.getProperty ( " oracle.driverClassName " )); 
} the catch (IOException E) { 
e.printStackTrace (); 
} 
} 
}

 

How to use the properties file in the Spring container?

Step one: Load properties file db.properties
1.1 First change the header file, add support for context tag of spring-context.xsd files and xml namespace [xmlns: context = "http://www.springframework.org/schema/context "http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd]] [
1.2 add context tags, the properties file is loaded into the context of [ <context: property-placeholder location = "classpath: properties / db.properties" /> ]
Step: Get value of the attribute file by the expression, achieved injection $ {key} {}

<? XML Version = "1.0" encoding = "UTF-. 8" ?> 
< Beans xmlns = "http://www.springframework.org/schema/beans" xmlns: the xsi = "http://www.w3.org / 2001 / XMLSchema-instance " xmlns: context =" http://www.springframework.org/schema/context " xsi: schemaLocation =" http://www.springframework.org/schema/beans HTTP: // the WWW. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd springframework.org/schema/beans/spring-beans.xsd " > 
<! - - how to make spring container to help baseDao complete property of the object properties in the properties file and baseDao associates -> 
<! - Step One: load properties file db.properties
1.1 First, change the header file, add support for context tag of spring-context.xsd file [xmlns: context = "http://www.springframework.org/schema/context"] 
1.2 add context tags, the properties file is loaded into context in [<context: property-placeholder location = "classpath: properties / db.properties" /> ] 
classpath: the directory on behalf of the corresponding compiled after the project if it is representative of java bin directory, if it is a web project WebRoot \ wEB-INF \ classes directory 
properties / db.properties: Representative preparation find properties / db.properties classes directory or from the bin directory 
-> 
< context: Property-placeholder LOCATION = "CLASSPATH: properties / db.properties" /> 
< the bean class = "org.springframework .beans.factory.config.PropertyPlaceholderConfigurer " > 
< Property name =" locations ">
<list> 
<! - Support one-time import multiple properties files, in general, can be directly imported more than one time -> 
< value > the CLASSPATH: the Properties / db.properties </ value > 
< value > the CLASSPATH: the Properties / the log4j.properties </ value > 
</ List > 
</ property > 
</ the bean > 
<-! Step: property of an object is accomplished by injecting the dao -> 
< the bean ID = "studentJdbcDaoImpl" class = "COM .yijng.dao.impl.StudentJdbcDaoImpl " > 
< Property name ="properties">
<props>
<prop key="driverClassName">${oracle.driverClassName}</prop>
<prop key="dburl">${oracle.dburl}</prop>
<prop key="username">${oracle.username}</prop>
<prop key="psw">${oracle.psw}</prop>
<prop key="log4j">${log4j.infolevel}</prop>
</props>
</property>
</bean>
</beans>

 




 

Guess you like

Origin www.cnblogs.com/codingleaf/p/11628484.html