Spring IOC, Here IOC will consist of a container. The container will have core container and a J2EE container. Core container is BeanFactory container and J2EE container is ApplicationContext and Configurable ApplicationContext. If the IOC container is the core container, then IOC will do the following things.
The container can create instances of POJO classes.
The Container can manage the life cycle of a POJO.
The container can do dependency injection to the POJO.
Conceptual
figure
IOC image as follows.
BeanFactory Container
Description
BeanFactory Container provides supports to dependency injection. In Bean factory, a developer needs to write Bean.xml, this xml file should must have DTD and XSD to validate the tag. The role of DTD AND XSD is check whether there are invalid tags are not in the xml file. Instead of using the correct tags if the developer uses invalid tags then it becomes well formed but not valid.
While loading the bean file IOC container will have SAX parsing. Bean file will be defined by the SAX parser. If any tags are invalid in the XML file then SAX will give a parsing exception at the time of loading.
The developer will configure bean definitions with bean id and bean class. By the request of the user only IOC container creates the instance of the bean with getBean () method. While the request of other users only same instance will be given to them also where the scope of the bean is a singleton.
Example of BeanFactory is as follows.
[java]Resource resource=new ClassPathResource("applicationContext.xml");
BeanFactory factory=new XmlBeanFactory(resource); [/java]
Where
XmlBeanFactory is the implementation class of the BeanFactory interface.
To use the BeanFactory developer needs to create the instance XmlBeanFactory class.
ApplicationContext Container
Description
In ApplicationContext Container developer has to create an instance of the bean at the time of the loading IOC at the scope of singleton. Here also xml file will be checked by the IOC.
In the scope of the prototype instance will not be created by IOC, if developer use prototype needs to create an instance for each user request.
Example of ApplicationContext Container as shown follows.
[java]ApplicationContext context =
new ClassPathXmlApplicationContext("applicationContext.xml");[/java]
ClassPathXmlApplicationContext class is the implementing class of ApplicationContext interface.
To use the ApplicationContext developer needs to create the ClassPathXmlApplicationContext class.
Dependency Injection
Description
Dependency Injection is a technique which helps to inject dependent objects of a class. Dependency injection makes programming code loosely coupled. Two types Dependency injections are as follows.
Constructor Method
Setter Method
Constructor Method
The constructor method of dependency injection is a method of injecting the dependencies of an object through its constructor object.
Every object will have constructor to pass the arguments, Java will have default constructor added to the class. This constructor does not take any argument values, in spring it is happening that class will start without passing any arguments.
Setter Method
The setter method injection approach of dependency injection is the method of injecting the dependencies of an object by using the setter method. Here no arguments will be passed.
Dependency Lookup
Description
In dependency lookup resource explicitly searches and gathers dependent values from others resource. Examples for DL are as follows.
If a student gets course book only after passing request for it then is called dependency look up.
The way EJB or Servlet component gets JDBC data source object from registry through JDDI lookup operation is called dependency lookup.
Advantage
Spring IOC - The resource can gather only required dependent values.
Disadvantage
Spring IOC - The resource should spend some time to gather dependent values before utilizing them.
Problems
Tightly coupling:DL makes the code tightly coupled. If any resource changed developer needs to rectify the lot of code.
Testing:DL creates the problem specially in black box testing.
Summary
Key Points
Spring IOC - In dependency injection resource can use to inject dependent values directly without spending time to get them.
Spring IOC - Dependency injection makes programming code loosely coupled.