Some common IOC container implementation classes

  • AnnotationConfigApplicationContext annotation-based IOC container
  • ClassPathXmlApplicationContext IOC container based on XML file
  • AnnotationConfigServletWebServerApplicationContext annotation-based webIOC container

ApplicationContext and BeanFactory

  • The BeanFactory is a top-level interface that provides basic bean operations
  • ApplicationContext is a BeanFactory subinterface that implements MessageSource, eventListner, etc

Initialization of the container

To the commonly used AnnotationConfigApplicationContext, for example

  • Executed first constructor initializes two scan, scan with AnnotationBeanDefinitionReader to inside the container is initialized, provide for the use of external ClassPathBeanDefinitionScanner, According to the external path to the resolution of BD registration.
  • AnnotaionBeanDefinitionReader using constructor registers 6 BD, ConfigrutionClassPostProcessor (to parse the configuration notes and beanDefinition registration), Auto WiredPostProcessor (attributes inject called when use it to parse the @ Autowire), CommonAnnotaionPostProcessor (attributes inject call it to parse the @ resource, @ inject annotation), RequiredAnno TaionPostProcessor (which resolves the @Required annotation during property injection),MessagePostProcessor, and ListenerPostProcessor
  • Execute refresh() to refresh the container
  • BeanFactoryPostProcess(), which is typically used by Web containers, injects a servletContextAware parser
  • InvokeBeanFactorPostProcessor (), call the beanFactory front processor, This time you need to call before we inject ConfigrationClassPostProcessor to complete our configration annotation parsing and BeanDefinition into work
  • RegisteBeanPostProcessor () to complete the pre-processor registration of the bean
  • InitMessageResouce (), initializes the resource
  • InitListener (), initializes the listener
  • OnRefresh (), which is the only method implemented by the Web container, is where Tomcat was created

Object instantiation

  • Execute the constructor to get the bean
  • Determine whether the used needs to be exposed in advance and add the bean to the SingleObjectFactory if necessary
  • Into the property injection method and invoke instanceAwareBeanPostProcessor After (), according to the return value to determine whether to attribute injection
  • Follow the auto-injection mode of the configuration value for injection, call the 3 directly created annotations to parse @autoWired, @Resource,@Inject annotations
  • The instantiation method is performed to determine whether the object implements the Awared interface, and if so, the corresponding value is injected
  • Call beanPostProcessor Before() to parse a @PostConstrutor annotation
  • Call initiallizeBean AfterPropertySet(), call init lifecycle method ();
  • Call beanPostProcessor After() to complete dynamic proxy implementation AOP
  • Put the last bean into SignletonObjects to clear the secondary and tertiary caches

How to implement AOP

Spring’s AOP implementation is based on dynamic proxies. @enableAspectJ will @import an ImprtRegistar to import AutoProxyCreator, This class implements the smartInitializeBeanPostProcessor. So this class is the post-processor for a bean

The execution flow of SpringMVC

dispatchServlet.doDispatch()

  • Get the mappedHander from handlerMapping
  • Get the corresponding handerAdapter according to mappedHander
  • Execute interceptor preHandle()
  • Execute handerAdapter.handle() to retrieve the modelAndView
  • Execute interceptor postHandle()
  • Call processResult() to find the corresponding viewResolver to render
  • Execute afterCompletion() of the interceptor

SpringBoot startup process

  • Springapplication.run () this method is a static method that eventually generates a SpringApplication and returns it by calling run()
  • The constructor of The SpringApplication sets the webApplicationType based on the current environment
  • The run() method sets the current environment
  • Gets the bannerPrinter for the current project
  • Create an IOC container based on the current environment
  • Call the container’s refresh()
  • Call AfterRefresh ()

Automatic annotation process for Springboot

  • The @SpringBootApplication annotation contains an @enableAutoConfiguration annotation
  • @ EnableConfiguration will use @ Import annotations Import AutoConfigrutionImportSeletor. Class
  • In AutoConfigurationImportSelector seletImports getAutoConfigrutionEntry () method call
  • GetAutoConfigrutionEntry () scans the meta-INF/Spring.Factories files under all packages to find all autoConfigrution
  • The context invokeBeanFactoryPostPorcessor again invoked ConfigurationClassPostProcessor to parse the import annotations, Instead of calling selectImports directly, getAutoConfigrutionEntry gets the configrution class

How SpringBoot is packaged as a WAR to run

  • Implement SpringBootServletInitializer interface and write config method
  • Because SpringBootServletInitializer interface implements webApplicationInitializer interface to onStartUp () method
  • SpringServletContainerInitializer realized Servlet3.0 a specification, initialization in the servlet container invokes all initializeer onStartUp method,