O'Reilly logo

Hands-On High Performance with Spring 5 by Dinesh Radadiya, Prashant Goswami, Pritesh Shah, Subhash Shah, Chintan Mehta

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Setter-based DI

In a constructor-based DI, we saw a dependent object injecting through a constructor argument. In a setter-based DI, the dependent object is provided by a setter method in the dependent class. Setter-based DI is accomplished by calling setter methods on beans after invoking no-args constructors through the container.

In the following code, we show how to use a setter method for injecting a CustomerService object in the BankingService class:

@Componentpublic class BankingService {  private CustomerService customerService;    // Setter-based Dependency Injection  @Autowired  public void setCustomerService(CustomerService customerService) {  this.customerService = customerService;  }  public void showCustomerAccountBalance() { customerService.showCustomerAccountBalance(); ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required