Springs - SPLessons
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Spring IOC

Spring IOC

shape Description

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.

shape Conceptual figure

IOC image as follows.

BeanFactory Container

shape 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

shape 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

shape 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

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

shape Description

In dependency lookup resource explicitly searches and gathers dependent values from others resource. Examples for DL are as follows.

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

Summary

shape 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.