#Spring #Container / #IOC
Rupali Giri
JAVA - Open Source Capability Leader | Technical Architect | Passionate Leader | Mentor
[
Lot of content already available on net with various terminologies and understandings around the various related topics . More than help at times it creates confusion when you find various content with different explanations on same thing. In an endeavour to keep interest alive and make subject interesting , have tried to give simple yet crisp understanding on all core concepts .
]
What is Container ?
It is Software component that takes care of complete life cycle of given resource/object from its birth till death , ?nothing but from its creation till destruction all stages are controlled by container .
Spring container is a core component of spring framework that is used directly or indirectly in any Spring based application . It is also possible to write standalone java application using only Spring container (nothing but Spring Core module)
While there are many Spring Containers , two of them are most popular :-
·???????? BeanFactory (org.springframework.beans.factory.BeanFactory?interface)
·???????? ApplicationContext (org.springframework.context.ApplicationContext?interface?
(Unlike legacy Spring , in Spring boot applications we don’t have to create any container class manually . It is provided by the framework implicitly)
How does it work in #Spring #Boot application ?
Many developers working Spring Boot REST and Microservice applications are not much familiar with container concept (many also think there’s no container concept in Spring boot ??) ?because in spring boot application (even in spring mvc) we don’t have to create any container class by implementing ApplicationContext interface . It is implicitly provided by the framework at runtime.
SpringApplication.run (..) which is available in Spring boot applications , creates container class at run time.
领英推荐
What does Container Do ?
Based on the declaration (xml / annotation) , it takes care all of the below on given java class.
·???????? Loading of class
·???????? Creation of the object
·???????? Raising life cycle events and calling life cycle methods
·???????? Destruction of object (garbage collection)
·???????? Dependency injection (DI) / dependency lookup
It runs on top of JVM
We need to pass instructions (configurations) to Spring container to make any java class as spring bean by either using xml configuration file (legacy) or using annotations (used in Spring boot and latest versions of Spring)
Inversion Of Control (# IOC) ?
( someone else(container) taking control (of beans ) on behalf of developers )
Spring Container takes the control of all java objects (beans) throughout the lifetime of an application. It not only creates the objects on demand at runtime but also responsible to ensure multiple instances of same object is not created (singleton) unless explicitly specified. When the application exits , it is also responsible to destroy all the objects and release memory (much relief to developers as this helps controlling memory leakage usually caused by objects that are no longer in use) This is one of the important reason behind Spring’s popularity amongst developers.