Design Patterns,  Java,  Spring Framework

Inversion of control in Spring

In Inversion of Control (IOC), the container itself maintains the class dependencies and manages the lifecycle of those dependencies. Objects are injected as dependencies when required at runtime or at application startup as they are added to the BeanFactory. Objects accept dependencies for the construction instead of constructing them.

Traditional dependency management

With traditional dependency management, a class containing a main method would construct the classes required for the implementation and any classes that it is further dependent on is constructed and managed at class creation. As a developer, you would have had to understand the multiple levels of classes, where and when they are supposed to be created and basically manage the lifecycle of the application through code which could get very complicated for large multi-layered applications.

IoC dependency management

With IoC dependency management, the IoC container is triggered from the main or parent class and constructs any objects that are required and referenced by the main class. Any further dependencies are constructed by the main or parent class itself. The IoC container injects dependent classes into the parent class and therefore manages the lifecycle of of the dependencies in the application.

Why use IoC?

The IoC container allows you to focus on the contracts of the interfaces you consume and develop business code only, leaving the object construction and management to the container. If the goal is to produce clean code, then the IoC container contributes greatly to this achieving this objective.

How does Spring facilitate IoC?

In Spring, the BeanFactory is the IoC container and by default you would not interact with the BeanFactory itself but if for some reason you need to interact with the BeanFactory, there are various hooks that can be utilized in the Spring bean lifecycle, eg. the BeanFactory post processor.

Instead, you would interact with the Application Context, a wrapper around the BeanFactory which triggers the application lifecycle. The Application Context and the BeanFactory share common interfaces. The BeanFactory maintains references to the Spring beans that can be configured manually or auto-configured by Spring Boot. After these references are configured, Spring handles the object initialization and instantiation. Once the beans are initialized, the sequence of operations for construction is created, based on the dependencies, ensuring that the beans are instantiated in the correct order by the BeanFactory. Any circular dependencies, where two beans are dependent on each other, Spring would warn you of the circular dependency during application compilation or startup.

To summarise:

  • The IoC container is configured by the developer
  • Spring maintains handles to objects constructed at startup
  • Spring serves singletons to classes during construction
  • Spring maintains the lifecycle of beans
  • The developer only has access to the application context

Leave a Reply

Your email address will not be published. Required fields are marked *