前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Spring - ApplicationContextInitializer 扩展接口

Spring - ApplicationContextInitializer 扩展接口

作者头像
小小工匠
发布2022-12-01 16:52:34
3310
发布2022-12-01 16:52:34
举报
文章被收录于专栏:小工匠聊架构

文章目录


Pre

Spring Boot - 扩展接口一览

org.springframework.context.ApplicationContextInitializer扩展点

代码语言:javascript
复制
package org.springframework.context;

/**
 * Callback interface for initializing a Spring {@link ConfigurableApplicationContext}
 * prior to being {@linkplain ConfigurableApplicationContext#refresh() refreshed}.
 *
 * 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.
 *
 * {@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  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
 */
@FunctionalInterface
public interface ApplicationContextInitializer<C extends ConfigurableApplicationContext> {

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

}

Callback interface for initializing a Spring ConfigurableApplicationContext prior to being refreshed.

该接口是整个spring容器在刷新之前初始化ConfigurableApplicationContext的回调接口,即在容器刷新之前会调用该类的initialize方法。

实现这个接口可以在整个spring容器还没被初始化之前搞事情。

举几个可能的例子:

  • 利用这时候class还没被类加载器加载的时机,进行动态字节码注入
  • 应用配置激活

扩展接口

代码语言:javascript
复制
package com.artisan.bootspringextend.testextends;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;

/**
 * @author 小工匠
 * @version 1.0
 * @date 2022/11/26 22:16
 * @mark: show me the code , change the world
 */
@Slf4j
public class ExtendApplicationContextInitializer implements ApplicationContextInitializer {
    @Override
    public void initialize(ConfigurableApplicationContext applicationContext) {
        log.info("ExtendApplicationContextInitializer # initialize  Called");
    }
}

扩展生效方式

方式一 : Spring SPI扩展

在spring.factories中加入

代码语言:javascript
复制
org.springframework.context.ApplicationContextInitializer=com.artisan.bootspringextend.testextends.ExtendApplicationContextInitializer

方式二 : 配置文件

代码语言:javascript
复制
context.initializer.classes=com.artisan.bootspringextend.testextends.ExtendApplicationContextInitializer

方式三 :启动类手工add

调整一下启动方式

代码语言:javascript
复制
   SpringApplication springApplication = new SpringApplication(BootSpringExtendApplication.class);
   springApplication.addInitializers(new ExtendApplicationContextInitializer());

  springApplication.run(args);

测试结果

代码语言:javascript
复制
  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v2.4.2)

2022-11-26 22:38:12.280  INFO 15048 --- [           main] .b.t.ExtendApplicationContextInitializer : ExtendApplicationContextInitializer # initialize  Called
2022-11-26 22:38:12.305  INFO 15048 --- [           main] c.a.b.BootSpringExtendApplication        : Starting BootSpringExtendApplication using Java 1.8.0_261 on LAPTOP-JF3RBRRJ with PID 15048 (D:\IdeaProjects\boot2\boot-spring-extend\target\classes started by artisan in D:\IdeaProjects\boot2)
2022-11-26 22:38:12.306  INFO 15048 --- [           main] c.a.b.BootSpringExtendApplication        : No active profile set, falling back to default profiles: default
2022-11-26 22:38:12.723  INFO 15048 --- [           main] c.a.b.BootSpringExtendApplication        : Started BootSpringExtendApplication in 0.777 seconds (JVM running for 1.916)

Process finished with exit code 0

代码语言:javascript
复制
2022-11-26 22:38:12.280  INFO 15048 --- [           main] .b.t.ExtendApplicationContextInitializer : ExtendApplicationContextInitializer # initialize  Called
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022/11/27 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 文章目录
  • Pre
  • org.springframework.context.ApplicationContextInitializer扩展点
  • 扩展接口
  • 扩展生效方式
    • 方式一 : Spring SPI扩展
      • 方式二 : 配置文件
        • 方式三 :启动类手工add
        • 测试结果
        相关产品与服务
        容器服务
        腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档