Spring – How to Load Literal Values From Properties File

Literals in Java are a synthetic representation of boolean, numeric, character, or string data. It is a medium of expressing particular values ​​in the program, such as an integer variable named ”/count is assigned an integer value in the following statement.

int x = 100; 
// Here 100 is a constant/literal.

Illustration: String literal.

String s = "Hello";

Properties File

The properties file is used to write the application-related property into that file. This file contains the different configuration which is required to run the application in a different environment, and each environment will have a different property defined by it. Inside the application properties file, we define every type of property like changing the port, database connectivity, connection to the eureka server, and many more. Now let’s see How to Load Literal Values ​​from Properties File in a Spring Application.

Prerequisite: Spring – Injecting Literal Values ​​By Setter Injection

Implementation: Project

First, let’s create a simple Spring Application and inject the literal values ​​by setter injection. So, create a simple class Student having three attributes rollNo, name, and age. Create setter methods for these two attributes and a simple method to print the details of the student.

Example:

Java

 

public class Student {

 

    

    private int rollNo;

    private String name;

    private int age;

 

    

 

    public void setRollNo(int rollNo)

    {

        this.rollNo = rollNo;

    }

 

    public void setName(String name)

    {

 

        

        this.name = name;

    }

 

    public void setAge(int age) { this.age = age; }

 

    

    public void display()

    {

        System.out.println("Roll No: " + rollNo);

        System.out.println("Name: " + name);

        System.out.println("Age: " + age);

    }

}

Now let’s create a Student Bean in the beans.xml file and inside the bean, you have to add your property’s name and its corresponding values ​​inside the tag. For example, for this project, we can write something like this

<bean id="student" class="Student">
   <property name="rollNo" value="101"/>
   <property name="name" value="Sagar"/>
   <property name="age" value="20"/>
</bean>

Example: beans.xml file

So now our bean is ready. Now let’s create a class and define the main() method inside that class. Suppose we have created a class named Main and we have defined the main() method inside this class. Below is the code for the Main.java class. Comments are added inside the code for better understanding.

Example

Java

 

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 student

            = context.getBean("student", Student.class);

 

        

        

        student.display();

    }

}

Now run your main() method and the output will be like this.

Output:

Roll No: 101
Name: Sagar
Age: 20

So we have seen this example before also. It’s a simple example of setter-based dependency injection. But now, here is the interesting thing. Here is the concept that we are going to discuss and it’s pretty useful. So let’s come to the beans.xml file again

<bean id="student" class="Student">
   <property name="rollNo" value="101"/>
   <property name="name" value="Sagar"/>
   <property name="age" value="20"/>
</bean>

The values ​​declared inside the property file are static values ​​(101, Sagar, 20). Now we want to load these particular values ​​from a properties file. So now let’s create a properties file in your classpath and name the file as student-info.properties (for this example we name it like this, you can name it according to your need). And in this file, we are going to write something like this

student.rollNo = 101
student.name = Sagar
student.age = 20

Now our properties file is ready and we have to load these values ​​from the properties file to the beans.xml file. How to do it? So we can write something like this

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

<bean id="student" class="Student">
   <property name="rollNo" value="${student.rollNo}"/>
   <property name="name" value="${student.name}"/>
   <property name="age" value="${student.age}"/>
</bean>

Note:

  • To load the dynamic values, the standard syntax is: “${ }”
  • Why this line “”?. This line is used to tell the Spring that we want to load the properties value from the student-info.properties file.

Example: File: beans.xml

Again run your main() method and the output will be like this.

Output:

Roll No: 101
Name: Sagar
Age: 20

So we have seen that our application is working fine. And this is how one can Load Literal Values ​​from Properties File.

Leave a Comment