
How Dependency Injection works
Traditionally, each object is responsible for obtaining its own references to the objects it collaborates with (its dependencies). This can lead to highly coupled and hard-to-test code.
E.g.
1 |
public class implements Knight{ |
With DI, objects are given their dependencies at creatiion time by some third party that coordinates each object in the system. Objects aren’t expected to create or obtain their dependencies.
1 |
public class BraveKnight implements Knight{ |
BraveKnight is given a quest at construction time as a constructor argument, instead of creating its own. This is a constructor injection.
What’s more, the quest he’s given is typed as Quest, an interface that all quests implement. So BraveKnight could embark on a RescueDamselQuest, a SlayDragonQuest, a MakeRoundTableRounderQuest, or any other Quest implementation he’s given.
The point is that BraveKnight isn’t coupled to any specific implementation of Quest. It doesn’t matter to him what kind of quest he’s asked to embark on, as long as it implements the Quest interface.
One of the most common ways a dependency is swapped out is with a mock implementation during testing
1 |
public class BraveKnightTest { |
Injecting a Quest into a Knight - Wiring
1 |
public class SlayDragonQuest implements Quest { |
SlayDragonQuest implements the Quest interface, making it a good fit for BraveKnight. But how can you give SlayDragonQuest to BrageKnight?
The act of creating associations between application components is commonly referred to as wiring.
Java-based configuration
1 |
|
Whether you use XML-based or Java-based configuration, the benefits are: although BraveKnight depends on a Quest, it doesn’t know what type of Quest it will be given or where that Quest will come from. This makes it possible to change those dependencies with no changes to the depending classes.
Seeing it work
1 |
public class JavaConfigurationMain { |
In a Spring application, an application context loads bean definitions and wired them together. The Spring application context is fully responsible for the creation of and wiring of the objects that make up the application.




近期评论