Spring Boot Tutorial

Spring Boot - Software Setup and Configuration (STS/Eclipse/IntelliJ)

Prerequisite (Spring Core Concepts)

Spring Boot Core

Spring Boot with REST API

Spring Boot with Database and Data JPA

Spring Boot with Kafka

Spring Boot with AOP

Spring - Injecting Literal Values By Setter Injection

Injecting literal values using setter injection in Spring involves defining setter methods in your Java class and using the Spring XML configuration to provide the values for those setters.

Let's continue with the Person class example:

1. Java Classes:

Define setters for the name and age fields in the Person class:

public class Person {
    private String name;
    private int age;

    // Setter for the name
    public void setName(String name) {
        this.name = name;
    }

    // Setter for the age
    public void setAge(int age) {
        this.age = age;
    }

    public void displayInfo() {
        System.out.println("Name: " + name);
        System.out.println("Age: " + age);
    }
}

2. XML Configuration:

To inject the name and age using setter injection, you would define the beans in the XML configuration as:

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

    <bean id="personBean" class="com.example.Person">
        <property name="name" value="John Doe" />
        <property name="age" value="30" />
    </bean>

</beans>

Here, the <property> tags are used to set values to the properties of the Person bean. The name attribute of the <property> tag corresponds to the name of the property (or the setter method) and the value attribute provides the value to be set.

3. Testing the Configuration:

Now, to test the configuration:

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class App {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        
        Person person = (Person) context.getBean("personBean");
        person.displayInfo();
    }
}

Running the App class should produce:

Name: John Doe
Age: 30

Note:

Setter injection is particularly useful when you have optional dependencies or when you want to override certain values that were set using constructor injection. However, as before, while this example uses XML-based configuration, modern Spring applications often lean towards annotations or Java-based configurations for bean definitions and dependencies.

  1. Spring setter injection with literal values:

    • Description: Setter injection in Spring involves injecting dependencies through setter methods. Literal values, such as strings or numbers, can be directly injected.
    • Code:
      public class MyService {
          private String serviceName;
      
          public void setServiceName(String serviceName) {
              this.serviceName = serviceName;
          }
      }
      
  2. Setter-based dependency injection in Spring with literals:

    • Description: Combine setter injection with literals to inject both dependencies and literal values into a bean.
    • Code:
      public class MyService {
          private String serviceName;
      
          public void setServiceName(String serviceName) {
              this.serviceName = serviceName;
          }
      }
      
  3. Injecting constants in Spring setter methods:

    • Description: Constants can be injected into a setter method. The method sets the constant value, ensuring it remains the same throughout the object's lifecycle.
    • Code:
      public class MyService {
          private int MAX_RETRIES;
      
          public void setMaxRetries(@Value("${max.retries}") int maxRetries) {
              this.MAX_RETRIES = maxRetries;
          }
      }
      
  4. Spring framework setter injection example for literals:

    • Description: The Spring framework supports setter injection as a method of injecting dependencies into beans. This is a fundamental feature of Spring's Inversion of Control (IoC) container.
    • Code:
      public class MyService {
          private MyRepository myRepository;
      
          public void setMyRepository(MyRepository myRepository) {
              this.myRepository = myRepository;
          }
      }
      
  5. Setter injection for primitive values in Spring:

    • Description: Primitives can be injected into setter methods in the same way as other objects. Spring automatically converts and injects the values.
    • Code:
      public class MyService {
          private int maxRetryAttempts;
      
          public void setMaxRetryAttempts(int maxRetryAttempts) {
              this.maxRetryAttempts = maxRetryAttempts;
          }
      }
      
  6. Injecting properties in Spring setter methods:

    • Description: Inject properties into setter methods by specifying them as parameters. Spring automatically resolves and injects these properties.
    • Code:
      public class MyService {
          private String serviceUrl;
      
          public void setServiceUrl(@Value("${service.url}") String serviceUrl) {
              this.serviceUrl = serviceUrl;
          }
      }
      
  7. Spring XML configuration for setter injection with literals:

    • Description: In XML-based configuration, define property values within the <property> element.
    • Code:
      <bean id="myService" class="com.example.MyService">
          <property name="serviceName" value="exampleService" />
      </bean>
      
  8. Autowired setter injection with literals in Spring:

    • Description: Use @Autowired on setter methods along with @Value to inject literals into the setter.
    • Code:
      public class MyService {
          private String serviceName;
      
          @Autowired
          public void setServiceName(@Value("${service.name}") String serviceName) {
              this.serviceName = serviceName;
          }
      }
      
  9. Injecting primitive values using @Value annotation in Spring setters:

    • Description: Use @Value annotation on setter parameters to inject values from properties files or configuration directly into the setter.
    • Code:
      public class MyService {
          private int maxRetryAttempts;
      
          @Value("${max.retries}")
          public void setMaxRetryAttempts(int maxRetryAttempts) {
              this.maxRetryAttempts = maxRetryAttempts;
          }
      }