Injecting literal values through Spring dependency injection

In this article, we will learn about Injecting literal values through Spring dependency injection with a proper example.

A Literal is a constant value that we assign to a variable.

Literals in java can be of multiple types:

Integer literal represents integer values. Ex. int sum = 100;
Here 100 is an Integer literal value assigned to the sum variable.

String literals represent String values. Ex. String name = “Suraj”;
Here “Suraj” is a String literal value assigned to the name variable.

Floating point literals represent decimal or exponent values.
Ex. float average = 67.5F.

Char literals represent individual character values.
Ex. char ch=’a’;

Boolean literals represent either true or false.
Ex. boolean isValid = true;

In the below example, we will inject the String literal values using Spring setter dependency injection. These literal values gets injected into one of the spring beans.

We will create a class Manager with 2 variables name and salary. We will create setter methods for both of these fields. Spring container will inject the literal values using this setter method.

package com.getinputs;

public class Manager implements Employee {

  private String name;
  private int salary;

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public int getSalary() {
    return salary;
  }

  public void setSalary(int salary) {
    this.salary = salary;
  }
}

Here the naming of the setter method should be proper

setter method for name = “set”+ Capitalize ( name )

Note* Capitalize ( name ) means begin the name word with a capital letter

setter method for name = setName

setter method for salary= setSalary

Now we will create an applicationContext.xml file which will be read by the Spring container.

In this context file, we will give the instruction to the Spring container on what all beans to create and how to manage the dependency injection.

Here we will also give the instruction to inject the literal values as well.

In the value tag, we will provide the literal values.

<?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.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd">

    <!-- Define your beans here -->
        
    <bean id="employee" class="com.getinputs.Manager">
    
       <!-- Here we are injecting the literal values -->
       
       <property name="name" value="Iqbal"></property>
       <property name="salary" value="50000"></property>
       
    </bean> 
</beans>

Now we will check if the literal values are injected or not.

package com.getinputs;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpringDemoApp {
  public static void main(String[] args) {

    ClassPathXmlApplicationContext context = new 
    ClassPathXmlApplicationContext("applicationContext.xml");
    
    Manager manager = (Manager) context.getBean("employee", Employee.class);
    
    System.out.println("Name of the manager is : " + manager.getName());
    
    System.out.println("Salary of the manager is : " + manager.getSalary());
    
    context.close();
  }
}

Output: Name of the manager is : Iqbal
Salary of the manager is : 50000

Here we can see both the name and salary literal values are injected in the Manager bean and are displayed properly in the output.

This is how we will be Injecting literal values through Spring dependency injection.

I hope you found this article interesting and valuable. If you are having any concerns or questions about this article please comment below.

https://github.com/getinputs/samples/tree/main/Injecting-literals-spring-dependency-injection

Leave a Comment