Wednesday, March 15, 2017

Spring IoC Container

In this post we will discuss and learn the overview of Spring IoC Container.The Spring Container will create the objects,write them together,configure them and manage their complete life cycle from creation till destruction.The IoC container gets information from the XML and works accordingly. The IoC container is the main component of the Spring framework. The core container is responsible for providing essential functionality to the Spring Framework.Spring's container used Dependency Injection to manage the components that make up an application.

The Spring IoC Contains mainly Two types they are:


  • BeanFactory
  • ApplicationContext
 The org.springframework.beans and org.springframework.context packages provide the basis for the spring framework's IoC container. 

BeanFactory:

BeanFactories are the simplest of containers,providing basic support for Dependency Injection.The org.springframework.beans.factory.BeanFactory is the actual representation of the Spring IoC container that is responsible for containing and otherwise managing the beans.

ApplicationContext:

The ApplicationContext interface builds on top of the BeanFactory(is is a sub-interface) and adds other functionality such as easier integration with spring's AOP features,message resources handling(i18N),event propagation,and application-layer specific contexts such as webApplicationContext for use in the web applications.

The ApplicationContext container includes all functionality of the BeanFactory container,so it is generally recommended over BeanFactory. BeanFactory is used when we develop applications like mobile devices or applet based applications.That means we used BeanFactory for light weight applications.

IoC is also known as dependency injection (DI). There are 3 forms of DI: They are


  1. Constructor Injection
  2. Setter Injection
  3. Interface Injection
Constructor Injection:
In this approach , an IoC container uses the constructor to inject the dependency. All the dependencies (simple or references) are declared in the constructor. One of the advantages of Constructor Injection is that all the dependencies are declared in one step. This also helps in understanding whether the class depends on too many services.

Through the Spring beans XML file you can configure your bean to initialize with an argument for the constructor, and then assign the arguments. Spring essentially “injects” the argument into your bean. This is referred to as constructor injection.

The following example passes in the String message using a constructor. The class is the same as the one in Basic Bean Creation except the default message on the message variable has been cleared and is now set to null. A single parameter constructor has been added to set a message.



Example:
public class ConstructorMessage {
private String message = null;
/**
* Constructor
*/
public ConstructorMessage(String message) {
this.message = message;
}
/**
* Gets message.
*/
public String getMessage() {
return message;
}

/**
* Sets message.
*/
public void setMessage(String message) {
this.message = message;
}

}

ConstructorMessageTest-context.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="message"
class="org.learnprogramingbyluckysir.di.xml.ConstructorMessage">
<constructor-arg value="Spring is fun." />
</bean>

</beans>


Setter Injection:
In this approach Dependency Injection uses Setters to inject the required resources or dependencies. Each of the objects that the class depends upon will have a setter and the IoC container will use the setters to provide the resource at run-time.

Spring uses the set method names to infer the property name, the naming of your set methods should follow the JavaBeans Spec, or at least be consistent within the confines of your application. See the example below for basic setter injection on our Message class

Example:
public class SetterMessage {
private String message = null;
/**
* Gets message.
*/
public String getMessage() {
return message;
}
/**
* Sets message.
*/
public void setMessage(String message) {
this.message = message;
}

}

The property element is used to define the setter injection:
SetterMessageTest-context.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="message"
class="org.learnprogramingbyluckysir.di.xml.SetterMessage">
<property name="message" value="Spring is fun." />
</bean>

</beans>


Interface Injection:

So far we have only injected constructor and property values with static values, which is useful if you want to eliminate configuration files. Values can also be injected by reference -- one bean definition can be injected into another. To do this, you use the constructor-arg or property's ref attribute instead of the value attribute. The ref attribute then refers to another bean definition's id.

In the following example, the first bean definition is a java.lang.String with the id springMessage. It is injected into the second bean definition by reference using the property element's ref attribute.

ReferenceSetterMessageTest-context.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="springMessage"
class="java.lang.String">
<constructor-arg value="Spring is fun." />
</bean>
<bean id="message"
class="org.learnprogramingbyluckysir.di.xml.SetterMessage">
<property name="message" ref="springMessage" />
</bean>
</beans>

Some Interview Questions:

1) What is difference between BeanFactory and ApplicationContext?

Ans: BeanFactory is the basic container whereas ApplicationContext is the advanced Container. ApplicationContext extends the BeanFactory interface. ApplicationContext provides more facilities than BeanFactory such as integration with AOP,message resources handling for i18N etc..

2) What is difference between Constructor Injection and setter injection?

Ans: 

3) What is the role of IoC container in spring?

Ans:
  1. Create the instance
  2. configure the instance,and
  3. assemble the dependencies


See also:

Spring MVC Architecture
Spring Interview Questions


No comments:

Post a Comment

High Paying Jobs after Learning Python

Everyone knows Python is one of the most demand Programming Language. It is a computer programming language to build web applications and sc...