Spring IOC
Inversion of Control transfers the responsibility of object creation, initialization, and destruction to the Spring container, which manages the complete lifecycle of objects.
Initializing the Spring Container
Classpath-based configurasion
The XML configuration file must reside within the classpath:
ApplicationContext context = new ClassPathXmlApplicationContext("application-config.xml");
Filesystem-based configuration
The XML configuration file is located at an absolute filesystem path:
ApplicationContext context = new FileSystemXmlApplicationContext("d:\app-config.xml");
Retrieving Objects from the Container
Configuration file:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
">
<bean id="customer" class="com.example.spring.Customer">
</bean>
</beans>
Java class:
package com.example.spring;
public class Customer {
public Customer() {
System.out.println("Customer instance created");
}
public void display() {
System.out.println("Display method invoked");
}
}
Client code:
package com.example.spring;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
ApplicationContext context = new
ClassPathXmlApplicationContext("application-config.xml");
Customer customer = (Customer) context.getBean("customer");
customer.display();
}
}
Bean Instantiation Strategies
Default Constructor
<bean id="customer" class="com.example.spring.Customer"></bean>
Constructor with Parameters
By parameter index:
<bean id="customer" class="com.example.spring.Customer">
<constructor-arg index="0" value="Alice"></constructor-arg>
<constructor-arg index="1" value="25"></constructor-arg>
</bean>
By parameter type:
<bean id="customer" class="com.example.spring.Customer">
<constructor-arg type="java.lang.String" value="Alice"></constructor-arg>
<constructor-arg type="java.lang.Integer" value="25"></constructor-arg>
</bean>
By parameter name:
<bean id="customer" class="com.example.spring.Customer">
<constructor-arg name="name" value="Alice"></constructor-arg>
<constructor-arg name="age" value="25"></constructor-arg>
</bean>
Static Factory Method
<bean id="customerFactory" class="com.example.spring.CustomerFactory" factory-method="createInstance"></bean>
package com.example.spring;
public class CustomerFactory {
public static Customer createInstance() {
return new Customer();
}
}
Bean Scopes
singleton
The default scope. A single shared instance exists per container. By default, singleton beans are eagerly instantiated when the container starts. Use the lazy-init attribute to defer instantiation until first access:
<bean id="customer" class="com.example.spring.Customer" lazy-init="true"></bean>
To apply lazy initialization global to all beeans:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
" default-lazy-init="true">
prototype
A new instance is created each time the bean is requested. The container does not manage the lifecycle of prototype beans, so the client code is responsible for cleanup:
<bean id="customer" class="com.example.spring.Customer" scope="prototype"></bean>
request
A distinct instance per HTTP request. Only valid in web-based application contexts.
session
A distinct instance per HTTP session. Only valid in web-based application contexts.
global session
A single instance across all HTTP sessions in a portlet-based web application.
Bean Lifecycle Callbacks
By default, Spring eagerly instantiates all singleton beans during container startup. While this may impact startup time, it ensures errors are caught early rather than at runtime. Spring supports custom initialization and destruction logic through the init-method and destroy-method attributes:
<bean id="customer" class="com.example.spring.Customer"
init-method="initialize"
destroy-method="cleanup"></bean>
The initialize method executes after the bean is fully set up, and the cleanup method executes when the container shuts down.