Spring IOC Container Configuration

hemasai jammana
3 min readMar 26, 2023

--

As we have already discussed the spring framework in the earlier posts
https://link.medium.com/7WXTppdGoyb, now we will be discussing configuring the spring IOC container.

Spring

Spring provides 2 containers
1. Bean factory
2. Application Context.(latest one)

containers perform the following operations:
1. Managing the bean LifeCyle
2. Dependency Management(Dependency Injection)

Now we will have a doubt What is Bean?
Any object in your application is called a bean it is instantiated and managed by the spring container.

In the above image, Spring Container will manage the objects of student and it will instantiate where ever requires.

In the same way, we will have a doubt about what is Dependency Management.
Dependency Management is a process of assigning the dependent objects to the target object by loading both classes and by creating the objects for both classes.
The classes/objects which use the other class services are called target objects
The classes/objects which acts like helper class to target objects are called dependent objects.

public class College {  // target class
private Student student; // dependent class
}

In the above code before creating an object for a college class, spring will create an object for a student and assign it to a particular college object.

Dependency Injection.

We will see more about dependency injection in later posts.

Configuring the container involves the below steps:
- Create a configuration file
- Define beans
- Specify dependencies
- Add configuration annotations
- Load the configuration file

To perform dependency injection we need to provide configuration metadata to the spring IOC container.
There are 4 different ways of configuring metadata
1. XML-based approach.
2. Annotation-driven configuration.
3. Using java code configuration.
4. Spring boot AutoDriven configuration.

  1. XML-based approach:
    In this type of approach, we will use one XML file to configure the container and define the beans and specify the dependent beans.
<?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="..." class="...">
<!-- collaborators and configuration for this bean go here -->
</bean>

<bean id="..." class="...">
<!-- collaborators and configuration for this bean go here -->
</bean>

<!-- more bean definitions go here -->

</beans>

2. Annotation-driven configuration:
In this approach, we will configure the container by using annotations like Required and Autowired such that the spring container will do the rest.

3. Using java code configuration.
In this approach, we will configure the container by using Configuration and Bean annotations and we need to provide a configuration file that is a little similar to the XML file.

4. Spring boot AutoDriven configuration.
In this approach, Spring boot automatically configures our spring application based on the dependent jars we have added. This is done by using one of the annotations EnableAutoConfiguration or SpringBootApplication. we will see more about this in later posts.

Please do follow if you are interested to learn with me.

--

--

hemasai jammana

Software Engineer, learning and exploring new technologies.