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 develop the startup level of Spring boot

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

Share

Shulou(Shulou.com)06/02 Report--

This article is about how to carry out Spring boot startup-level development, the editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.

The startup of Spring boot can be divided into two stages, calling the refresh method of AbstractApplicationContext and the refresh of AbstractApplicationContext.

We know that AbstractApplicationContext's refresh method is a template method. Almost all types of ApplicationContext are initialized around this refresh method.

1. Before the refresh method

1.1 ApplicationContextInitializer

The class called by this class Spring boot*** is mainly to initialize some BeanFactoryPostProcessor (described later), or something that needs to be done when the Application is initialized. These classes load the full path name of the class by scanning the org.springframework.context.ApplicationContextInitializer word in the META-INF/spring.factories file under the calsspath path, get the object through reflection, and then call the initialize method.

1.2 ApplicationListener

This is similar to the way ApplicationContextInitializer loads, and is also configured from the META-INF/spring.factories file. The main function is to punish different events (ApplicationEvent and its subclasses) at different stages of Spring boot initialization, and these listeners will complete their own processing logic according to the circumstances in which different events are triggered. For example, ConfigFileApplicationListener is responsible for loading configuration files.

Of course, you can also add beanFactory in other ways. For more information, please refer to the method of adding BeanFactoryPostProcessor to beanFactory.

Some events in 1.ApplicationListener are triggered before ApplicationContextInitializer

two。 It is recommended that you do not add your own BeanFactoryPostProcessor method directly to ApplicationContextInitializer. XxxAware will not help you inject it.

2. Refresh method

This is the template method of spring, and there are three important interfaces.

2.1 BeanFactoryPostProcessorpublic interface BeanFactoryPostProcessor {void postProcessBeanFactory (ConfigurableListableBeanFactory beanFactory) throws BeansException;}

This method is to give some custom operations to beanFactory, such as adding some bean and so on. Of course, the premise is that the bean we define is in beanFactory. There are many ways to do this.

Define join in ApplicationContextInitializer, for example

Public class MyContextInitializer implements ApplicationContextInitializer {public void initialize (ConfigurableApplicationContext applicationContext) {applicationContext.addBeanFactoryPostProcessor (new MyBeanFactoryPostProcessor ());}}

Join through other BeanFactoryPostProcessor, you can annotate @ Import or load XML)

For example, through @ Import (value= {AnnotationBeanDefinitionRegistrar.class})

@ Configurationpublic class AnnotationBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar {private String BEAN_NAME = "annotationBeanPostProcessor"; public void registerBeanDefinitions (AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {List basePackages = getPackagesToScan (importingClassMetadata); if (! registry.containsBeanDefinition (BEAN_NAME)) {addPostProcessor (registry, basePackages);} private void addPostProcessor (BeanDefinitionRegistry registry, List basePackages) {GenericBeanDefinition beanDefinition = new GenericBeanDefinition (); beanDefinition.setBeanClass (AnnotationBeanPostProcessor.class) BeanDefinition.getConstructorArgumentValues () .addGenericArgumentValue (basePackages); beanDefinition.setRole (BeanDefinition.ROLE_INFRASTRUCTURE); registry.registerBeanDefinition (BEAN_NAME, beanDefinition);} private List getPackagesToScan (AnnotationMetadata metadata) {AnnotationAttributes attributes = AnnotationAttributes.fromMap (metadata.getAnnotationAttributes (EnableDubbo.class.getName (); String [] basePackages = attributes.getStringArray ("basePackages"); return Arrays.asList (basePackages);}}

In this way, you can join our custom BeanFactoryPostProcessor and develop at the level of Bean registration.

2.2 BeanPostProcessorpublic interface BeanPostProcessor {Object postProcessBeforeInitialization (Object bean, String beanName) throws BeansException; Object postProcessAfterInitialization (Object bean, String beanName) throws BeansException;}

This interface is mainly aimed at doing some custom development before and after bean instantiation. Generally, batch operations are only performed on an interface or an annotation.

3. Summary

1. If you need to do something when SpringApplication initializes, use ApplicationContextInitializer

two。 If you need to do something at a particular stage of SpringApplication, use ApplicationListener (recommended)

3. If you need to develop at the beanFactory level, use BeanFactoryPostProcessor (recommended)

4. If you need to develop at the instantiation level of a bean, use BeanPostProcessor (general business development can be satisfied with InitializingBean or init-method)

The above is how to develop at the startup level of Spring boot. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

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