SpringBoot - XXXAware相关的接口使用场景是什么?

Spring中的Aware接口及应用场景 Aware接口是Spring Framework中提供的一组标记接口,用于在Bean装配的过程中获取Spring容器中提供的一些核心组件或运行时上下文等信息。通过使用Aware接口,我们可以在Bean实例化和初始化过程中获取到Spring容器中其他组件,方便Bean类实现更复杂的业务逻辑。本文将会一一介绍Spring中各个Aware接口,以及这些接口的主要应用场景。 阅读详情

写在前面

aware:英 [əˈweə®] 美 [əˈwer] adj.意识到的;知道的;觉察到的。
XXXAware在Spring里表示对XXX感知,当业务中实现XXXAware接口并且实现了对应的setXXX方法以后,就可以使用XXX的特性了。在某些特定的业务中需要获取一些在SpringBoot启动时的信息,也就是说有一些业务场景需要获取某些信息,而这些信息只有在SpringBoot各个生命周期中的各个阶段才能暴露出来,这时实现Aware接口就可以满足业务场景了。
Aware自身是一个空的接口,SpringBoot有很多Aware的实现接口,开发者实现这些已有的接口就能获取特定的信息。SpringBoot 提供了九大感知类接口:ApplicationContextAware、EnvironmentAware、ApplicationEventPublisherAware、BeanNameAware、BeanClassLoaderAware、BeanFactoryAware、EmbeddedValueResolverAware、ResourceLoaderAware、MessageSourceAware

使用场景

使用XXXAware可以让Bean获得Spring容器的服务,从而获取SpringBoot启动时的一些信息。

调用过程

如果某个Bean想要使用当前的 ApplicationContext,那么只需要让该Bean实现ApplicationContextAware接口,然后实现接口中唯一的方法void setApplicationContext(ApplicationContext var1) throws BeansException就可以了,Spring容器会自动调用这个回调方法将ApplicationContext 传给我们就可以了。

内置接口

序号接口说明
1ApplicationContextAware获取当前应用的上下文对象
2EnvironmentAware获取系统变量和环境变量,包括业务配置的以及系统默认的属性信息
3ApplicationEventPublisherAware获取应用事件发布器,可以发布我们监听的事件
4BeanNameAware获取容器中的Bean的名称或者是ID
5BeanClassLoaderAware获取Bean的类加载器
6BeanFactoryAware获取Bean的创建工厂对象,获取到这个工厂对象后可以使用BeanFactory提供大量功能
7EmbeddedValueResolverAware获取Spring容器加载的properties文件属性值
8ResourceLoaderAware获取资源加载器,可以加载特定业务需要的资源
9MessageSourceAware获取文本信息 ,解析消息

代码示例

@Slf4j
public class KieTemplate implements BeanClassLoaderAware {
    ...
    private ClassLoader classLoader;
    // SpringBoot会自动回调这个函数,将ApplicationContext对象传递给业务上下文,获取到这个实例可以做对应的处理
    @Override
    public void setBeanClassLoader(ClassLoader classLoader) {
        this.classLoader = classLoader;
    }
    ...
}

public class MyApplicationContext implements ApplicationContextAware {

    private ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        Optional.ofNullable(this.applicationContext).orElse(this.applicationContext = applicationContext);
    }

    public <T> T getBean(String name, Class<T> requiredType) throws BeansException {
        return applicationContext.getBean(name, requiredType);
    }

}
SpringBoot扩展点--Aware 在Spring Boot中,有一些可以实现的Aware接口,用于在应用程序中获取特定的上下文或对象。这些接口允许您的组件意识到它们所在的环境,并与之进行交互。 阅读详情

相关推荐

LMX2594时钟配置实战:TICS PRO从入门到寄存器导出

本文详细介绍了LMX2594时钟芯片的配置实战,重点讲解了如何使用TICS PRO调试软件简化复杂的寄存器设置过程。通过实例演示从基础频率配置到高级参数调优的全流程,并分享SPI写入的嵌入式系统移植技巧,帮助工程师快速掌握高性能时钟芯片的配置方法。

weixin_29189003的博客 555

Spring框架源码探索之 XXXAware 接口的作用

