Spring - ApplicationContextInitializer 扩展接口

news2025/2/27 5:38:51

文章目录

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

在这里插入图片描述


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}.
 *
 * <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
 */
@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还没被类加载器加载的时机,进行动态字节码注入
  • 应用配置激活

扩展接口

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中加入

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

在这里插入图片描述

方式二 : 配置文件

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

在这里插入图片描述

方式三 :启动类手工add

在这里插入图片描述

调整一下启动方式

   SpringApplication springApplication = new SpringApplication(BootSpringExtendApplication.class);
   springApplication.addInitializers(new ExtendApplicationContextInitializer());

  springApplication.run(args);

测试结果

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: 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

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

在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/39199.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

详解诊断数据库ODX-C

文章目录 前言一、ODX—C作用是什么?二、ODX-C数据库在工具ODXStudio的编辑方法总结前言 ODX是全球通用的一种诊断数据库格式,相比CDD文件(Vector公司私有的一种数据库格式),应用场景和范围更广,包含了不同的子类: ODX-C\-D\-V\-E\-F\-FD 今天这篇文章仅对ODX-C做一个…

开源物联网系统 ThingsBoard 上手

开源物联网系统 ThingsBoard 上手 centos yum 被占用问题解决&#xff1a; 描述&#xff1a;Another app is currently holding the yum lock; waiting for it to exit 参考&#xff1a;https://blog.csdn.net/Dan1374219106/article/details/112450922 查看yum占用&#xff1a…

制作一个简单HTML中华传统文化网页(HTML+CSS)

&#x1f389;精彩专栏推荐 &#x1f4ad;文末获取联系 ✍️ 作者简介: 一个热爱把逻辑思维转变为代码的技术博主 &#x1f482; 作者主页: 【主页——&#x1f680;获取更多优质源码】 &#x1f393; web前端期末大作业&#xff1a; 【&#x1f4da;毕设项目精品实战案例 (10…

scrapy的入门使用

目录 一、 安装scrapy 1.windonws/Mac安装命令&#xff1a; 2. 安装依赖包&#xff1a;pip install pypiwin32 二、 scrapy项目开发流程 1.创建项目:    2.生成一个爬虫: 3.提取数据: 4.保存数据: 三、 创建项目 四、创建爬虫 五、完善爬虫 5.2 定位元素以及提取…

序列化与反序列化笔记

序列化与反序列化 为什么会有序列化与反序列化的需求呢&#xff1f; 序列化是把对象转换成有序字节流&#xff0c;通常都是一段可阅读的字符串&#xff0c;以便在网络上传输或者保存在本地文件中。同样&#xff0c;如果我们想直接使用某对象时&#xff0c;就可能通过反序列化…

23-Vue之事件修饰符

事件修饰符前言阻止默认行为阻止冒泡事件前言 本篇来学习两个常用的事件修饰符 阻止默认行为 .prevent : 阻止默认行为 <!DOCTYPE html> <html lang"en"> <head><meta charset"UTF-8"><meta name"viewport" cont…

基于JSP的网上书城平台【数据库设计、源码、开题报告】

数据库脚本下载地址&#xff1a; https://download.csdn.net/download/itrjxxs_com/86469277 主要使用技术 ServletJDBCJSPC3P0JqueryMysql 功能介绍 1). 用户模块功能有&#xff1a; 用户注册: 表单页面是jQuery做校验(包含了ajax异步请求) 表单页面使用一次性图形验证码…

分布式事务Seata

目录 一、分布式事务的认识 事务的ACID原则 CAP定理 BASE理论 二、Seata简介、安装和部署 1.初识Seata 2.下载Seata&#xff08;1.4.2版本&#xff0c;其他版本可能与本章教程冲突&#xff09; 3.修改配置文件 4.在nacos添加配置 5.创建配置文件中的数据库表 6.启动TC…

二、微服务拆分案例

文章目录一、服务拆分&#xff08;order-service、user-service&#xff09;1.创建数据库2.创建order-service和user-service模块&#xff0c;引入依赖3、order-service各层代码4、user-service各层代码一、服务拆分&#xff08;order-service、user-service&#xff09; 1.创建…

猴子也能学会的jQuery第十二期——jQuery遍历

&#x1f4da;系列文章—目录&#x1f525; 猴子也能学会的jQuery第一期——什么是jQuery 猴子也能学会的jQuery第二期——引用jQuery 猴子也能学会的jQuery第三期——使用jQuery 猴子也能学会的jQuery第四期——jQuery选择器大全 猴子也能学会的jQuery第五期——jQuery样式操作…

