Benefits
- The creation code of the dependent class is removed from the dependency class without explicitly writing new.
- The creation of dependent classes can be maintained separately.
Facilitates the sharing of this class.
If the class is initialized with a lot of properties, using configuration is much simpler than hard code writing.
For example, when DataSource configures a database connection pool.
- When there are multiple layers of dependencies, the removal of dependencies actually simplifies the view and maintenance of dependencies.
Because for each class, you only need to be concerned about the dependency of this class.
In the case of Web projects, the controller layer only needs to care about the service layer, and the service layer only needs to care about the DAO layer.
It is possible to implement logic in a method by using more than n DAO in a service.
But the controller simply refers to this service and calls this method.
Benefits of Dependency Injection