Wednesday, March 29, 2017

Spring - Inversion of control and dependency injection


The concept behind Inversion of Control (IoC) is often expressed in the Hollywood Principle: "Don't call me, I'll call you.". IoC moves the responsibility for making things happen into the framework, and away from application code.

Dependency Injection (DI) is a form of IoC that removes explicit dependence on container APIs; ordinary Java methods are used to inject dependencies such as collaborating objects or configuration values into application object instances. Where configuration is concerned this means that while in traditional container architectures such as EJB, a component might call the container to say "where's object X, which I need to do my work", with Dependency Injection the container figures out that the component needs an X object, and provides it to it at runtime. The container does this figuring out based on method signatures (usually JavaBean properties or constructors) and, possibly, configuration data such as XML.

No comments: