Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the refresh method in Spring

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces "how to use the refresh method in Spring". In the daily operation, I believe that many people have doubts about how to use the refresh method in Spring. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to use the refresh method in Spring". Next, please follow the editor to study!

1 preface

After understanding some content of the principle, we will be able to battle and show muscle well in the project development and interview.

2 where to find the source code entrance

To read the source code of Spring, you need to find the entry file. In general, in a Spring project, the tomcat container loads the web.xml set in the project after startup, and adds project configuration files, listeners and other contents in the modification file, of which DispatcherServlet is particularly important, and its onRefresh method is its core operation. However, with the birth of springboot, after the idea of configuration is formed, the entrance to the source code is relatively simple, and the core needs to pay attention to AbstractApplicationContext.refresh.

The common entry points for getting methods are as follows:

# read ClassPathXmlApplicationContext ct = new ClassPathXmlApplicationContext ("bean.xml") through configuration file; # load AnnotationConfigApplicationContext ct = new AnnotationConfigApplicationContext (MainConfig.class) through configuration class; # both are subclasses of AbstractApplicationContext AbstractApplicationContext

As shown in the following figure, the ClassPathXmlApplicationContext loaded through the configuration file calls the refresh method of its parent class in the constructor, which is the core, known as AbstractApplicationContext.refresh ().

3 refresh core method

On the reading of the source code, because of its complex file structure, when combing its structure, we need to grasp the big and release the small, not go deep into the details, otherwise it is easy to get lost and do not remember the way to come. The following figure shows the core method AbstractApplicationContext.refresh

The refresh method is mainly divided into the following 12 core methods:

1 pre-processing before prepareRefresh () refresh, call the initPropertySources () method to initialize some property settings, call getEnvironment (). ValidateRequiredProperties () to verify the validity of the property, and set earlyApplicationEvents= new LinkedHashSet () to save earlier events in the container.

2 obtainFreshBeanFactory () gets the BeanFactory, creates a refreshed Bean factory, refreshBeanFactory (), sets the container ID, and returns the object of the created DefaultListableBeanFactory.

3 prepareBeanFactory (beanFactory) BeanFactory preparation, set BeanFactory class loader and expression parser, and add BeanPostProcessor [ApplicationContextAwareProcessor], set auto-assembly interface, add BeanPostProcessor.

4 the post-processing work that occurs after the postProcessBeanFactory (beanFactory) BeanFactory preparation is complete, and the subclass overrides this method to make further settings.

5 invokeBeanFactoryPostProcessors (beanFactory) executes the post processor of the BeanFactoryPostProcessor and BeanFactory methods. BeanFactoryPostProcessor and BeanDefinitionRegistryPostProcessor are executed after the BeanFactory standard initialization. The BeanDefinitionRegistryPostProcessor.postProcessBeanDefinitionRegistry method is executed first, according to PriorityOrdered, Ordered, and other unset priorities, and then the BeanFactoryPostProcessor.postProcessBeanFactory method is executed at the same priority.

6 registerBeanPostProcessors (beanFactory) registers BeanPostProcessor (the post processor of Bean). The execution time of BeanPostProcessor with different interface types is different before and after the creation of Bean.

7 initMessageSource () initializes the MessageSource component for internationalization, message binding, message parsing, and so on.

8 initApplicationEventMulticaster () initializes the event dispatcher, and if there is no event dispatcher in the container, create a SimpleApplicationEventMulticaster and add it to the container.

9 onRefresh () is left to subcontainers (subclasses), such as in springboot, to create tomcat, jetty containers and launch.

10 registerListeners () registers the ApplicationListener in all projects with the container and registers the listener with the event dispatcher.

11 finishBeanFactoryInitialization (beanFactory) initializes all remaining single instance bean, which is the core method and will be explained in detail in the following main process.

12 finishRefresh () completes the initialization and creation of the BeanFactory, and the IOC container is created.

At this point, the study on "how to use the refresh method in Spring" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report