Open In App
Related Articles

Spring – How to Load Literal Values From Properties File

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

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

// Java Program to Illustrate Student Class
  
// Class
public class Student {
  
    // Class data members
    private int rollNo;
    private String name;
    private int age;
  
    // Getters and setters
  
    public void setRollNo(int rollNo)
    {
        this.rollNo = rollNo;
    }
  
    public void setName(String name)
    {
  
        // This keyword refers to current instance itself
        this.name = name;
    }
  
    public void setAge(int age) { this.age = age; }
  
    // Method
    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 <property> 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

XML

<?xml version="1.0" encoding="UTF-8"?>
    xsi:schemaLocation="http://www.springframework.org/schema/beans
  
    <bean id="student" class="Student">
        <property name="rollNo" value="101"/>
        <property name="name" value="Sagar"/>
        <property name="age" value="20"/>
    </bean>
  
</beans>

                    

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

// Java Program to Illustrate Application File
  
// Importing required classes
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
  
// Application class
public class Main {
  
    // Main driver method
    public static void main(String[] args)
    {
        // Implementing Spring IoC
        // using ApplicationContext
        ApplicationContext context
            = new ClassPathXmlApplicationContext(
                "beans.xml");
  
        // Getting the bean student
        Student student
            = context.getBean("student", Student.class);
  
        // Calling the methods
        // inside main() method
        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 “<context:property-placeholder location=”classpath:student-info.properties”/>”?. 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 

XML

<?xml version="1.0" encoding="UTF-8"?>
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
  
    <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>
  
</beans>

                    

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. 



Last Updated : 07 Mar, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads