🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
In this article, we will learn @Autowired annotation to inject the dependency automatically using Constructor injection, Setter 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 PizzaSetter 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; // } // 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
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 PizzaField 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 PizzaConclusion
In this article, we learned @Autowired annotation to inject the dependency automatically using Constructor injection, Setter injection, and Field injection.
Related Spring and Spring Boot Annotations
- Spring Boot @Bean Annotation Example
- Spring @Qualifier Annotation Example
- Spring @Autowired Annotation with Example
- Spring @Bean Annotation with Example
- Spring @Configuration Annotation with Example
- Spring @PropertySource Annotation with Example
- Spring @Import Annotation with Example
- Spring @ImportResource Annotation Example
- Spring - @Lazy Annotation Example
- Spring - @Primary Annotation Example
- Spring @PostConstruct and @PreDestroy Example
- Spring @Repository Annotation
- Spring @Service Annotation
- The Spring @Controller and @RestController Annotations
- Spring Boot @Component, @Controller, @Repository and @Service
- Spring @Scope annotation with Prototype Scope Example
- Spring @Scope annotation with Singleton Scope Example
- Spring Boot @PathVariable
- Spring Boot @ResponseBody
- Spring @RequestBody - Binding Method Parameters to Request Body
- Spring Boot @ResponseStatus Annotation
- Spring Boot - Creating Asynchronous Methods using @Async Annotation
- @SpringBootTest Spring Boot Example
- @SpringBootTest vs @WebMvcTest
- @DataJpaTest Spring Boot Example
- Spring @PostConstruct and @PreDestroy Example
- Spring @GetMapping, @PostMapping, @PutMapping, @DeleteMapping and @PatchMapping
- Spring Boot @EnableAutoConfiguration Annotation with Example
- Spring Boot @SpringBootApplication Annotation with Example
Comments
Post a Comment
Leave Comment