You are here: Home / Topics / How to use (dot)properties file in Spring Framework Full Explanation

How to use (dot)properties file in Spring Framework Full Explanation

Filed under: Spring on 2023-08-20 12:41:12

Suppose that we have a class Student.java and we have three dependencies in this class


private string name;
private int age;
private Cheat cheat; //Cheat is an interface


We want to use .properties file to inject values in our primitive type dependencies. How can we do that..... We can do that by making one .property class in our Resources folder. In my case, I have made student.properties file in my resources folder.

Content of my student.properties file is

 
student.name = Shyam Dubey
student.age = 27

Now comes to the beans.xml file which is also located in our Resources folder. We will use this properties file by telling the spring that Hey spring I have a property file and i want to use this file for injecting the values of my primitive dependencies. For that I have to write a line of code in beans.xml

<context:property-placeholder location="classpath:student.properties" />

Note You might face an error here for removing that error make sure your beans.xml file has this code written above the <context:...... /> code

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:context="http://www.springframework.org/schema/context"
      xsi:schemaLocation="http://www.springframework.org/schema/beans
 http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
 http://www.springframework.org/schema/context
 http://www.springframework.org/schema/context/spring-context-4.1.xsd">
   <!-- bean definitions here -->


<context:property-placeholder location="classpath:student.properties" />

</beans>


Now we will tell spring to create an object of my class Student.java All of you know how to write code for that

<bean id="student" class="org.example.Student">

</bean>

Now you have to see that Student class has three dependencies and we have to inject these dependencies, How can we do that ............... simple by telling spring that we have three properties or dependencies in this class.

<bean id="student" class="org.example.Student">
<property name="name" value="${student.name}" />
<property name="age" value = "${student.age}" />
<property name = "cheat" ref="scienceCheatBean" />
</bean>

You might wonder what is scienceCheatBean. To make this simple ,  I want to tell you that Our Cheat interface is implementing our two more classes MathCheat and Science Cheat. We have to tell spring that hey spring create object for these two classes as well....
How can be do that? Just by writing code in our beans.xml file.

<bean id="scienceCheatBean" class="org.example.Science" /> 
<bean id="mathCheatBean" class="org.example.Math" />

Now we have created object for these classes and we have used reference of these objects in our student bean just by writing ref="scienceCheatBean"

Hope you understand that......

And Now when we run our Main.java file

Code of Main.java

package org.example;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
   public static void main(String[] args) {
      ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
      Student stu = context.getBean("stu", Student.class);
      stu.cheat();
   }
}

 

Output of this code will be:

Science Cheating started...
Student name is : Shyam Dubey
Student age is : 27

Process finished with exit code 0

In next tutorial we will learn how to use Annotation inplace of Beans.xml file...


Thank you guyss if you have any problem in this code... Feel free to ask me in comment section.


About Author:
M
Mr. Dubey     View Profile
Founder of MCQ Buddy. I just like to help others. This portal helps students in getting study material free. Share your stuff here so that others can get benefitted.