Enjoying this Java & Spring tutorial?
Subscribe to Ram N Java on YouTubeIntroduction
In Spring Framework, constructor injection is the preferred way to ensure all mandatory dependencies are initialized properly. When working with collections such as a java.util.Map, scenarios often arise where both the key and the value are custom dependent objects rather than simple primitives or strings.
In this tutorial, you will learn how to configure Spring XML to inject a Map containing custom objects as both its keys and values using constructor dependency injection.
1. Create the Key and Value Model Classes
Let's create two separate model classes: Customer (to act as the Map key) and Car (to act as the Map value).
public class Customer {
private String name;
public Customer(String name) {
this.name = name;
}
@Override
public String toString() {
return "Customer [name=" + name + "]";
}
}
public class Car {
private String model;
public Car(String model) {
this.model = model;
}
@Override
public String toString() {
return "Car [model=" + model + "]";
}
}
2. Create the Service or Container Class
Next, create a class named CarRentalService that accepts a Map<Customer, Car> through its constructor:
import java.util.Map;
public class CarRentalService {
private Map<Customer, Car> rentalMap;
public CarRentalService(Map<Customer, Car> rentalMap) {
this.rentalMap = rentalMap;
}
public void displayRentalInfo() {
for (Map.Entry<Customer, Car> entry : rentalMap.entrySet()) {
System.out.println(entry.getKey() + " rented " + entry.getValue());
}
}
}
3. Configure Spring XML with <key-ref> and <value-ref>
In your applicationContext.xml, declare the dependent beans and then wire them inside the <map> element using key-ref and value-ref attributes inside <entry>:
<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">
<!-- Key Beans -->
<bean id="customer1" class="Customer">
<constructor-arg value="John" />
</bean>
<!-- Value Beans -->
<bean id="car1" class="Car">
<constructor-arg value="Tesla Model 3" />
</bean>
<!-- Target Bean -->
<bean id="rentalService" class="CarRentalService">
<constructor-arg>
<map>
<entry key-ref="customer1" value-ref="car1" />
</map>
</constructor-arg>
</bean>
</beans>
4. Run and Test the Code
Initialize the Spring context and call the display method to see your injected dependent objects:
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainApp {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
CarRentalService service = (CarRentalService) context.getBean("rentalService");
service.displayRentalInfo();
}
}
Watch More Spring Tutorials
Click here to watch on Youtube:
https://www.youtube.com/watch?v=3SY5lU9aoyE&list=UUhwKlOVR041tngjerWxVccw
Click the below Image to Enlarge:
| How to Inject Map having dependent Object in key and value using dependency injection by constructor |
| How to Inject Map having dependent Object in key and value using dependency injection by constructor |
| How to Inject Map having dependent Object in key and value using dependency injection by constructor |
| How to Inject Map having dependent Object in key and value using dependency injection by constructor |
| How to Inject Map having dependent Object in key and value using dependency injection by constructor |
| How to Inject Map having dependent Object in key and value using dependency injection by constructor |
No comments:
Post a Comment