• Spring - ApplicationContextInitializer 扩展接口


    在这里插入图片描述


    Pre

    Spring Boot - 扩展接口一览

    在这里插入图片描述

    org.springframework.context.ApplicationContextInitializer扩展点

    
    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); }

    • 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
    • 35
    • 36
    • 37

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

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

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

    举几个可能的例子:

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

    扩展接口

    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");
        }
    }
        
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    扩展生效方式

    方式一 : Spring SPI扩展

    在spring.factories中加入

    org.springframework.context.ApplicationContextInitializer=com.artisan.bootspringextend.testextends.ExtendApplicationContextInitializer
    
    • 1

    在这里插入图片描述

    方式二 : 配置文件

    context.initializer.classes=com.artisan.bootspringextend.testextends.ExtendApplicationContextInitializer
    
    • 1

    在这里插入图片描述

    方式三 :启动类手工add

    在这里插入图片描述

    调整一下启动方式

       SpringApplication springApplication = new SpringApplication(BootSpringExtendApplication.class);
       springApplication.addInitializers(new ExtendApplicationContextInitializer());
    
      springApplication.run(args);
    
    • 1
    • 2
    • 3
    • 4

    测试结果

      .   ____          _            __ _ _
     /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
    ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
     \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
      '  |____| .__|_| |_|_| |_\__, | / / / /
     =========|_|==============|___/=/_/_/_/
     :: 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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    2022-11-26 22:38:12.280  INFO 15048 --- [           main] .b.t.ExtendApplicationContextInitializer : ExtendApplicationContextInitializer # initialize  Called
    
    • 1

    在这里插入图片描述

  • 相关阅读:
    C中的基本函数
    ROS官方教程知识点总结[低阶阶段]
    案例赏析 | 土耳其开赛利:闲置屋顶坐享“阳光收益”,助力企业实现绿色低碳转型
    axios和fetch
    python requests请求接口返回304问题解决
    NR slot配置
    Design Experience
    D-Desthiobiotin Amine_D-脱硫生物素-胺相关的产品性质
    【typescript】Typescript tsconfig.json全解析
    Linux硬盘分区与文件系统
  • 原文地址:https://blog.csdn.net/yangshangwei/article/details/128058533