IoC Container
1. IoC Container
IoC(Inversion of Control) : Let the framework control object creation and wiring instead of the application itself.
Responsible for:
- Bean Creation: Responsible for instantiating, configuring, and assembling beans.
- Bean Lifecycle Management: Manages the lifecycle of bean(objects).
- Dependency Injection: Injecting dependencies(bean) into other beans.
Key Points:
- Enable loosely coupled applications, where objects do not need to know about how their dependencies are created.
- Implements
IoCthroughDIto provide dependencies at runtime. - primarily represented by the
ApplicationContext.
2. Types of IoC Container
BeanFactory- An interface type.
- Lazily loads beans (creates them only when requested).
- Provides basic dependency injection.
ApplicationContext- An interface type.
- Extends BeanFactory with more features.
- Supports Internationalization (i18n),Event propagation,Bean post-processing,Access to resources (files, URLs).
3. Types of ApplicationContext subclasses
ClassPathXmlApplicationContext– Load Context from XML in classpath.FileSystemXmlApplicationContext– XML from file system.AnnotationConfigApplicationContext– Java-based config using annotation(@Configuration).WebApplicationContext– For web apps integrating with Spring MVC.GenericApplicationContext– Flexible,can combine XML ,annotation and programmatic Configuration.
Responsible for:
- Loads Config.xml
- Reads bean definitions
- Creates & initializes beans inside Spring container
Example
What actually happens:
- Reads Config.xml.
- Finds bean definitions (classes you declared with
or via annotations like @Component). - Duplicate bean id : Exception (duplicate bean IDs not allowed).
- Requesting undefined bean id : Exception (bean not found in config).
- (Reference of other bean)passing id of other class : bean is matching but data type is not matching.
- For each bean definition
- Checks if the class exists in the classpath.
- If it exists, Spring creates an object (bean).
- If it’s missing, it throws an error at startup.
so Spring only checks & creates objects for the configured beans, not for every class in our project.
4. IoC Container Lifecycle
- Lifecycle of the Spring container itself (e.g.
ApplicationContext). - Scope is Global : affects the entire application context.
- Includes : Context creation,Event publishing,Bean loading,Shutdown handling.
- Key annotations/APIs:
ApplicationListener,ContextRefreshedEvent, etc. - Managed by : Spring framework at the container level.
Life Cycle Method
- Initialization: Container starts, reads configuration, and loads bean definitions.
- Bean Instantiation: Creates bean objects using constructors or factory methods.
- Dependency Injection (DI): Injects required dependencies into beans.
- Lifecycle Management: Calls
@PostConstructmethods after initialization and@PreDestroybefore destruction. - Singleton Scope:** By default, one instance per bean is maintained in the container.
- ApplicationContext Events: Publishes events that components can listen to for loosely coupled, event-driven design.
- Bean Post-Processing: BeanPostProcessors can modify beans before and after initialization.