Say, we are using an application where we are saving data to the database. In such cases it is a good practice to keep the database credentials like username, connection url in an external file or a properties file.
Spring gives us the flexibility to fetch the contents from the properties file. Let us understand with the below example where we are going to get the value of the 'brand' property of 'Car' class from a properties file :
So, in the above case we have defined a properties file named 'application.properties' and set the value of 'brandProp' to 'Honda'.
Now, in the application-context.xml we are going to inform Spring about the application.properties file.
The below line from application-context.xml tells Spring that the name of the properties file is 'application.properties' and it needs to be included in the application.
Then comes the main part where we are trying to fetch value from 'application.properties' and assign it to the 'brand' property of the 'Car' class.
Note that value="${brandProp} gets the value from 'application.properties'
And assigns it to the 'brand' property of'Car' class.