【金融项目】尚融宝项目(十五)

29、提现和还款 29.1、提现 29.1.1、需求 放款成功后&#xff0c;借款人可以申请提现。 参考《汇付宝商户账户技术文档》3.15用户申请提现 29.1.2、前端整合 pages/user/withdraw.vue <script> export default {data() {return {fetchAmt: 0,}},methods: {commitWit…

基于SSM的宿舍财产管理系统【数据库设计、源码、开题报告】

数据库脚本下载地址&#xff1a; https://download.csdn.net/download/itrjxxs_com/86469100 主要使用技术 SpringSpringMVCMybatisEasyUIJqueryMysql 功能介绍 本系统的用户可以分为三种&#xff1a;管理员、教师、学生。 系统设置 菜单管理&#xff1a;菜单节点的增删改查…

中国传统美食网页HTML代码 学生网页课程设计期末作业下载 美食大学生网页设计制作成品下载 DW餐饮美食网页作业代码下载

&#x1f380; 精彩专栏推荐&#x1f447;&#x1f3fb;&#x1f447;&#x1f3fb;&#x1f447;&#x1f3fb; ✍️ 作者简介: 一个热爱把逻辑思维转变为代码的技术博主 &#x1f482; 作者主页: 【主页——&#x1f680;获取更多优质源码】 &#x1f393; web前端期末大作业…

【学习笔记41】DOM操作的练习

一、回到顶部 我们在浏览页面的时候&#xff0c;当我们浏览到一个页面的底部的时&#xff0c;一般都会有一个返回底部 &#xff08;一&#xff09;案例分析 1、当页面滚动的距离大于300的时候&#xff0c;让herder和btn展示 header的top设置为0的时候就能看到btn的display设置…

web网页设计期末课程大作业——海贼王大学生HTML网页制作 HTML+CSS+JS网页设计期末课程大作业 web前端开发技术 web课程设计 网页规划与设计

HTML实例网页代码, 本实例适合于初学HTML的同学。该实例里面有设置了css的样式设置&#xff0c;有div的样式格局&#xff0c;这个实例比较全面&#xff0c;有助于同学的学习,本文将介绍如何通过从头开始设计个人网站并将其转换为代码的过程来实践设计。 ⚽精彩专栏推荐&#x1…

HTML常用标记(超详解)

目录 一、文本标记 二、列表标记 三、分割线标记 四、超链接标记 五、图片标记 六、多媒体标记 七、标记类型 八、meta标记 一、文本标记 1.标题标记 语法格式&#xff1a; <hn align"对齐方式">标题文本</hn> html中提供了六级标题&#xff…

产业经济专题:产业结构高级化、合理化指数、工业化率、机构水平化及产业升级度

一、产业关联度的密度指数 1、数据来源&#xff1a;见参考文献 2、时间跨度&#xff1a;无 3、区域范围&#xff1a;无 4、指标说明&#xff1a; 附件中包括命令和案例数据 部分数据如下&#xff1a; 计算参考文献&#xff1a; Xiao J, Boschma R, Andersson M. Industr…

分布式NoSQL数据库HBase实践与原理剖析(一)

title: HBase系列 第一章 HBase基础理论 1.1 HBase简介 Apache HBase™ is the Hadoop database, a distributed, scalable, big data store. Apache HBase™ 是Hadoop数据库&#xff0c;是一种分布式、可扩展的大数据存储。HBase 是 BigTable 的开源 java 版本。 建立在 HDF…

SecXOps 核心技术能力划分

核心能力 为了加快安全分析能力更全面、更深入的自动化 &#xff0c;SecXOps 的目标在于创建一个集成的用于 Security 的 XOps 实践&#xff0c;提升安全分析的场景覆盖率和运营效率。SecXOps 技术并不 015 SecXOps 技术体系 是 Ops 技术在安全领域的简单加和&#xff0c;SecXO…

浏览器输入www.baidu.com的请求过程是怎么样的? 响应的过程是怎样的呢?

假设我们电脑的相关配置为&#xff1a; ip地址&#xff1a;192.168.31.37 子网掩码&#xff1a;255.255.255.0 网关地址&#xff1a;192.168.31.1 DNS地址&#xff1a;8.8.8.8 1. DNS 解析 我们打开一个浏览器&#xff0c;请求ww.baidu.com地址&#xff0c;这个时候找DNS 服务…