《这是知识点》之ApplicationContextInitializer源码和简单使用

《这是知识点》之ApplicationContextInitializer源码和简单使用

一、 ApplicationContextInitializer 介绍

首先看下spring官方说明
在这里插入图片描述

源码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package org.springframework.context;

/**
* Callback interface for initializing a Spring {@link ConfigurableApplicationContext}
* prior to being {@linkplain ConfigurableApplicationContext#refresh() refreshed}.
*
* <p>Typically used within web applications that require some programmatic initialization
* of the application context. For example, registering property sources or activating
* profiles against the {@linkplain ConfigurableApplicationContext#getEnvironment()
* context's environment}. See {@code ContextLoader} and {@code FrameworkServlet} support
* for declaring a "contextInitializerClasses" context-param and init-param, respectively.
*
* <p>{@code ApplicationContextInitializer} processors are encouraged to detect
* whether Spring's {@link org.springframework.core.Ordered Ordered} interface has been
* implemented or if the @{@link org.springframework.core.annotation.Order Order}
* annotation is present and to sort instances accordingly if so prior to invocation.
*
* @author Chris Beams
* @since 3.1
* @param <C> the application context type
* @see org.springframework.web.context.ContextLoader#customizeContext
* @see org.springframework.web.context.ContextLoader#CONTEXT_INITIALIZER_CLASSES_PARAM
* @see org.springframework.web.servlet.FrameworkServlet#setContextInitializerClasses
* @see org.springframework.web.servlet.FrameworkServlet#applyInitializers
*/
public interface ApplicationContextInitializer<C extends ConfigurableApplicationContext> {

/**
* Initialize the given application context.
* @param applicationContext the application to configure
*/
void initialize(C applicationContext);

}

简单说明一下:

  1. 用于spring上下文初始化的回调函数在上下文(ConfigurableApplicationContext)刷新(refresh)之前调用。
  2. 通常被用作web应用,在一些程序设计在spring容器初始化使用。比如说注册一些熟悉配置或者激活一些配置文件针对(ConfigurableApplicationContext的getEnvironment()方法)。参考ContextLoader和FrameworkServlet支持定义一个”contextInitializerClasses”上下文参数或者初始化参数。
  3. 可排序的(实现Ordered接口,或者添加@Order注解)

二、 ApplicationContextInitializer实现方式

首先新建一个类 MyApplicationContextInitializer 并实现 ApplicationContextInitializer 接口

  1. SpringBoot的main方法添加
1
2
3
4
5
6
7
8
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication application = new SpringApplication(Application.class);
application.addInitializers(new MyApplicationContextInitializer());
application.run(args);
}
}
  1. 在application.properties中配置
1
context.initializer.classes=org.springframework.boot.demo.common.MyApplicationContextInitializer
  1. SpringBoot的SPI扩展—META-INF/spring.factories中配置
1
org.springframework.context.ApplicationContextInitializer=org.springframework.boot.demo.common.MyApplicationContextInitializer

关注Github:1/2极客

关注博客:御前提笔小书童

关注网站:HuMingfeng

关注公众号:开发者的花花世界


本作品采用知识共享署名 4.0 中国大陆许可协议进行许可,欢迎转载,但转载请注明来自御前提笔小书童,并保持转载后文章内容的完整。本人保留所有版权相关权利。

本文链接:https://royalscholar.cn/2020/07/30/《这是知识点》之ApplicationContextInitializer源码和简单使用/

# Spring

评论

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×