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

What are the three ways to use springboot ApplicationContextInitializer

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

What are the three ways to use springboot ApplicationContextInitializer? I believe many inexperienced people don't know what to do about it. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

Summary of three methods of using ApplicationContextInitializer

ApplicationContextInitializer is called during the springboot startup process (before the refresh method), mainly by pulling up the class ConfigurationClassPostProcessor in the initialize method in ApplicationContextInitializer (as I described in the springboot startup process), and implementing beandefinition through this processor.

To get to the point, there are three main ways to implement ApplicationContextInitializer:

1. Use spring.factories method

First we customize a class to implement ApplicationContextInitializer, and then create a new / META-INF/spring.factories file under resource.

Public class Demo01ApplicationContextInitializer implements ApplicationContextInitializer {@ Override public void initialize (ConfigurableApplicationContext configurableApplicationContext) {System.out.println ("user add method = = > ApplicationContextInitializer");}}

The loading process is to load directly in the getSpringFactoriesInstances () method in SpringApplication and execute the corresponding initialize method after the instance. The code is as follows:

Private Collection > getInitializerClasses (ConfigurableEnvironment env) {String classNames = env.getProperty (PROPERTY_NAME); List > (); if (StringUtils.hasLength (classNames)) {for (String className: StringUtils.tokenizeToStringArray (classNames, ",")) {classes.add (getInitializerClass (className)) }} return classes;}

The corresponding initialization class information is obtained from the configuration file, and then the initialization method is executed.

3. Directly through the add method

This method is relatively simple. When springboot starts, add a class that implements ApplicationContextInitializer. The code is as follows:

@ SpringBootApplicationpublic class InitializerDemoApplication {public static void main (String [] args) {/ / type01 SpringApplication springApplication = new SpringApplication (InitializerDemoApplication.class); springApplication.addInitializers (new Demo01ApplicationContextInitializer ()); springApplication.run (args); / / SpringApplication.run (InitializerDemoApplication.class,args);}}

All of the above 3 methods can implement custom Initializer, but the order of execution is different. Here I am more interested in two, one through spring.factories to implement the SPI pattern, interested can take a look at jdbc-starter and other related springboot starter.

The second is the bean which is used as a hook to pull up a "lump".

What has ApplicationContextInitializer done?

Initialization method

Org.springframework.boot.context.config.DelegatingApplicationContextInitializer

@ Override public void initialize (ConfigurableApplicationContext context) {ConfigurableEnvironment environment = context.getEnvironment (); / * * initialize the class * * / List specified by context.initializer.classes in the environment variable

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