如何自动装配-Aware注入Spring底层组件及原理
发表于:2025-12-01 作者:千家信息网编辑
千家信息网最后更新 2025年12月01日,本篇内容主要讲解"如何自动装配-Aware注入Spring底层组件及原理",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"如何自动装配-Aware注入Spri
千家信息网最后更新 2025年12月01日如何自动装配-Aware注入Spring底层组件及原理
本篇内容主要讲解"如何自动装配-Aware注入Spring底层组件及原理",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"如何自动装配-Aware注入Spring底层组件及原理"吧!
23、自动装配-Aware注入Spring底层组件&原理
Aware 接口,提供了类似回调函数的功能
自定义组件想要使用Spring 容器底层的一些组件(Application Context,Bean Factory);自定义组件需要实现xxxAware接口;在创建对象的时候,会调用接口规定的方法注入相关组件
package org.springframework.beans.factory;public interface Aware {}23.1 ApplicationContextAware 自动注入IOC容器
package org.springframework.context;import org.springframework.beans.BeansException;import org.springframework.beans.factory.Aware;public interface ApplicationContextAware extends Aware { void setApplicationContext(ApplicationContext applicationContext) throws BeansException;}23.2 ApplicationEventPublisherAware 注入事件派发器
package org.springframework.context;import org.springframework.beans.factory.Aware;public interface ApplicationEventPublisherAware extends Aware { void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher);}23.3 BeanClassLoaderAware 类加载器
package org.springframework.beans.factory;public interface BeanClassLoaderAware extends Aware { void setBeanClassLoader(ClassLoader classLoader);}23.4 BeanFactoryAware Bean工厂
package org.springframework.beans.factory;import org.springframework.beans.BeansException;public interface BeanFactoryAware extends Aware { void setBeanFactory(BeanFactory beanFactory) throws BeansException;}23.5 BeanNameAware Bean名字
package org.springframework.beans.factory;public interface BeanNameAware extends Aware { void setBeanName(String name);}23.6 EmbeddedValueResolverAware Embedded值解析器
package org.springframework.context;import org.springframework.beans.factory.Aware;import org.springframework.util.StringValueResolver;public interface EmbeddedValueResolverAware extends Aware { void setEmbeddedValueResolver(StringValueResolver resolver);}23.7 EnvironmentAware 环境
package org.springframework.context;import org.springframework.beans.factory.Aware;import org.springframework.core.env.Environment;public interface EnvironmentAware extends Aware { void setEnvironment(Environment environment);}23.8 ImportAware 导入相关的
package org.springframework.context.annotation;import org.springframework.beans.factory.Aware;import org.springframework.core.type.AnnotationMetadata;public interface ImportAware extends Aware { void setImportMetadata(AnnotationMetadata importMetadata);}23.9 LoadTimeWeaverAware 导入相关的
package org.springframework.context.weaving;import org.springframework.beans.factory.Aware;import org.springframework.instrument.classloading.LoadTimeWeaver;public interface LoadTimeWeaverAware extends Aware { void setLoadTimeWeaver(LoadTimeWeaver loadTimeWeaver);}23.10 MessageSourceAware 国际化
package org.springframework.context;import org.springframework.beans.factory.Aware;public interface MessageSourceAware extends Aware { void setMessageSource(MessageSource messageSource);}23.11 NotificationPublisherAware 发送通知的支持
package org.springframework.jmx.export.notification;import org.springframework.beans.factory.Aware;public interface NotificationPublisherAware extends Aware { void setNotificationPublisher(NotificationPublisher notificationPublisher);}23.12 ResourceLoaderAware 资源加载器
package org.springframework.context;import org.springframework.beans.factory.Aware;import org.springframework.core.io.ResourceLoader;public interface ResourceLoaderAware extends Aware { void setResourceLoader(ResourceLoader resourceLoader);}23.13 测试用例
package com.hw.springannotation.beans;import org.springframework.beans.BeansException;import org.springframework.beans.factory.BeanNameAware;import org.springframework.context.ApplicationContext;import org.springframework.context.ApplicationContextAware;import org.springframework.context.EmbeddedValueResolverAware;import org.springframework.stereotype.Component;import org.springframework.util.StringValueResolver;/** * @Description TODO * @Author hw * @Date 2018/11/28 15:44 * @Version 1.0 */@Componentpublic class Red implements ApplicationContextAware, BeanNameAware, EmbeddedValueResolverAware { private ApplicationContext applicationContext; public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { System.out.println("传入的IOC " + applicationContext); this.applicationContext = applicationContext; } public void setBeanName(String name) { System.out.println("当前bean的名字" + name); } public void setEmbeddedValueResolver(StringValueResolver resolver) { String value = resolver.resolveStringValue("你好${os.name} 我是#{20*20}"); System.out.println("解析的字符串:" + value); }}运行:

说白了 Aware接口类,就是一个回调类的抽象
到此,相信大家对"如何自动装配-Aware注入Spring底层组件及原理"有了更深的了解,不妨来实际操作一番吧!这里是网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
组件
底层
原理
装配
接口
内容
名字
容器
方法
学习
实用
更深
说白了
事件
你好
兴趣
函数
功能
国际
字符
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
数据库课设体会800字
注册界面数据库
海淀区管理网络技术服务平台
同一首歌钢琴谱软件开发
五中全会中涉及到的网络安全
数据库怎么重新打开
命令行连接sql数据库
航测服务器搭建
软件开发怎么选择职业
计算机和网络安全相关的毕业设计
软件开发企业成立条件
学校对青少年网络安全教育
excle 每日数据库
c 注册 数据库
mysql建数据库语句
广州软件开发地点
数据库按行生成文件
思维导图类软件开发流程
专家解读网络安全知识
宝山区正规软件开发销售方法
计算计网络技术是学什么的
免费转服的服务器
高考网络安全2018招生
进贤软件开发
浙江管理软件开发中心
选择苏州戴尔服务器生产商
惠程科技与互联网游戏
服务器老化出故障导致业务停滞
远程鉴定服务器
网络安全工程师很严格