文章目录spring中共有三种Aware接口:Aware接口作用测试实例代码运行结果 spring ioc框架中有许多类都实现的XXXAwre接口的类。 这些类有用呢? spring中共有三种Aware接口: BeanNameAware BeanClassLoaderAawre BeanFactoryAware 分别定义了以下三种方法: void setBeanName(String nam...

weixin_42213903的博客 854

别再乱调NvM_WriteBlock了!手把手教你搞懂Autosar NVM状态机与读写时序

本文深入解析Autosar NVM状态机与读写时序,帮助开发者避免常见错误。通过详细的状态机解读、最佳实践代码示例和异常处理策略,提升嵌入式系统中数据存储的可靠性。特别针对NvM_WriteBlock的调用逻辑和状态机机制进行实战讲解,解决工程师在实际开发中遇到的异步写入问题。

weixin_33701617的博客 345

SpringBoot基础-Spring中的Aware

SpringBoot基础-Spring中的Aware

silly8543 1292

pandas,构造MultiIndex的四种方法:from_tuples、from_arrays、from_product、from_frame

1、from_tuples:通过 “元组对” 构造MultiIndex """ 一对元组,为一个索引 """ tuples = [(1, 'red'), (1, 'blue'), (2, 'red'), (2, 'blue')] pd.MultiIndex.from_tuples(tuples, names=('number', 'color')) 2、from_arrays:通过 “列表元素的组合” 构造MultiIndex """ 列中表的元素,按位置 对应组合,形成索引 """ arrays

jieru_liu的博客 5388

AI原生应用API编排:如何实现高效的权限管理?

随着GPT-4、Claude 3等大模型的普及,AI原生应用(从设计之初就以AI为核心的应用)正从“概念”走向“落地”。依赖大量外部API的协同工作(如用户身份API→大模型生成API→支付API→存储API)。但随之而来的挑战是:如何在复杂的API调用链中,确保每个环节的权限合规?本文将聚焦这一问题,覆盖API编排的基础逻辑、权限管理的核心机制(如RBAC/ABAC)、实战工具链(如OPA策略引擎),以及常见场景的解决方案。本文将按照“场景引入→核心概念→原理拆解→实战演练→趋势展望”的逻辑展开。

大数据洞察的博客 452

spring功能扩展之Aware接口

aware 英文译为:意识到的 一 .XXXAware在spring里表示对XXX感知,实现XXXAware接口,并通过实现对应的set-XXX方法,然后就可以使用XXX了。 二. Spring提供的Aware接口 接口 解释 BeanNameAware 获得到容器中Bean的名称 BeanFactoryAware 获得当前bean factory,这样可以调用容器的服务 ApplicationContextAware 获得当前application context,这样可以调用容器

weixin_38429251的博客 766

链路状态路由协议 OSPF (一)

本章将会学习链路状态路由协议OSPF,理解OSPF的基本概念,工作原理,掌握OSPF配置基本命令。本章重点,OSPF的基本概念,OSPF配置。

yj11290301的博客 3475

java学习日常

java学习日常,学习java点滴

song854601134的博客 2635

Open3D 计算点云配准的旋转和平移误差

通过计算旋转误差和平移误差来量化配准结果与真实变换之间的偏差。

点云侠的博客 253

霜冰优化算法(RIME)详解:从自然现象到智能优化

霜冰优化算法(RIME)是一种新型元启发式优化算法,灵感源于自然界中霜冰的形成过程。该算法通过模拟软霜和硬霜的不同生长特性,实现了高效的全局探索和局部开发平衡。RIME算法包含三个核心机制:软霜搜索策略模拟随机覆盖特性实现全局探索;硬霜穿刺机制模拟定向生长特性进行局部开发;正向贪婪选择机制确保解的持续优化。与遗传算法、粒子群算法相比,RIME具有参数少、收敛快、自适应平衡等优势,特别适合处理高维复杂优化问题。算法已在工程优化、机器学习等领域获得广泛应用,发表一年内即成为高被引论文。

qq_42212808的博客 787

深入分析Spring Aware原理

深入分析Spring Aware原理

及时当勉励,岁月不待人 1982

matlab 质心重合法实现点云配准【2026最新版】

质心重合法是将源点云和目标点云的质心重合,以此缩小两点云集之间的平移误差,从而为下一步粗配准提供更好的初始位置姿态。

点云侠的博客 510

关于Spring的Aware接口

Aware接口是一种框架辅助属性注入的一种思想,其他框架中也口可以看到类似的接口。框架具备高度封装性,我们接触到的一般都是业务代码,一个底层功能API不能轻易的获取到, 但是这不意味着永远用不到这些对象,如果用到了就可以使用框架提供的类似Aware的接口,让框架给我们注入该该对象。

qq_43231740的博客 548

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

struts2笔记04-XxxAware接口

1、XxxAware接口 ApplicationAware, RequestAware,SessionAware, ParameterAware. struts2提供了这四个Aware接口用于Action类的实现,从而注入对应的application、request、session,parameter, 不过它们都是Map类型的。这和ActionContext一样是解耦的,...

weixin_30462049的博客 226

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

Spring源码分析 (Aware接口调用) (八)

Aware接口是利用的回调机制, 将资源注入ioc容器 1.Aware是什么时候赋值进来的 1.createBeanInstance实例化 bean 2.populateBean属性注入 3.initializingBean初始化对象 createBeanInstance:getBean -> doGetBean -> createBean -> doCreateBean initializingBean初始化对象: 1.激活Aware 2.后置处理器的应用 3.init方法 所以说A.

959 982

数学建模模型总结

这是本人在两年内准备参加数学建模竞赛搜集到的模型资料,已经经过了本人的筛选和整理,希望能够对准备参加数学建模竞赛的学弟学妹们提供一些帮助!

SpringBoot中Aware接口回调顺序

SpringAware回调

碎银几两能解世间惆怅 915

Spring Boot 中的 Aware 接口以及 ApplicationContextAware 的详细说明、使用示例及注意事项

Spring Boot 中的 Aware 接口以及 ApplicationContextAware 的详细说明、使用示例及注意事项

爱的叹息的专栏 2084

springboot拓展接口ApplicationContextAware的应用场景

springboot的核心扩展接口应用分享,持续更新,不断学习~

m0_59588838的博客 1362

SpringBoot中的Aware接口

看源码过程中可以看到很多XXXAware,之前没去深究,直到看到有人面试阿里被问到这问题。才决定去了解一下。 (1)Aware是什么? 是一个接口,里面空白一片,没有任何方法,它是一个空荡荡的定义,一种标记。 (2)有什么用? Aware的英文意思是“可感知”,它自身是一个空白的接口,但是它有很多实现类,所以它的功能其实就是可以让调用者获取到某些信息,例如:加载当前Bean的容器名,当前Bean在容器中的BeanName,获取一些文本信息和资源文件等等,去获取加载当前Bean的加载器信息,等等等等。 我们

whiteBearClimb的博客 2662

Spring ---XXXAware 接口 XXXCapable接口

1.Spring 上下文 其实是指Spring容器。 1.Spring 中 XXXAware 接口 XXXAware 表示对XXX可以感知,实现 XXXAware接口的类 表示 这个类需要XXX 如果某个类 需要使用spring的一些东西,通过实现XXXAware接口就可以了,Spring 看到会给你送过来,接收方式 是通过 实现XXXAware接口的唯一方法set方法来接收 。 例如...

dream_heheda的博客 540

Spring原理学习(四)Aware 接口

Java 配置类包含 BeanFactoryPostProcessor 的情况,因此要创建其中的 BeanFactoryPostProcessor 必须提前创建 Java 配置类,而此时的 BeanPostProcessor 还未准备好,导致 @Autowired 等注解失效。例如下面这个例子:你会发现用 Aware 注入 ApplicationContext 成功,而 @Autowired 注入 ApplicationContext 失败要想生效就得加处理器。配置类MyConfig1。

小鲁蛋的博客 5097

Spring底层组件xxxAware家族

了解并使用Spring中xxxAware 组件家族。

BeALittleCat的专栏 488

【Springboot系列】springboot扩展点大整理,赶紧收藏起来

这些接口都属于Spring框架的一部分,可以通过在您的组件类中实现相应的接口并实现相应的方法来使用它们。通过使用这些扩展点,可以在应用程序的不同生命周期阶段插入自定义逻辑,例如应用程序启动前的准备、应用程序启动后的初始化、应用程序关闭时的清理等。:通过实现该接口,您的组件可以获取ApplicationContext(应用程序上下文)对象的引用,从而访问应用程序上下文中的Bean以及其他特定的Spring功能。这些接口提供了不同层面的扩展点,可以让您在Bean的生命周期中插入自定义逻辑。

我是香菜 2873

springboot 高级话题Spring Aware

Spring的依赖注入的最大亮点就是你所有的Bean对Spring容器的存在是没有意识的。即你可以将你的容器替换成别的容器,如Google Guice,这时Bean之间的耦合度很低。但是在实际项目中,你不可避免的要用到Spring容器本身的功能资源,这时你的Bean必须要意识到Spring容器的存在,才能调用Spring所提供的资源,这就是所谓的Spring Aware。

虾米大王的学习历程 174
上一篇: SpringBoot - InitializingBean的作用是什么?
下一篇: JAVA基础 - SPI机制使用详解(一)
cloneme01
博客等级 码龄20年 2万+粉丝 248原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

cloneme01

谢谢您的支持与鼓励!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值