Understanding basic Spring Framework and Total Flow
Asked Answered
E

3

9

I am new to the Spring Framework. Please guide me regarding basic understanding of Spring. I know Java and JSF, but I don't know anything about Struts or other frameworks. I have searched the Internet and was able to download a PDF, but I don't understand the Spring flow like I understand JSF flow. Please provide me with links for a simple way to understand Spring's flow.

Extracellular answered 6/7, 2012 at 9:34 Comment(0)
S
3

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

Susiesuslik answered 6/7, 2012 at 9:37 Comment(2)
Thanks i actually have that documentation,starting it was good but going on ,there is some lackness i felt so i am asking,may be if we go through it 3 times we can understand i think soo ,anyway Thanks dude.Extracellular
Hi, it's me again. When I was totally confused by terms like IoC and AOP, I found this site and it is really basic and clear.springbyexample.orgSusiesuslik
M
9
  • Transition 1 – User sends request to server by submitting form / by clicking hyperlink etc. Request is initially given to WEB.XML.
  • Transition 2 – WEB.XML routes request to DispatcherServlet by looking at tag.
  • Transition 3 – Inside DispatcherServlet, First ‘HandlerMapping’ handed over request to suitable ‘Controller’.
  • Transition 4 – Controller maps request to proper Model class. All BUSINESS LOGIC is done inside Model class.
  • Transition 5 – If database operation is needed then Model class will route request to suitable DAO. All database operations should be carried out in DAO.
  • Transition6 – If needed then attach attributes into request/session/application scope and return back to Model.
  • Transition 7 – If needed then attach attributes into request/session/application scope and return back to Controller.
  • 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:

Marjana answered 23/4, 2014 at 9:43 Comment(1)
Did you read the question right? The question was about Spring flow but not about spring MVC request-response flow.Ingroup
S
3

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

Susiesuslik answered 6/7, 2012 at 9:37 Comment(2)
Thanks i actually have that documentation,starting it was good but going on ,there is some lackness i felt so i am asking,may be if we go through it 3 times we can understand i think soo ,anyway Thanks dude.Extracellular
Hi, it's me again. When I was totally confused by terms like IoC and AOP, I found this site and it is really basic and clear.springbyexample.orgSusiesuslik
N
2

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 Spring container. 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.

Naught answered 15/2, 2013 at 21:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.