In the spring boot, @Autowired injects object arrays and java collections. Java collections such as list, set, map, array are injected using the @Autowired annotation. Collections commonly used, such as ArrayList, HashMap, HashTable, HashSet, TreeHashMap, can be automatically wired using @Autowired on the spring boot.
The @Autowired annotation creates java collections and loads & injects all the beans in the class. Here, we can see how to @Autowire with list, set, map, etc.
@Autowired injecting List – String
At Spring boot, @Autowired annotation injects a list of java string objects. The collection of java strings is added to the ArrayList. The array list can be injected using the @Autowired annotation on the spring boot. In the example below, the name list is given in the NameService class. The list is created as a spring boot bean using the @Bean annotation in the @Configuration annotation class.
@Component
public class NameService {
@Autowired
List<String> nameList;
@PostConstruct
public void print() {
System.out.println(nameList);
}
}
@Configuration
public class ListConfig {
@Bean
public List<String> nameList() {
ArrayList<String> list = new ArrayList<String>();
list.add("Danit");
list.add("Jin");
list.add("Simon");
return list;
}
}
Output
[Danit, Jin, Simon]
@Autowired injecting List of Objects
The @Autowired annotation injects a list of objects that are implemented by an interface. If the interface is auto wire with @Autowired annotation, all the class objects loaded as bean in the ApplicationContext will be added to the list. In the example below, Vehicle is an interface, all classes implemented are auto-wired in the vehicle list.
@Component
public class VehicleService {
@Autowired
List<Vehicle> vehicleList;
@PostConstruct
public void print() {
System.out.println(vehicleList);
}
}
public interface Vehicle {
}
@Component
public class Car implements Vehicle{
@Override
public String toString() {
return "Car";
}
}
@Component
public class Bike implements Vehicle{
@Override
public String toString() {
return "Bike";
}
}
@Component
public class Bus implements Vehicle{
@Override
public String toString() {
return "Bus";
}
}
Output
[Bike, Car, Bus]
@Autowired injecting List of Beans
Spring boot allows you to create multiple beans of a class using the @Bean annotation. All beans are a single class instance. If the class is auto wired, it throws exception saying multiple beans are available for the class. In this case, we can load all the class in a list. The annotation @Autowired injects all the beans as list.
@Component
public class VehicleService {
@Autowired
List<Vehicle> vehicleList;
@PostConstruct
public void print() {
System.out.println(vehicleList);
}
}
@Configuration
public class VehicleConfig {
@Bean
public Vehicle getCar() {
return new Vehicle("Car");
}
@Bean
public Vehicle getBike() {
return new Vehicle("Bike");
}
@Bean
public Vehicle getBus() {
return new Vehicle("Bus");
}
@Bean
public Vehicle getBicycle() {
return new Vehicle("Bicycle");
}
}
public class Vehicle {
private String name;
public Vehicle(String name) {
this.name = name;
}
@Override
public String toString() {
return name;
}
}
Output
[Car, Bike, Bus, Bicycle]
@Autowired injecting List of Beans with sorting order
In the spring boot, @Autowired annotation injects a list of Beans based on the loading order in the ApplicationContext. The @Order annotation changes the loading order and the @Autowired annotation injects the bean list as specified in the order. The example below shows how the order is created and the output displays the order as specified.
@Component
public class VehicleService {
@Autowired
List<Vehicle> vehicleList;
@PostConstruct
public void print() {
System.out.println(vehicleList);
}
}
@Configuration
public class VehicleConfig {
@Bean
@Order(2)
public Vehicle getCar() {
return new Vehicle("Car");
}
@Bean
@Order(4)
public Vehicle getBike() {
return new Vehicle("Bike");
}
@Bean
@Order(1)
public Vehicle getBus() {
return new Vehicle("Bus");
}
@Bean
@Order(3)
public Vehicle getBicycle() {
return new Vehicle("Bicycle");
}
}
public class Vehicle {
private String name;
public Vehicle(String name) {
this.name = name;
}
@Override
public String toString() {
return name;
}
}
Output
[Bus, Car, Bicycle, Bike]
@Autowired injecting List of Beans with @Qualifier
There are many implementation classes created for the interface in the spring boot application. The @Autowired annotation injects all the class implemented in the list. The @Qualifier annotation is used to filter a list of implemented classes based on the need. The @Qualifier annotation creates a subset of the implemented class to be loaded into the created list using the @Autowired annotation.
In the example below, the @Qualifier annotation is created for two wheeler vehicles. The list is loaded only by the two wheeler vehicles using the @Autowire annotation in the spring boot application.
@Component
public class VehicleService {
@Autowired
@Qualifier("twowheeler")
List<Vehicle> vehicleList;
@PostConstruct
public void print() {
System.out.println(vehicleList);
}
}
@Configuration
public class VehicleConfig {
@Bean
public Vehicle getCar() {
return new Vehicle("Car");
}
@Bean
@Qualifier("twowheeler")
public Vehicle getBike() {
return new Vehicle("Bike");
}
@Bean
public Vehicle getBus() {
return new Vehicle("Bus");
}
@Bean
@Qualifier("twowheeler")
public Vehicle getBicycle() {
return new Vehicle("Bicycle");
}
}
Output
[Bicycle, Bike]
@Autowired Injecting Arrays
In the spring boot, @Autowired injects an array of objects. A list of the elements stored in the java array may be injected into another bean. Like java collections, the array of elements is injected using @Autowired annotation. The example below shows how to inject the array using the @Autowired annotation in the spring boot.
@Component
public class VehicleService {
@Autowired
Vehicle[] vehicleArray;
@PostConstruct
public void print() {
for(Vehicle vehicle : vehicleArray) {
System.out.print(vehicle+", ");
}
}
}
@Configuration
public class VehicleConfig {
@Bean
public Vehicle[] vehicleArray() {
Vehicle[] vehicleArray = new Vehicle[3];
vehicleArray[0]= new Vehicle("Car");
vehicleArray[1]= new Vehicle("Bike");
vehicleArray[2]= new Vehicle("Bus");
return vehicleArray;
}
}
Output
Bus, Car, Bike,
@Autowired Injecting Set – HashSet
In the spring boot, @Autowired injects a set of objects. A list of the elements stored in the java set may be injected into another bean. Like java collections, the set of elements is injected using @Autowired annotation. The example below shows how to inject the set using the @Autowired annotation in the spring boot.
@Component
public class VehicleService {
@Autowired
Set<Vehicle> vehicleSet;
@PostConstruct
public void print() {
System.out.println(vehicleSet);
}
}
@Configuration
public class VehicleConfig {
@Bean
public Set<Vehicle> vehicleSet() {
Set<Vehicle> vehicleSet = new HashSet<Vehicle>();
vehicleSet.add( new Vehicle("Car") );
vehicleSet.add( new Vehicle("Bike") );
vehicleSet.add( new Vehicle("Bus") );
return vehicleSet;
}
}
Output
Bus, Car, Bike,
@Autowired Injecting Map – HashMap, HashTable
In the spring boot, @Autowired injects a Map with key value pair. A list of the elements stored in the java map may be injected into another bean. Like java collections, the map is injected using @Autowired annotation. The example below shows how to inject the map using the @Autowired annotation in the spring boot.
@Component
public class VehicleService {
@Autowired
Map<String, Vehicle> vehicleMap;
@PostConstruct
public void print() {
System.out.println(vehicleMap);
}
}
@Configuration
public class VehicleConfig {
@Bean
public Map<String, Vehicle> vehicleMap() {
Map<String, Vehicle> vehicleMap = new HashMap<String, Vehicle>();
vehicleMap.put( "Car", new Vehicle("Car") );
vehicleMap.put( "Bike", new Vehicle("Bike") );
vehicleMap.put( "Bus", new Vehicle("Bus") );
return vehicleMap;
}
}
Output
{Bus=Bus, Car=Car, Bike=Bike}