This article was compiled by Colodoo (Paper Umbrella)

QQ 425343603

Java Learning Exchange Group (717726984)

Spring parsing notes startup process section has been completed, interested in the source code of the startup process can be used as a reference article.

  • Source code parsing: Spring source code parsing notes (2)
  • Source code parsing: Spring source code parsing notes (3)
  • Source code parsing: Spring source code parsing notes (4)

We have finished reading about the startup process. If there are omissions and errors, we will correct them. Next, we will study the design of Spring in depth.

We start with the boot process is often used in the chapter is the core of a class of AbstractApplicationContext as a starting point, to briefly draw its corresponding class diagram.

We still start with the source code, usually you may not be willing to directly read the lengthy comments, Spring source code comments is very standard, so that you can more directly understand the idea of design.

AbstractApplicationContext (abstract application context)

  • Simple implementation of the most common context functions.
  • Template method design patterns (design patterns) require subclasses to implement abstract methods.
  • Detect special beans defined in its in-memory bean factory and automatically register them
    • Rear BeanFactoryPostProcessors (Bean factory processor)
    • BeanPostProcessors
    • ApplicationListeners
  • MessageSource
  • ApplicationEventMulticaster (multicast)
    • The default multicast: SimpleApplicationEventMulticaster
  • The DefaultResourceLoader extension implements resource loading
    • Non-url resource paths are treated as classpath resources
    • You can override the getResourceByPath method in subclasses

The next parsing AbstractApplicationContext directly depend on the classes and interfaces.

  • ConfigurableApplicationContext application context (configurable)
  • DefaultResourceLoader (DefaultResourceLoader)

ConfigurableApplicationContext application context (configurable)


The next parsing ConfigurableApplicationContext this interface, it includes:

  • ApplicationContext
  • Closeable (Closeable)
  • Lifecycle (= Lifecycle)



Provides a central interface to the configuration for the application. This is read-only while the application is running, but it may be reloaded if the implementation supports it.

  • Provide factory methods to access beans (inherited from the ListableBeanFactory interface)
  • The ability to load file resources in a generic manner (inherited from the ResourceLoader interface)
  • Can time to post to the registered listener (inherited from ApplicationEventPublisher interface)
  • Ability to parse messages, with internationalization support (inherited from the MessageSource interface)
  • Inherits from the parent context. Definitions in the descendant context will always take precedence
    • The parent context is used by the entire Web application
    • Each servlet has its own subcontext, independent of the subcontext of any other servlet
  • BeanFactory life cycle
  • Implement detection and invocation interfaces
    • ApplicationContextAware
    • ResourceLoaderAware
    • ApplicationEventPublisherAware (application event publishing)
    • MessageSourceAwar (message source)



  • Access the root interface of the Spring Bean container.

  • The basic client views of the Bean container, ListableBeanFactory and ConfigurableBeanFactory, can be used for specific purposes.

  • This interface is implemented by objects that contain multiple bean definitions, each uniquely identified by a string name.

    • Depending on the bean definition, the factory will return either a standalone instance containing the object (the prototype design pattern) or a single shared instance (an advanced alternative to the singleton design pattern, where the instance is a singleton factory within scope).
    • Starting with Spring 2.0, more scopes are available depending on the specific application context (such as the “request” and “session” scopes in a Web environment).
  • Usually the BeanFactory will load is stored in the bean definition in the configuration source (such as an XML document), and use the org. Springframework. Beans package to configure a bean.

  • Initialization methods and their standard order:

    • The BeanNameAware setBeanName
    • The BeanClassLoaderAware setBeanFactory
    • The EnviromenetAware setEnvronment
    • The EmbeddedValueResolverAware setEmbeddedValueResolver
    • SetResourceLoader for ResourceLoaderAware (only available when running in an application context)
    • ApplicationEventPublisherAware setApplicationEventPublisher (within the context of the application runs only when applicable)
    • SetMessageSource for MessageSourceAware (only available when run in an application context)
    • ApplicationContextAware setApplicationContext (only available when run in the context of the application)
    • SetServletContext for ServletContextAware (only available when run in a Web application context)
    • BeanPostProcessors postProcessBeforeInitialization method
    • The InitializingBean afterPropertiesSet
  • Custom initialization method definitions

    • BeanPostProcessors postProcessAfterInitialization method
  • When shutting down the Bean factory, the following lifecycle methods apply

    • DestructionAwareBeanPostProcessors postProcessBeforeDestruction method
    • The DisposableBean destroy
    • Custom destruction method definitions

Closeable (Closeable)

Closeable is a data source or target that can be closed. Call the close method to release resources held by the object (such as open files).

Lifecycle (= Lifecycle)


  • A generic interface to define start/stop lifecycle control methods
  • A typical use case is to control asynchronous processing
  • It can be implemented by components (beans) and containers (ApplicationContext).
  • The container will propagate the start/end new hao to all components within each container of the application.

DefaultResourceLoader (DefaultResourceLoader)

  • Default implementation of the ResourceLoader interface
  • By ResourceEditor as org. Springframework. Context. Support. AbstractApplicationContext base class.
  • It can also be used alone
  • If the location value is a URL, return UrlResource if it is a non-URL path or a “classpath:” pseudo-URL, return ClassPathResource


  • Policy interface for loading resources, such as classpath or file system resources.
  • ApplicationContext is required to provide this functionality, along with extended ResourcePatternResolver support.
  • DefaultResourceLoader is a standalone implementation that can be used outside of ApplicationContext and also by ResourceEditor.
  • When run in ApplicationContext, Bean properties of type Resource and Resource array can be populated from strings using context-specific Resource loading strategies.


Because it is an overview, there will be no in-depth reading of the classes mentioned in the source code.

The purpose of this chapter is to identify some of the classes and interfaces that we need to know for our line-by-line parsing.