I'm new to Spring too, and I had a similar question. First of all, I want to recommend Craig Walls 'Spring in Action' book, it was very useful and easy to understand for me, also http://www.tutorialspoint.com/spring/ helped me figure out many things. If I understood your question correctly, we could split the "Spring stream" into the Spring IoC container and Spring bean life cycles. Here is a very small overview with exapmle on the Spring bean life cycle. A bean goes through several steps between creating and destroying a Spring container. These steps are:
- Instantiate
- Fill Properties
- BeanNameAware`s setBeanName ()
- BeanFactoryAware`s setBeanFactory
- ApplicationContextAware`s setApplicationContext ()
- Initialization of BeanPostProcessors
- InitializingBean`s afterPropertiesSet ()
- Invoke a custom init method
- After initialization of BeanPostProcessors
- DisponsableBean`s destroy
- Call custom destroy method
Each step provides its own customization options. Here is some code that simply "tracks" the bean`s life:
For bean ClassA:
public class ClassA implements InitializingBean, DisposableBean, BeanNameAware, BeanFactoryAware, ApplicationContextAware{ private String messageA; public ClassA() { System.out.println("ClassA: default constructor called."); } public void customInitMethod(){ System.out.println("ClassA: customInitMethod() method called."); } public void customDestroyMethod(){ System.out.println("ClassA: customDestroyMethod() method called."); } public String getMessageA() { System.out.println("ClassA: message get method called."); return messageA; } public void setMessageA(String message) { System.out.println("ClassA: message set method called."); this.messageA = message; } public void afterPropertiesSet() throws Exception { System.out.println("ClassA: afterPropertiesSet() called because InitializingBean interface."); } public void destroy() throws Exception { System.out.println("ClassA: destroy() called because DisposableBean interface."); } public void setApplicationContext(ApplicationContext arg0) throws BeansException { System.out.println("ClassA: application context set: " + arg0.getApplicationName()); } public void setBeanFactory(BeanFactory arg0) throws BeansException { System.out.println("ClassA: beanFacrory set."); } public void setBeanName(String arg0) { System.out.println("ClassA: bean name set: " + arg0); } }public class ClassA implements InitializingBean, DisposableBean, BeanNameAware, BeanFactoryAware, ApplicationContextAware{ private String messageA; public ClassA() { System.out.println("ClassA: default constructor called."); } public void customInitMethod(){ System.out.println("ClassA: customInitMethod() method called."); } public void customDestroyMethod(){ System.out.println("ClassA: customDestroyMethod() method called."); } public String getMessageA() { System.out.println("ClassA: message get method called."); return messageA; } public void setMessageA(String message) { System.out.println("ClassA: message set method called."); this.messageA = message; } public void afterPropertiesSet() throws Exception { System.out.println("ClassA: afterPropertiesSet() called because InitializingBean interface."); } public void destroy() throws Exception { System.out.println("ClassA: destroy() called because DisposableBean interface."); } public void setApplicationContext(ApplicationContext arg0) throws BeansException { System.out.println("ClassA: application context set: " + arg0.getApplicationName()); } public void setBeanFactory(BeanFactory arg0) throws BeansException { System.out.println("ClassA: beanFacrory set."); } public void setBeanName(String arg0) { System.out.println("ClassA: bean name set: " + arg0); } }
For CustomPostProcessor:
public class CustomPostProcessor implements BeanPostProcessor { public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { System.out.println("CustomPostProcessor: beforeInitialization on: " + beanName); return bean; } public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { System.out.println("CustomPostProcessor: afterInitialization on: " + beanName); return bean; } }
In the main class, we create an ApplicationContext, get a bean and display a message:
public static void main(String[] args) { AbstractApplicationContext context = new ClassPathXmlApplicationContext( "META_INF/spring/app-context.xml"); ClassA objA = (ClassA) context.getBean("classA"); System.out.println(objA.getMessageA()); context.registerShutdownHook(); }
In app-context.xml we have:
<bean id="classA" class="ClassA" init-method="customInitMethod" destroy-method="customDestroyMethod"> <property name="messageA" value="messagA: Hello Spring!" /> </bean> <bean class="CustomPostProcessor" />
As I understand it, the output lines correspond to the stages of the life cycle in this way:
ClassA: default constructor called.
Class A: The message set method is called.
- 3.BeanNameAware`s setBeanName ()
ClassA: bean set name: classA
- 4.BeanFactoryAware`s setBeanFactory
ClassA: beanFacrory set.
- 5.ApplicationContextAware`s setApplicationContext ()
ClassA: set of application contexts:
- 6.Pre-initialization BeanPostProcessors
CustomPostProcessor: beforeInitialization on: classA
- 7.InitializingBean`s afterPropertiesSet ()
ClassA: afterPropertiesSet () is called because the InitializingBean interface.
- 8.Call custom init-method
The ClassA method is called: customInitMethod ().
- 9.Post-initialization BeanPostProcessors
CustomPostProcessor: afterInitialization on: classA
- The program prints a message
Class A: A method for receiving a message was called. messagA: Hello Spring!
- 10.DisponsableBean`s destroy
ClassA: destroy () is called for the DisposableBean interface.
- 11.Call custom destroy-method
ClassA: method called customDestroyMethod ().