3. Spring IoC Container
Spring IoC Container
- Spring Framework provides two of the most fundamental and important packages, they are the org.springframework.beans and org.springframework.context packages.
- The Spring container is the core of Spring Framework.
- The container, use for creating the objects and configuring them. Also, Spring IoC Containers use for managing the complete lifecycle from creation to its destruction.
- It uses Dependency Injection (DI) to manage components that make up the application.
- Spring containers are responsible for creating bean objects and injecting them into the classes.
- The container uses configuration metadata which represent by Java code, annotations or XML along with Java POJO classes.
How the Spring IoC Container Works:
1. Bean Definition:
- Beans in a Spring application are defined by creating a bean definition, which is essentially a metadata that defines how the bean should be created, configured, and managed.
2. Bean Lifecycle:
- The container manages the complete lifecycle of a bean: instantiation, population of properties, and initialization.
- It also manages the destruction of beans, calling any configured cleanup methods.
3. Dependency Injection:
- The container injects dependencies into the beans at runtime, resolving them from the container itself.
4. Configuration Metadata:
- The configuration metadata can be provided in XML, Java annotations Configuration (Spring 2.5 introduced), or Java-based configuration (Spring 3.0 introduced).
- Java-based configuration To use these new features, see the @Configuration, @Bean, @Import and @DependsOn annotations.
- Java configuration typically uses @Bean annotated methods within a @Configuration class.
- This metadata helps the container understand how to create and wire beans.
Example (XML-based configuration):
Consider the following XML configuration:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- Bean definition -->
<bean id="myService" class="com.example.MyService">
<property name="myDependency" ref="myDependencyBean"/>
</bean>
<!-- Another bean definition -->
<bean id="myDependencyBean" class="com.example.MyDependency"/>
</beans>
In this example, the container will create a `MyService` bean and inject the `MyDependency` bean into it based on the configuration.
Configuration metadata
- configuration metadata represents how you as an application developer tell the Spring container to instantiate, configure, and assemble the objects in your application.
Note : XML-based metadata is not the only allowed form of configuration metadata. The Spring IoC container itself is totally decoupled from the format in which this configuration metadata is actually written. These days many developers choose Java-based configuration for their Spring applications.
The two containers are namely,
BeanFactory(I) – Available in org.springframework.beans.factory package.
ApplicationContext(I) – Available in org.springframework.context package.
1. BeanFactory Interface
- This is the root interface for accessing a Spring bean container.
- It is the actual container that instantiates, configures, and manages a number of beans.
- XmlBeanFactory reads configuration metadata from XML file for creating a fully configured application.
- Lazy-loading of beans, which means beans are only created when requested.
Syntax :
ClassPathResource resource = new ClassPathResource("beans.xml");
XmlBeanFactory factory = new XmlBeanFactory(resource);
2. ApplicationContext Interface
- The ApplicationContext container is Spring’s advanced container.
- The ApplicationContext container has all the functionalities of BeanFactory.
- It is generally recommended over BeanFactory. The most common implementations of ApplicationContext are:
- Provides additional features like event propagation, AOP (Aspect-Oriented Programming), declarative mechanisms for handling transactions, etc.
- Supports eager-loading of beans, meaning beans are created eagerly at the application startup.
- ApplicationContext supports different ways of configuring beans. The configuration can be done using XML-based configuration, annotation-based configuration, or Java-based configuration.
- Use to implement ApplicationContext interface by using AnnotationConfigApplicationContext, ClasspathXMLApplicationContext , FileSystemXMLApplicationContext this classes.
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
Spring Dependency Injection
Comments
Post a Comment