Common ways to compare dependency injection in Spring

Source: Internet
Author: User

Broadly divided into: Field injection, construction injection, setter injection

Where Field injections are considered harmful:

1. Violation of the single principle

When there are multiple dependencies in a class, you don't see a lot of dependencies if you just use Field injection. By using the construct injection \setter injection, you can see that I usually refactor the code.

2. Rely on hidden

With the DI container, the class itself no longer manages its dependencies, but it should explicitly show dependencies through the public interface method \ constructor.

3. DI Container Coupling

Because dependencies are ambiguous, they cannot be instantiated independently of the container.

Reference documents

Https://stackoverflow.com/questions/39890849/what-exactly-is-field-injection-and-how-to-avoid-it

https://www.vojtechruzicka.com/field-dependency-injection-considered-harmful/

Common ways to compare dependency injection in Spring

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.