Understanding basic Spring Framework and Total Flow Understanding basic Spring Framework and Total Flow spring spring

Understanding basic Spring Framework and Total Flow


  • Transition 1 – User sends request to server by submitting form / byclicking hyperlink etc. Request is initially given to WEB.XML.
  • Transition 2 – WEB.XML routes request to DispatcherServlet by lookingat tag.
  • Transition 3 – Inside DispatcherServlet,First ‘HandlerMapping’ handed over request to suitable ‘Controller’.
  • Transition 4 – Controller maps request to proper Model class. AllBUSINESS LOGIC is done inside Model class.
  • Transition 5 – If databaseoperation is needed then Model class will route request to suitableDAO. All database operations should be carried out in DAO.
  • Transition6 – If needed then attach attributes into request/session/applicationscope and return back to Model.
  • Transition 7 – If needed then attachattributes into request/session/application scope and return back toController.
  • Transition 8 – Controller simply returns it to any View(JSP/HTML etc).
  • Transition 9 – JSP/Html is viewed back to user.

Spring MVC Application Flow:


I'm new to Spring Framework too.Up to now, the document below is the most basic one. I'm reading it as well, hope it could help you.

Introduction to Spring Framework


I am new to Spring too and time ago had similar question. First of all I want to recommend you 'Spring in Action' book by Craig Walls, I found it very usefull and easy to understand, also http://www.tutorialspoint.com/spring/ helped me to figure many things out. If I understood your question right, then we can divide “Spring's flow” into Spring IoC container's and Spring bean's life cycles. Here is very small overview with exapmle on Spring bean's life cycle. A bean goes through several steps between creation and destruction in the Springcontainer. These steps are:

  1. Instantiate
  2. Populate properties
  3. BeanNameAware`s setBeanName()
  4. BeanFactoryAware`s setBeanFactory
  5. ApplicationContextAware`s setApplicationContext()
  6. Pre-initialization BeanPostProcessors
  7. InitializingBean`s afterPropertiesSet()
  8. Call custom init-method
  9. Post-initialization BeanPostProcessors
  10. DisponsableBean`s destroy
  11. Call custom destroy-method

Each step provides own oportunities for customization. Here is some code which simply “traces” 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 main class we creating ApplicationContext, getting bean and printing message out:

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 output lines correspond to life cycle stages this way:

  • 1.Instantiate

ClassA: default constructor called.

  • 2.Populate properties

ClassA: message set method called.

  • 3.BeanNameAware`s setBeanName()

ClassA: bean name set: classA

  • 4.BeanFactoryAware`s setBeanFactory

ClassA: beanFacrory set.

  • 5.ApplicationContextAware`s setApplicationContext()

ClassA: application context set:

  • 6.Pre-initialization BeanPostProcessors

CustomPostProcessor: beforeInitialization on: classA

  • 7.InitializingBean`s afterPropertiesSet()

ClassA: afterPropertiesSet() called because InitializingBean interface.

  • 8.Call custom init-method

ClassA: customInitMethod() method called.

  • 9.Post-initialization BeanPostProcessors

CustomPostProcessor: afterInitialization on: classA

  • Program prints message

ClassA: message get method called.messagA: Hello Spring!

  • 10.DisponsableBean`s destroy

ClassA: destroy() called because DisposableBean interface.

  • 11.Call custom destroy-method

ClassA: customDestroyMethod() method called.