Spring Core

Suvam Banerjee
3 min readDec 13, 2020

Inversion of Control

This is a principle by which control and management of the object are transferred to the framework. IOC enables the framework to take control of the flow of the program, we just need to provide a configuration file.

Inversion of control can be achieved by different techniques one of them is Dependency injection(DI)

Dependency Injection

Using this technique a developer is not responsible to provide dependency to a class. DI automatically inject required dependency to the class. The best example to understand dependency injection will be, suppose we want to create a vehicle but a vehicle is a combination of parts like engine, tires, metal body, etc, and lot many dependencies are there, we just need to ask di to provide all dependency rather than creating all and injecting them manually. DI will create, manage, and inject it automatically.

There are many ways we can implement Dependency Injection in spring by configuring web.xml file or by using annotations, known as Autowiring.

  • As XML configuration is deprecated so we will discuss Autowiring

@Autowiring

  • IOC container will look for a class that matches the property, like match by class or interface.
  1. Constructor Injection:

Steps are, a)Define dependency class or interface

b)Create the argument constructor in the targeted class

c)Configure constructor with @Autowire

eg: public class Bus implements Engine{

Engine engine;

@Autowired

public Bus(Engine engine){

this.engine=engine;

}

  • Spring will check the argument of constructor based on which it will inject an object which implements Engine but if multiple class implements engine then spring will be in an ambiguous position, so to avoid that we can use @Qualifier(“class name”), now spring will inject specified class.
  • Same like Constructor we can use @Autowire with the setter method

2)Field Injection: The easiest way for dependency injection here we just need to specify the property name even in private scope and add @Autowired above it, IOC container will find the required object and assigned it. Internally field injection uses the Java reflection technique.

eg: public class Bus implements Engine{

@Autowired

Private Engine engine;

… other methods … }

The Spring IoC Container

This is the core of the spring framework, IOC containers will create the required object, wire them together, manage their life cycle until destruction. The container uses Dependency injection to manage all objects these objects are known as spring beans. Container scan for bean and create an object of it and inject it if required. We can explicitly define beans in web.xml with their bean id or allow auto-scanning and decorate the required class with @Component, IOC will automatically scan them and give a bean id internally. The container gets its instructions on what objects to instantiate, configure, and assemble by scanning the configuration data provided. The configuration metadata can be represented either by XML, Java annotations, or Java code ie POJO classes. The Spring IoC container makes use of Java POJO classes and configuration metadata to produce a fully configured and executable system or application. IOC container is represented by ApplicationContext which is an interface and its implementation classes are ClassPathXMLApplicationContext, AnnotationConfigApplicationContext, FileSystemXMLApplicationContext using any of these objects and by calling the get method we can get bean of our requirement.

Lazy Beans Initialized

By default, the IOC container creates only one object of that bean, and all get() calls are referred to that object. The advantage of this is faster initialization time, we can alter this by using @Scope(“prototype”), on every get() call a new object will be created, or by default scope is Singleton.

--

--