How spring Loads a properties file _java

Source: Internet
Author: User

In the project if some parameters often need to be modified, or later may need to be modified, then we'd better put these parameters in the properties file, the source code read properties inside the configuration, so that only need to change the properties of the file can be, do not need to modify the source code, This is more convenient. This can also be done in spring, and spring has two ways to load the properties file: xml-based and annotation based methods.

The next two approaches are discussed below.
1. Loading properties files by XML
as an example of the spring instantiation DataSource, we typically make the following configuration in the Beans.xml file:

 <!--Com.mchange.v2.c3p0.ComboPooledDataSource class--> <bean ID in C3p0- package com.mchange.v2.c3p0 package 
= "DataSource" class= "Com.mchange.v2.c3p0.ComboPooledDataSource" > 
 <property name= "Driverclass" Com.mysql.jdbc.Driver "/> 
 <property name= 
 jdbcurl" value= "Jdbc:mysql://localhost:3306/shop"/> <property name= "User" value= "root"/> <property name= "password" value= " 

Now if we want to change the datasource, we have to modify the source code, but if we use the properties file, we just need to modify the inside of it, regardless of the source code things. So how do you do that?
Spring has a <context:property-placeholder location= ""/> tag that can be used to load the properties configuration file, location is the path to the configuration file. We are now in the Engineering directory of SRC under a new file, which is written above datasource configuration:

jdbcUrl=jdbc\: Mysql\://localhost\:3306/shop 

Now just make the following modifications in the Beans.xml:

<context:property-placeholder location= ""/><!--load configuration file--> 
<!-- The Com.mchange.v2.c3p0.ComboPooledDataSource class is--> <bean id= "in the COM.MCHANGE.V2.C3P0 package of the C3p0- package 
 " DataSource "class=" ${datasource} "> <!--these configuration spring will go to for--> <property when starting 
 Driverclass "value=" ${driverclass} "/> 
 <property name=" Jdbcurl "value=" ${jdbcurl} "/> 
 <property Name= "User" value= "${user}"/> 
 <property name= "password" value= "${password}"/> 

<context:property-placeholder location= ""/> Tags can also be used to replace the,<bean> tags with the following <bean> tags we are more familiar with, more readable:

 <!--is equivalent to the above configuration, the following is easier to understand--> <bean class= " Org.springframework.beans.factory.config.PropertyPlaceholderConfigurer "> <property name=" Locations "> <!--the Propertyplaceholderconfigurer class has a locations property that receives an array, where we can match multiple properties files--> <array> <va lue></value> </array> </property> </bean> 

        Although it does not seem to have the above <context:property-placeholder location= ""/> Concise, But more clearly, it is recommended to use the latter. But this is limited to XML, that is, using ${key} in Beans.xml to get value values in the configuration file.
2. Load properties file by annotation
        There is another way of annotating Use the @value annotation in Java code to load the values in the configuration file.
        Let's take a look at an example: if we're going to get an absolute path to a file in a program, it's natural to think that we can't write it down in the program. Then we can also uninstall the properties file. or in the SRC directory to create a new file, assuming that there is a record:
Filepath=e\:\\web\\apache-tomcat-8.0.26\\webapps \\E_shop\\image   
        If you want to get this filepath in Java code by annotations, you first have to configure the annotation in the Beans.xml file:

<!--the second way is to inject using annotations, mainly using annotations in Java code to inject the corresponding value value in the properties file--> 
<bean id= "prop" class= " Org.springframework.beans.factory.config.PropertiesFactoryBean "> 
 <property name=" Locations "><!- -Here is the Propertiesfactorybean class, which also has a locations attribute, and also receives an array, just like the above 

Now we can use annotations in Java code to get the value of filepath:

@Component ("FileUpload") Public 
class Fileuploadutil implements FileUpload { 
 private String filePath; 
 The @Value ("#{prop.filepath}") 
 //@Value indicates that a bean id= "prop" is found in the Beans.xml file, which reads the properties configuration file through annotations. Then go to the corresponding configuration file to read Key=filepath's corresponding value value public 
 void SetFilePath (String filePath) { 
  System.out.println ( FilePath); 
  This.filepath = FilePath; 

Note that a set method is required to be injected in, and the annotation is written on the set method. The filepath is printed through the console in the SetFilePath method to see if the console has output when you start Tomcat, and if so, show that spring is loading the filepath at startup, let's take a look at the startup information for the console:

These are the two ways that spring loads the properties configuration file. In fact, the Propertyplaceholderconfigurer class based on XML and the Propertiesfactorybean class here based on annotations are inherited Propertiesloadersupport, is used to load the properties configuration file.

Original address:

If there is a wrong place please understand, thank you for your reading.

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: 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.