Sunday, October 14, 2018

Spring IoC Container with XML configuration

What is IoC Container ? 

The org.springframework.context.ApplicationContext interface represents the Spring IoC container. This container instantiates, configures, and assemble beans by using externally provided configuration metadata.
ApplicationContext and BeanFactory are the same, but the ApplicationContext adds more enterprise-related functionality. In short, the ApplicationContext is a superset of the BeanFactory.
"Control of the managing bean and it's lifecycle" is part of the container and not the programmers, which is why it is named the "Inversion of Control."

What is Configuration metadata ?

Configuration metadata represents how you, as an application developer, tell the Spring Container to instantiate, configure, and assemble the objects in your application.
This configuration metadata is nothing but bean definitions. BeanDefinitions are provided in the form of an XML file, annotations, or a Java config class.
Forms of metadata with the Spring container are: XML-based configuration, annotation-based configuration, and Java-based configuration.                 
The following image represents how IoC containers work with the configuration metadata.
Image title

Before having deep dive into the configuration, we will understand a couple of concepts at a very high level.

DI (Dependency Injection) :

DI is a design pattern and is the act of connecting objects with other objects, or "injecting" objects into other objects. Dependency injections are performed by an assembler rather than by the objects themselves.
                   "Having DI is opposite to having hardcore dependency".
There are mainly two type of DI : Constructor injection and Setter Injection.

Ways to Define a Bean in an XML-Based Configuration

Since the application developer provides the configuration of metadata to the IoC container in the form of an XML file, we can define one or more bean definition in this configuration file. In this configuration, bean is defined as the <bean/> element inside a top-level <beans/> element.
Configuration signatures of bean definition (Constructor Injection): 


 The <constructor-arg /> element defines the constructor argument and does so using number of attributes, but 'ref' is the most common, and it is used to tell the container that the value of this attribute is reference to another bean.



Another attribute that is commonly used is 'value'. This attribute is used when the value to inject is a scalar.

Examples : 

1. We can define bean with no "id" and "name" attributes. Container generates bean name by its own as below.










In the above code, the generated name is com.kasegvikas.model.ThingOne#0.

2. Some different combinations of bean definitions with examples.

beans.xml :

Main class :
Output : 
Source Code :
Above examples are available on GitHub.

Conclusion : 
The Spring IoC container with DI and configuration metadata all work together to introduce the best way for de-coupled and fast application development. This makes the application developer's life easy and takes care of the bean's lifecycle.

1 comment: