了解基本的 Spring 框架和总流程

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11359607/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 05:13:55  来源:igfitidea点击:

Understanding basic Spring Framework and Total Flow

springspring-3springsourcespring2.x

提问by user1407297

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.

我是 Spring 框架的新手。请指导我对 Spring 的基本理解。我知道 Java 和 JSF,但我对 Struts 或其他框架一无所知。我在互联网上搜索并能够下载PDF,但我不了解Spring流程,就像我了解JSF流程一样。请给我提供一个简单的方法来理解 Spring 流程的链接。

采纳答案by POPOL

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.

我也是 Spring Framework 的新手。到目前为止,下面的文档是最基本的。我也在读,希望对你有帮助。

Introduction to Spring Framework

Spring框架介绍

回答by Anil Satija

  • 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.
  • 转换 1 – 用户通过提交表单/点击超链接等方式向服务器发送请求。请求最初被提供给 WEB.XML。
  • 转换 2 – WEB.XML 通过查看标记将请求路由到 DispatcherServlet。
  • 转换 3 – 在 DispatcherServlet 中,首先“HandlerMapping”将请求移交给合适的“Controller”。
  • 转换 4 – 控制器将请求映射到正确的模型类。所有的业务逻辑都是在 Model 类中完成的。
  • Transition 5 – 如果需要数据库操作,那么 Model 类会将请求路由到合适的 DAO。所有的数据库操作都应该在DAO中进行。
  • Transition6 – 如果需要,然后将属性附加到请求/会话/应用程序范围并返回到模型。
  • 转换 7 – 如果需要,然后将属性附加到请求/会话/应用程序范围并返回给控制器。
  • Transition 8 – 控制器简单地将其返回到任何视图(JSP/HTML 等)。
  • Transition 9 – JSP/Html 返回给用户。

Spring MVC Application Flow:

Spring MVC 应用流程:

回答by Vladimir D

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:

我也是 Spring 的新手,前段时间也有类似的问题。首先,我想向您推荐 Craig Walls 的“Spring in Action”一书,我发现它非常有用且易于理解,而且 http://www.tutorialspoint.com/spring/帮助我弄清楚了很多事情。如果我理解你的问题,那么我们可以将“Spring 的流程”分为 Spring IoC 容器和 Spring bean 的生命周期。这是关于 Spring bean 生命周期的示例的非常小的概述。一个 bean 在 Spring 容器中经历了创建和销毁之间的几个步骤。这些步骤是:

  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
  1. 实例化
  2. 填充属性
  3. BeanNameAware 的 setBeanName()
  4. BeanFactoryAware 的 setBeanFactory
  5. ApplicationContextAware 的 setApplicationContext()
  6. 预初始化 BeanPostProcessors
  7. InitializingBean 的 afterPropertiesSet()
  8. 调用自定义初始化方法
  9. 初始化后的 BeanPostProcessors
  10. DisponsableBean 的销毁
  11. 调用自定义销毁方法

Each step provides own oportunities for customization. Here is some code which simply “traces” bean`s life:

每个步骤都提供了自己的定制机会。下面是一些简单地“追踪”bean 生命周期的代码:

For bean ClassA:

对于 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:

在主类中,我们创建 ApplicationContext,获取 bean 并打印消息:

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:

在 app-context.xml 中,我们有:

<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
  • 1.实例化

ClassA: default constructor called.

ClassA:调用的默认构造函数。

  • 2.Populate properties
  • 2.填充属性

ClassA: message set method called.

ClassA:调用的消息集方法。

  • 3.BeanNameAware`s setBeanName()
  • 3.BeanNameAware 的 setBeanName()

ClassA: bean name set: classA

ClassA:bean 名称集:classA

  • 4.BeanFactoryAware`s setBeanFactory
  • 4.BeanFactoryAware的setBeanFactory

ClassA: beanFacrory set.

ClassA:beanFacrory 集。

  • 5.ApplicationContextAware`s setApplicationContext()
  • 5.ApplicationContextAware 的 setApplicationContext()

ClassA: application context set:

ClassA:应用程序上下文集:

  • 6.Pre-initialization BeanPostProcessors
  • 6.预初始化BeanPostProcessors

CustomPostProcessor: beforeInitialization on: classA

CustomPostProcessor: beforeInitialization on: classA

  • 7.InitializingBean`s afterPropertiesSet()
  • 7.InitializingBean 的 afterPropertiesSet()

ClassA: afterPropertiesSet() called because InitializingBean interface.

ClassA:afterPropertiesSet() 因为 InitializingBean 接口而被调用。

  • 8.Call custom init-method
  • 8.调用自定义init-method

ClassA: customInitMethod() method called.

ClassA:调用的 customInitMethod() 方法。

  • 9.Post-initialization BeanPostProcessors
  • 9.Post-initialization BeanPostProcessors

CustomPostProcessor: afterInitialization on: classA

CustomPostProcessor: afterInitialization on: classA

  • Program prints message
  • 程序打印消息

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

ClassA:调用的消息获取方法。留言A:春天你好!

  • 10.DisponsableBean`s destroy
  • 10.DisponsableBean的销毁

ClassA: destroy() called because DisposableBean interface.

ClassA:destroy() 因为 DisposableBean 接口而被调用。

  • 11.Call custom destroy-method
  • 11.调用自定义destroy-method

ClassA: customDestroyMethod() method called.

ClassA:调用的 customDestroyMethod() 方法。