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
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:
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); } }
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.
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
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.
Spring setter injection with literal values:
public class MyService { private String serviceName; public void setServiceName(String serviceName) { this.serviceName = serviceName; } }
Setter-based dependency injection in Spring with literals:
public class MyService { private String serviceName; public void setServiceName(String serviceName) { this.serviceName = serviceName; } }
Injecting constants in Spring setter methods:
public class MyService { private int MAX_RETRIES; public void setMaxRetries(@Value("${max.retries}") int maxRetries) { this.MAX_RETRIES = maxRetries; } }
Spring framework setter injection example for literals:
public class MyService { private MyRepository myRepository; public void setMyRepository(MyRepository myRepository) { this.myRepository = myRepository; } }
Setter injection for primitive values in Spring:
public class MyService { private int maxRetryAttempts; public void setMaxRetryAttempts(int maxRetryAttempts) { this.maxRetryAttempts = maxRetryAttempts; } }
Injecting properties in Spring setter methods:
public class MyService { private String serviceUrl; public void setServiceUrl(@Value("${service.url}") String serviceUrl) { this.serviceUrl = serviceUrl; } }
Spring XML configuration for setter injection with literals:
<property>
element.<bean id="myService" class="com.example.MyService"> <property name="serviceName" value="exampleService" /> </bean>
Autowired setter injection with literals in Spring:
@Autowired
on setter methods along with @Value
to inject literals into the setter.public class MyService { private String serviceName; @Autowired public void setServiceName(@Value("${service.name}") String serviceName) { this.serviceName = serviceName; } }
Injecting primitive values using @Value annotation in Spring setters:
@Value
annotation on setter parameters to inject values from properties files or configuration directly into the setter.public class MyService { private int maxRetryAttempts; @Value("${max.retries}") public void setMaxRetryAttempts(int maxRetryAttempts) { this.maxRetryAttempts = maxRetryAttempts; } }