June 2018
Intermediate to advanced
408 pages
11h 23m
English
In the preceding sections, we saw how we can use constructor-based and setter-based dependencies in our application. In the following example, we will see field-based DI. Actually, field-based DI is easy to use, and it has clean code compared to the other two types of injection method; however, it has several serious trade-offs, and should generally be avoided.
Let's look at the following example of a field-based DI. In the following code, we will see how to use a field for injecting a CustomerService object in the BankingService class:
@Componentpublic class BankingService { //Field based Dependency Injection @Autowired private CustomerService customerService; public void showCustomerAccountBalance() { customerService.showCustomerAccountBalance(); ...Read now
Unlock full access