Spring Inversion of Control (IoC) also known as
Dependency Injection (DI) is a process by which
objects define their dependencies with collaborating objects.
Spring provides InitializingBean interface which allows us to
perform post processing tasks after bean initialization by Spring container.
InitializingBean.java
package org.springframework.beans.factory;
public abstract interface InitializingBean
{
public abstract void afterPropertiesSet() throws Exception;
}
System.out.println("Initializing ApplicationContext");
ApplicationContext factory = new ClassPathXmlApplicationContext("init.xml");
System.out.println("ApplicationContext Initialized");
System.out.println("Accessing first bean");
SimpleInitBean first = (SimpleInitBean) factory.getBean("first");
System.out.println(first);
System.out.println("Accessing second bean");
SimpleInitBean second = (SimpleInitBean) factory.getBean("second");
System.out.println(second);
}
}
It gives the following output,
Initializing ApplicationContext
ApplicationContext Initialized
Accessing first bean
In afterPropertiesSet callback : FIRST
In afterPropertiesSet callback : FIRST@callback
SimpleInitBean[FIRST@callback]
Accessing second bean
In afterPropertiesSet callback : SECOND
In afterPropertiesSet callback : SECOND@callback
SimpleInitBean[SECOND@callback]