Header Ads

  • Breaking Now

    What do you understand by Inversion of Control/Dependency Injection?

    Spring is an Inversion of Control container through its bean factory concept.IoC helps in loose coupling of the code .Spring is most closely identified with a flavor of Inversion of Control known as Dependency Injection(DI)--a name coined by Martin Fowler, Rod Johnson and the PicoContainer team in late 2003.DI is an old concept which caught the fancy of Java EE community not so long ago.DI is quite useful in test driven development and avoiding dependencies on other collaborating objects helps in building unit tests of objects behavior that is under test.

    It follows famous Hollywood principle "Don't call me.I will call you". IoC moves the responsibility for making things happen into the framework, and away from application code. Whereas your code calls a traditional class library, an IoC framework calls your code. It segregates the calling mechanism of methods from actual implementation.

    Dependency Injection is a form of IoC.In DI an object uses the other object to provide a specific functionality.It removes explicit dependence on container APIs.The ordinary Java methods are used for injecting dependencies by collaborating objects or configuration values into application object instances. With Dependency Injection the container figures out that a 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.

    DI can be accomplished either by Setter Injection or Constructor Injection and Spring supports both.

    Some advantages of DI:

    -No need for look up code at runtime, hence simpler to write and maintain. JavaBean setter method or constructors arguments are used to introduce DI in Spring.

    -Easier testing of JavaBeans through JUnits.

    -Spring framework provides strongly typed dependencies which a developer needs not be bothered about and type mismatches are raised as errors when the framework configures the application.

    -With a DI approach, dependencies are explicit, and evident in constructor or JavaBean properties.

    -Spring is non invasive that means using it won't invade your code with dependency on its APIs.No dependency on container APIs of most business objects. Any third party code or any POJO can be introduced as a Spring Bean.

    -Spring BeanFactories are very lightweight,can be used inside applets, as well as standalone Swing applications,work fine withing EJB containers as well.

    Post Top Ad

    Post Bottom Ad