In the spring boot, @Autowired annotation is used for dependency injection.In spring boot application, all loaded beans are eligible for auto wiring to another bean. The annotation @Autowired in spring boot is used to auto-wire a bean into another bean. The annotation @Autowired helps to auto wire the collaborative beans in spring boot framework. The autowired spring boot bean is eligible for use in Springboot.

In the spring boot, the @Autowired annotation simplifies the auto-wire by endorsing all available means. Here, we can see two types @Autowired by name and @Autowired by type. Even how to use @Autowired in properties, @Autowired in setter methods, @Autowired in constructor, @Autowired in @bean in spring boot.

The annotation @Autowired in spring boot can be configured for optional injection. @Autowired annotation maps for a single bean only. If two or more beans are loaded, the dispute between more than one bean will be resolved using the @Qualifier annotation.



Enable @Autowired in Spring Boot

In the spring boot application, all loaded beans are eligible for auto wiring to another bean. The @Component annotation is used to load a java class as a bean. All classes with annotation such as @Component, @bean etc are auto-wired in the spring boot application.

The package of spring boot main class is called root package. If the spring boot beans package is not created under the sub package of root package, the annotation @ComponentScan is used to enable the automatic wiring of these spring boot beans.

package com.yawintutor;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Component;

@Component
@ComponentScan("com.yawin")
public class Zoo {
	@Autowired
	Lion lion;
}


@Autowired by Type in Spring boot

The @autowired annotation is used to inject dependency. Dependency injection is done in two ways, by name and by type. @Autowired by type uses the class type to auto wire the spring boot bean class. The bean class can be auto-wired with the class name. Only one bean class should be loaded into the ApplicationContext.

@Component
public class Car {	
}
@Component
public class VehicleService {
	@Autowired
	Car myCar;
}


@Autowired by Name in Spring boot

The @Autowired annotation is used to inject a bean in another class in two ways. One way is @Autowired by name. For auto wiring by name, the name of the variable is used for the dependency injection. The name of the variable should be the same as the name of the class or the bean name configured in the @Component annotation.

@Component
public class VehicleService {
	@Autowired
	Vehicle car;
}
@Component
public class Car implements Vehicle{
}
public interface Vehicle {
}


@Autowired in properties

The @Autowired annotation injects a bean class configured as the properties of another bean. Properties in a class are automatically assigned with a value or a reference of another class. It avoids the explicit call of getter and setter methods. The @Autowired annotation in properties is used to initialize variables in the class.

@Component
public class VehicleService {
	@Autowired
	Vehicle car;   // ---> By name

	@Autowired
	Car mycar;   // --> By type
}
@Component
public class Car implements Vehicle{
}

public interface Vehicle {
}


@Autowired in Constructors

The @Autowired annotation is used in the class constructor method. The value of the constructor argument is passed automatically while the instance class is created. In the spring boot, @Autowired assigns values to the constructor. This is an alternative to using the @Autowired annotation in properties.

@Component
public class VehicleService {
	Vehicle vehicle;

	@Autowired
	public VehicleService(Vehicle vehicle) {
		this.vehicle = vehicle;
	}
}


@Autowired in setter methods

In the spring boot, the @Autowired annotation is used in setter methods to inject the value of the class properties. When the bean is loaded in the ApplicationContext, the setter method is automatically called by the spring boot and the value is assigned.

@Component
public class VehicleService {
	Vehicle vehicle;

	@Autowired
	public void setVehicle(Vehicle vehicle) {
		this.vehicle = vehicle;
	}

	public Vehicle getVehicle() {
		return this.vehicle;
	}
}


@Autowired in @Bean creation

The @Autowired annotation is used in class methods while creating a bean using the @Bean annotation. In the method argument, the value is passed automatically.

@Configuration
public class VehicleFactory {
	@Bean
	@Autowired
	public Vehicle getVehicle(Car car) {
		return car;
	}
}
@Component
public class Car implements Vehicle{
}

public interface Vehicle {
}


@Autowired with @Qualifier on conflicts

If two or more classes of interface are used, the @Autowired annotation can not inject an implemented class for the interface. The @Autowired annotation will conflict with two classes to choose one to inject. The @Qualifier annotation specifies the name of the bean that is injected into the bean’s auto-wiring system.

@Component
public class VehicleService {
	@Autowired
	@Qualifier("car")
	Vehicle vehicle;
}
@Component("car")
public class Car implements Vehicle{
}
@Component("bike")
public class Bike implements Vehicle{
}

public interface Vehicle {
}


@Autowired with @Qualifier as method arguments

In the spring boot, the @Autowired annotation must be used in the constructor with more than one argument. Each argument needs to load a bean different from the ApplicationContext spring boot. The annotation can not be specified in the constructor, as only one bean name can be specified. The method argument level annotation @Qualifier will solve this problem.

@Component
public class VehicleService {
	Vehicle car;
	Vehicle bike;
	
	@Autowired
	public VehicleService(
			@Qualifier("car") Vehicle vehicle1, 
			@Qualifier("bike") Vehicle vehicle2) {
		this.car = vehicle1;
		this.bike = vehicle2;
	}
}


@Autowired with required=false (Safe auto wiring)

In the spring boot, the beans are loaded at the start of the application. In some cases, due to dynamic loading, some beans are not loaded. In this case, the @Autowired annotation could not find any bean to be injected. It’s going to throw an exception saying there’s no bean available to inject.

expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

The @Autowired annotation should be optional enough to inject the bean if the bean is available. If the bean is not available, it should not show any error and should simply be ignored without injection. This can be achieved by using the @required parameter in the @Autowired annotation

@Component
public class VehicleService {
	@Autowired (required = false)
	Vehicle car;
}



Leave a Reply