Spring - ApplicationContextInitializer 扩展接口

2022-12-01 16:52:34 浏览数 (1)

文章目录

  • Pre
  • org.springframework.context.ApplicationContextInitializer扩展点
  • 扩展接口
  • 扩展生效方式
    • 方式一 : Spring SPI扩展
    • 方式二 : 配置文件
    • 方式三 :启动类手工add
  • 测试结果

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:IdeaProjectsboot2boot-spring-extendtargetclasses started by artisan in D:IdeaProjectsboot2)
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

0 人点赞