Spring IOC
February 28, 2008 by muralis
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).
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.
Why is Spring Framework needed anyway?
The main aim of Spring is to make J2EE easier to use and promote good programming practice.It does not reinvent the wheel but makes existing technologies easier to use. The main advantages of Spring framework are enumerated as given below:-
-No matter whether you use EJBs or Struts Framework or any other framework for writing business objects, Spring organizes your business objects in an effective manner with configuration management services on any runtime environment.You can keep one calling mechanism for your business objects while changing the implementation technology of them altogether.
-Spring allows you to get rid of EJBs,if one wants to,no more compulsory to have,with alternative technologies like POJOs for building business objects and AOP provides a way to handle declarative transaction management, making EJB container absolutely not required.
-Increased development productivity
-Increased runtime performance
-Improving test coverage as unit testing of code can easily be done.
-Improving application quality
-Easier manageability of code in Integration technology.
-A consistent data access mechanism either using JDBC or O/R mapping techniques.
-Spring is portable between application servers which avoids anything platform-specific or non-standard in the developer’s view, and most of the application servers.