Inversion Of Control In Spring :

RAKESH KUMAR SINGH
4 min readJun 13, 2021

When we were using Java EE for developing web pages then for creating any object of any class we were using new keyword but let suppose our application has to many classes then managing all objects and classes was not easy, then spring comes up with a principle/pattern by which the control of objects(creation, destruction …etc), is transferred to a framework. Which means that the act of connecting objects with other objects, or “injecting” objects into other objects, is done by a framework rather than by the objects themselves. It is called inversion of control.

IOC container in Spring Framework:

We know in IOC control goes to spring for creating and managing object but for this first we need to configure IoC container. We need to define bean in the container so that we can use it later. Just like a real life container, empty Spring IoCC is of no usage. So we need to add, store some things/components in it for later usage. The process of filling IoC container with the components you need is called “Configuration” process. The Components which will be stored inside are “Spring beans”. These beans are just simple POJO(Plain Old Java Object)-s that go through this configuration process and are created, assembled (dependencies are
injected), initialized, and managed by the Spring IoC container.

There are just two steps for configuring your objects to become Spring managed beans.

  • You write your class in “Plain Old Java Object” style like you used to( with no args. constructor and setters, getters of all the properties ).
  • You add these classes in IoCC by providing some kind of a marker, also known as a configuration metadata. When an application starts, Spring crawls through all the classes in packages and understands that it has to manage the instances of the marked ones, thus adds them in the IoC Container.

We learn that we need to configure spring container. We can configure it in three ways -

XML Configuration -

In XML file we need to provide class detail in <bean> tag where we define id of bean and class name as -

<bean id=”id” class=”className”></bean>

In the given bean tag we can specify a particular setter method for setting any variable. In constructor-arg tag inside ref attribute we can specify aany setter method for setting dependency.

Annotation based Configuration -

Let’s suppose in our application there is many beans then configuring all the beans in xml file will not easy then we can do annotation based configuration. There are few steps by which we can do annotation based configuration :

step -1 : Enable component scanning in spring config file. Basically we will tell the spring that search bean in package not in xml config file as -

<context: component-scan base package=”basePackageName” />

step — 2 : Add the @Component(“classId”) annotation to java class which is stored inside given base package.

step -3 : Retrieve bean from spring container.

Java based Configuration -

We can configure spring container by pure java code without xml.

step -1 : Create a java class and annotate as @Configuration.

step -2 : We can add component scanning support by using @ComponentScan. We will mention base package name in ComponentScan so that spring can search classes in that package.

step -3 : Retrieve beans from spring container.

Dependency Injection :

We know in spring object is created by spring and will stored in IoC container but the thing is let’s suppose there is a class A which depends on class B. When object of class A will created then dependency will also injected by spring itself we as a programmer don’t need to worry about this is called dependency injection. Now we will learn how dependency will injected -

By using xml configuration :

In XML config if we define a bean and if there is any dependency in that bean then inside that bean we insert a property tag. We can do it in two ways -

a. By using constructor of dependent class -

<bean id=”bean_id” class=”class_name” >

<constructor-arg index=”0" ref=”id_of_injected_class”></>

</bean>

b. By using setter method -

<bean id=”bean_id” class=”class_name” >

<property name=”attribute” ref=”id_of_injected_class”></>

</bean>

In depend class it will search for setAttribute() function and send the reference of mentioned class in ref .

Dependency Injection using Annotation & Autowiring -

We can inject dependency by autowiring in three ways -

  1. Constructor Injection
  2. Setter Injection
  3. Field Injection

For injecting dependency by autowiring annotation we can follow these three steps -

step -1: Define the dependency interface and class.

step -2 : Create a constructor or setter method in our class.

step -3: Configure the dependency injection by @Autowired annotation.

In field injection method we don’t need to create constructor or setter method. We simply put @Autowired before reference variable and dependency will injected.

--

--