Spring @Autowired Annotation Example

In this article, we will learn @Autowired annotation to inject the dependency automatically using Constructor injection, Setter injection, and Field injection.

@Autowired Annotation Overview

The @Autowired annotation is used to inject the bean automatically.

The @Autowired annotation is used in Constructor injectionSetter injection, and Field injection.

YouTube Video

Constructor Injection using @Autowired Annotation

Let's create Interfaces and classes required to demonstrate @Autowired annotation.

Pizza Interface

package net.javaguides.springboot.service;

public interface Pizza {
    String getPizza();
}

VegPizza Class

package net.javaguides.springboot.service;

import org.springframework.stereotype.Component;

@Component
public class VegPizza implements Pizza{

    @Override
    public String getPizza() {
        return "Veg Pizza";
    }
}

NonVegPizza

package net.javaguides.springboot.service;

import org.springframework.stereotype.Component;

@Component
public class NonVegPizza implements Pizza{
    @Override
    public String getPizza() {
        return "Non-veg Pizza";
    }
}

PizzaController Class - Constructor Injection using @Autowired Annotation

package net.javaguides.springboot.controller;

import net.javaguides.springboot.service.Pizza;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

@Component
public class PizzaController {

    private Pizza pizza;

    @Autowired
    public PizzaController(@Qualifier("vegPizza") Pizza pizza) {
        System.out.println("inside PizzaController constructor");
        this.pizza = pizza;
    }

    public String getPizza(){
        return pizza.getPizza();
    }
}
In this above code snippet, we are using @Autowired annotation to inject VegPizza dependency in PizzaController class using constructor injection. Note that we are using @Qualifier annotation in conjunction with @Autowired to avoid confusion when we have two or more beans configured for the same type.

Constructor injection using @Autowired annotation:
    @Autowired
    public PizzaController(@Qualifier("vegPizza") Pizza pizza) {
        System.out.println("inside PizzaController constructor");
        this.pizza = pizza;
    }

Testing

Let's retrieve the PizzaController spring bean from the ApplicationContext and call its method:
package net.javaguides.springboot;

import net.javaguides.springboot.controller.PizzaController;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.util.Arrays;

@SpringBootApplication
public class SpringbootDockerDemoApplication {

	public static void main(String[] args) {
		var context = SpringApplication.run(SpringbootDockerDemoApplication.class, args);
		System.out.println("calling pizzaController.getPizza()");
		PizzaController pizzaController = context.getBean(PizzaController.class);
		String message = pizzaController.getPizza();
		System.out.println(message);
	}
}
Output:
calling pizzaController.getPizza()
Veg Pizza

Setter Injection using @Autowired Annotation

Let's use Setter Injection with @Autowired Annotation to inject VegPizza bean in PizzaController class:
package net.javaguides.springboot.controller;

import net.javaguides.springboot.service.Pizza;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

@Component
public class PizzaController {

    private Pizza pizza;

//    @Autowired
//    public PizzaController(@Qualifier("vegPizza") Pizza pizza) {
//        System.out.println("inside PizzaController constructor");
//        this.pizza = pizza;
//    }

    // setter injection
    @Autowired
    @Qualifier("vegPizza")
    public void setPizza(Pizza pizza) {
        this.pizza = pizza;
    }

    public String getPizza(){
        return pizza.getPizza();
    }
}
In this above code snippet, we are using @Autowired annotation to inject VegPizza dependency in PizzaController class using setter injection. Note that we are using @Qualifier annotation in conjunction with @Autowired to avoid confusion when we have two or more beans configured for the same type.

Setter injection using @Autowired annotation:

    @Autowired
    @Qualifier("vegPizza")
    public void setPizza(Pizza pizza) {
        this.pizza = pizza;
    }

Testing

Let's retrieve the PizzaController spring bean from the ApplicationContext and call its method:
package net.javaguides.springboot;

import net.javaguides.springboot.controller.PizzaController;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.util.Arrays;

@SpringBootApplication
public class SpringbootDockerDemoApplication {

	public static void main(String[] args) {
		var context = SpringApplication.run(SpringbootDockerDemoApplication.class, args);
		System.out.println("calling pizzaController.getPizza()");
		PizzaController pizzaController = context.getBean(PizzaController.class);
		String message = pizzaController.getPizza();
		System.out.println(message);
	}
}
Output:
calling pizzaController.getPizza()
Veg Pizza

Field Injection using @Autowired Annotation

Let's use Field Injection with @Autowired Annotation to inject VegPizza bean in PizzaController class:
package net.javaguides.springboot.controller;

import net.javaguides.springboot.service.Pizza;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

@Component
public class PizzaController {

    @Autowired
    @Qualifier("vegPizza")
    private Pizza pizza;

//    @Autowired
//    public PizzaController(@Qualifier("vegPizza") Pizza pizza) {
//        System.out.println("inside PizzaController constructor");
//        this.pizza = pizza;
//    }

//    // setter injection
//    @Autowired
//    @Qualifier("vegPizza")
//    public void setPizza(Pizza pizza) {
//        this.pizza = pizza;
//    }

    public String getPizza(){
        return pizza.getPizza();
    }
}
In this above code snippet, we are using @Autowired annotation to inject VegPizza dependency in PizzaController class using field injection. Note that we are using @Qualifier annotation in conjunction with @Autowired to avoid confusion when we have two or more beans configured for the same type.

Field injection using @Autowired annotation:

    @Autowired
    @Qualifier("vegPizza")
    private Pizza pizza;

Testing

Let's retrieve the PizzaController spring bean from the ApplicationContext and call its method:
package net.javaguides.springboot;

import net.javaguides.springboot.controller.PizzaController;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.util.Arrays;

@SpringBootApplication
public class SpringbootDockerDemoApplication {

	public static void main(String[] args) {
		var context = SpringApplication.run(SpringbootDockerDemoApplication.class, args);
		System.out.println("calling pizzaController.getPizza()");
		PizzaController pizzaController = context.getBean(PizzaController.class);
		String message = pizzaController.getPizza();
		System.out.println(message);
	}
}
Output:
calling pizzaController.getPizza()
Veg Pizza

Conclusion

In this article, we learned @Autowired annotation to inject the dependency automatically using Constructor injectionSetter injection, and Field injection.

Related Spring and Spring Boot Annotations

Comments