Read the properties configuration file with spring read the properties

Source: Internet
Author: User

Create a new properties file called Test.properties content as follows:
Name=studiozero
Address=beijing
First we make the following configuration in Applicationcontext.xml. Note that the test.properties file needs to be placed in the same directory as the Applicationcontext.xml
<bean id= "Propertyconfigurer" class= "Org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" >
<property name= "Location" >
<value>classpath:test.properties</value>
</property>
</bean>
After doing the above work, spring will be able to read the information in the properties. Let's take a look at the ReadProperties class if you add the information you get
First, configure the ReadProperties class to spring. The code is as follows
public class readproperties{
Private String MyName;
Private String myAddress;
Provide getter and setter methods for name and address

public static void Main (string[] args) {
System.out.println ("My name is" +myname);
System.out.println ("My address is" +myaddress);
}
}
<bean id= "readproperties" class= "com.sring.test.ReadProperties" scope= "Request" >
</bean>
Then we do some configuration in Applicationcontext.xml to assign the values of MyName and myaddress in Test.properties to the name and address code in ReadProperties as follows
<property name= "MyName" value= "${name}" ></property>
<property name= "myAddress" value= "${address}" ></property>
The full spring configuration file is as follows:
<bean id= "readproperties" class= "com.sring.test.ReadProperties" scope= "Request" >
<property name= "MyName" value= "${name}" ></property>
<property name= "myAddress" value= "${address}" ></property>



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.