In this article, we will learn @Autowired
annotation to inject the dependency
automatically
using
Constructor
injection, Setter injection, and Field
injection.
The @Autowired
annotation is used to inject the bean automatically.
The @Autowired
annotation is used in Constructor
injection, Setter
injection, and Field injection.
Let's create Interfaces and classes required to demonstrate @Autowired annotation.
package net.javaguides.springboot.service;
public interface Pizza {
String getPizza();
}
package net.javaguides.springboot.service;
import org.springframework.stereotype.Component;
@Component
public class VegPizza implements Pizza{
@Override
public String getPizza() {
return "Veg Pizza";
}
}
package net.javaguides.springboot.service;
import org.springframework.stereotype.Component;
@Component
public class NonVegPizza implements Pizza{
@Override
public String getPizza() {
return "Non-veg Pizza";
}
}
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.
@Autowired
public PizzaController(@Qualifier("vegPizza") Pizza pizza) {
System.out.println("inside PizzaController constructor");
this.pizza = pizza;
}
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);
}
}
calling pizzaController.getPizza()
Veg Pizza
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.
@Autowired
@Qualifier("vegPizza")
public void setPizza(Pizza pizza) {
this.pizza = pizza;
}
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);
}
}
calling pizzaController.getPizza()
Veg Pizza
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.
@Autowired
@Qualifier("vegPizza")
private Pizza pizza;
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);
}
}
calling pizzaController.getPizza()
Veg Pizza
In this article, we learned @Autowired
annotation to inject the dependency
automatically using Constructor injection, Setter
injection, and Field injection.