Dependency Injection (DI) in Spring

The role of IoC: reduce the dependencies (coupling) between programs.
Dependency is the need to use objects of other classes in the current class, provided by spring for us, we only need to explain in the configuration file. Dependency management will be handed over to spring for maintenance. Dependency maintenance is called dependency injection.

Dependency injection:
There are three types of data that can be injected:

  • Basic types and String
  • Other bean types (beans configured in configuration files or annotations)
  • Complex type / collection type

There are three ways to inject:

  • Use constructor to provide
  • Use the set method to provide (commonly used)
  • Use annotations to provide

Example:
Using constructor to perform dependency injection in
Spring Using set method to perform
dependency injection

Published 56 original articles · praised 0 · visits 527

Guess you like

Origin blog.csdn.net/qq_41242680/article/details/105610649