How to Read key value from .properties file in Spring

Connect with

Spring FrameworkReading single .properties file or multiple properties file in spring is very easy and it can be done by different way. I’m going to demonstrate by using basic bean configuration.

We can use PropertyPlaceholderConfigurer class which contains in spring core module itself.

Properties file

config.properties file:

mongo.host=10.10.8.8
mongo.db=mydb
mongo.port=27017
mongo.userName=mydbUser
mongo.password=mydbUser@123
ftp.driverdata.location=/opt/mydbApp/data/

2. Single properties file in xml meta data configuration

If you have single properties file, you have to user location property of PropertyPlaceholderConfigurer class.






	



	
	
	
	
	
 

3. Multiple properties file

If you have multiple properties file , you have to user locations property of PropertyPlaceholderConfigurer class. If .properties file outside of your application you can use absolute path with prefix as file:. If within you application you can use classpath: prefix in you location. You can use locations property as well if you have only one .properties file.



	
	   
		file:/opt/config.properties
                file:/opt/jdbc.properties
           
	

Your comments are welcome. Happy Learning 🙂


Connect with

Leave a Reply

Your email address will not be published. Required fields are marked *