Understanding Spring Boot @Autowired Annotation

In Spring Boot, @Autowired is an annotation that allows automatic dependency injection of beans. It is used to inject an instance of a class into another class.

What is @Autowired in Spring Boot

@Autowired is an annotation used in Spring Boot to enable automatic dependency injection. It allows the Spring container to provide an instance of a required dependency when a bean is created. This annotation can be used on fields, constructors, and methods to have Spring provide the dependencies automatically. Dependency injection is a design pattern in which objects are passed their dependencies rather than creating them internally. In Spring Boot, @Autowired is used to inject objects into other objects. This allows for loose coupling between components and helps to keep code more maintainable.

Example of @Autowired

Let’s look at an example of how to use the @Autowired annotation.

We have a CarService class that needs an EngineService instance to function properly. This can be injected into the CarService class using the @Autowired annotation.

@Service
public class CarService {

    @Autowired
    private EngineService engine;

    public void drive() {
        engine.start();
    }

}

The @Autowired annotation specifies that the Spring container should supply the EngineService to the CarService class. When the container creates the CarService instance, it will automatically inject the EngineService instance into the CarService class.

Note that in order for @Autowired to work, you need to have properly configured the Spring Boot application context with the required beans defined. You can define beans by annotating classes with @Component, @Service, @Repository, @Controller, or @Bean.

Spring Boot Autowired

Using @Autowired in Private Fields

Private fields can be annotated with @Autowired to have Spring inject a dependency into the field.

Here Spring uses reflection to access internally to the class and inject the component into the field.

@Service
public class CarService {

    @Autowired
    private EngineService engine;

    public void drive() {
        engine.start();
    }

}

Using @Autowired in Constructors

Constructors can also be annotated with @Autowired to have Spring inject the dependency into the class.

Inject by constructors is the recommended use over the others ways to use Autowired.

@Service
public class CarService {

    private final EngineService engine;

    @Autowired
    public CarService(EngineService engine) {
        this.engine = engine;
    }

    public void drive() {
        engine.start();
    }

}

Also in this way:

@Service
public class CarService {

    private final EngineService engine;

    public CarService(@Autowired EngineService engine) {
        this.engine = engine;
    }

    public void drive() {
        engine.start();
    }

}

Using @Autowired in Set Methods

Set methods can also be annotated with @Autowired to have the dependency injected into the class by Spring.

@Service
public class CarService {

    private EngineService engine;

    @Autowired
    public void setEngine(EngineService engine) {
        this.engine = engine;
    }

    public void drive() {
        engine.start();
    }

}

The ‘required’ @Autowired parameter

By default, with the @Autowired annotation the dependency is required. But we could indicate that it is NOT required, so Spring will leave the dependency null.

@Service
public class CarWithNotRequiredService {

    @Autowired(required = false)
    private EngineService engine; // This could be null !

    public void drive() {
        if (Objects.isNull(engine)) {  // prevent null pointer
            System.out.println("Engine is null!");
        }else {
            engine.start();
        }
    }

}

Conclusion

@Autowired is an annotation used in Spring Boot to use automatic dependency injection in classes that need others components.
It allows the Spring container to provide an instance of a necessary dependency when a component is created. This annotation can be used on fields, constructors, and methods, as well as with the “required” attribute to indicate whether a dependency is needed or not.

This code is in GitHub Spring Boot Code examples

Hi! If you find my posts helpful, please support me by inviting me for a coffee :)

See also