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>