Saturday, January 30, 2021

Types of Spring @Autowired

 We can implement dependency injection with:

https://www.baeldung.com/properties-with-spring

https://reflectoring.io/constructor-injection/

  • constructor-based injection
@Component
class Sandwich {

  private Topping toppings;
  private Bread breadType;

  @Autowired
  Sandwich(Topping toppings, Bread breadType) {
    this.toppings = toppings;
    this.breadType = breadType;
  }

}

  • setter-based injection
@Component
class Cookie {

  private Topping toppings;

  @Autowired
  void setTopping(Topping toppings) {
    this.toppings = toppings;
  }
}

  • field-based injection.
@Component
class Pizza {

  @Autowired
  private Topping toppings;
}

No comments:

Post a Comment

Azure - Pipeline - Add Approver for Stage

https://learn.microsoft.com/en-us/azure/devops/pipelines/process/approvals?view=azure-devops&tabs=check-pass