Spring IoC (Inversion Of Control) Containers

The basic concept of Inversion of Control (IoC) is that, programmer do not need to create the objects, instead just describe how it should be created. No need to directly connect your components and services together in program, instead just describe which services are needed by which components in a configuration file/xml file. The Spring IOC container is then responsible for binding it all up.

The Spring container is at the core part of the Spring Framework. The container will create the objects, tide them together, configure them, and manage their lifecycle from creation to destruction. The Spring container make use of dependency injection to manage the components of an application. These objects are called Spring Beans.

The container By reading configuration metadata, gets instructions on what objects to instantiate, configure, and assemble. The configuration metadata can be XML file, Java annotations, or Java code. The Spring IoC container makes use of Java POJO classes and configuration metadata to produce a fully configured and executable system or application. The below given diagram represent a high-level view of how Spring works.

IOC Container

Spring Framework provide two IoC containers.

  • Spring BeanFactory Container org.springframework.beans.factory.BeanFactory interface is the simplest container providing basic support for DI. The BeanFactory interface is the central IoC container interface in Spring. The BeanFactory and related interfaces, such as BeanFactoryAware, InitializingBean, DisposableBean, are still present in Spring to provide backward compatibility with other large number of third-party frameworks that integrate with Spring.
  • Spring ApplicationContext Container This container is defined by the org.springframework.context.ApplicationContext interface that is a wrapper of BeanFactory and adds other functionality such as easier integration with Spring's AOP features, message resource handling (for use in internationalization), event propagation, and application-layer specific contexts such as the WebApplicationContext for use in web applications.

Spring Dependency Injections

Every java application has a few objects that work together to present a working application. When writing a complex Java application, the classes should be loosely coupled from other Java classes to increase reusability of these classes and to test them independently of other classes while doing unit testing. Dependency Injection helps in keeping classes independent.

The basic principle behind Dependency Injection (DI) is that objects define their dependencies only through constructor arguments, arguments to a factory method, or properties which are set on the object instance after it has been constructed or returned from a factory method. Then, it is the job of the container to actually inject those dependencies when it creates the bean. This is fundamentally Inversion of Control (IoC).

Spring Framework provide two type of dependency injections.

  • Constructor injection : Constructor-based Dependency Injection is achieves when the container invokes a class constructor with a number of arguments, each representing a dependency on other class.
  • Setter injection : Setter-based Dependency Injection is achieves by the container calling setter methods on your beans after invoking a no-argument constructor or no-argument static factory method to instantiate your bean.