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
*/
@Component
public 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);
}
}

运行:

23、自动装配-Aware注入Spring底层组件&原理的更多相关文章

  1. Spring9——通过用Aware接口使用Spring底层组件、环境切换

    通过用Aware接口使用Spring底层组件 能够供我们使用的组件,都是Aware的子接口. ApplicationContextAware:实现步骤:             (1)实现Applic ...

  2. 【Spring注解驱动开发】自定义组件如何注入Spring底层的组件?看了这篇我才真正理解了原理!!

    写在前面 最近,很多小伙伴出去面试都被问到了Spring问题,关于Spring,细节点很多,面试官也非常喜欢问一些很细节的技术点.所以,在 Spring 专题中,我们尽量把Spring的每个技术细节说 ...

  3. Spring第八发—自动装配及让Spring自动扫描和管理Bean

    依赖注入–自动装配依赖对象(了解即可) 对于自动装配,大家了解一下就可以了,实在不推荐大家使用.例子: byName:按名称装配,可以根据属性的名称,在容器中寻找跟该属性名相同的bean,如果没有找到 ...

  4. 在自定义组件中获取spring底层组件

    要想在自定义组件中获取spring底层的各种组件,只需让自定义组件实现一系列接口即可,这些接口都是Aware的子接口.常见的有: 1. ApplicationContextAware——用于获取IOC ...

  5. 使用传统的三层架构出现的问题.引入Spring底层实现原理来解决(工厂模式+反射+XML配置文件/注解)

    以前写的代码 mapper层 public interface PersonMapper { void selectPersonList(); } public class PersonMapperI ...

  6. 【spring 注解驱动开发】spring自动装配

    尚学堂spring 注解驱动开发学习笔记之 - 自动装配 自动装配 1.自动装配-@Autowired&@Qualifier&@Primary 2.自动装配-@Resource& ...

  7. Spring笔记(2) - 生命周期/属性赋值/自动装配及部分源码解析

    一.生命周期 @Bean自定义初始化和销毁方法 //====xml方式: init-method和destroy-method==== <bean id="person" c ...

  8. Spring注解驱动开发(三)-----自动装配

    自动装配 概念 Spring利用依赖注入(DI),完成对IOC容器中中各个组件的依赖关系赋值. @Autowired-----自动注入 1.默认优先按照类型去容器中找对应的组件 application ...

  9. Spring注解开发系列Ⅴ --- 自动装配&Profile

    自动装配: spring利用依赖注入和DI完成对IOC容器中各个组件的依赖关系赋值.自动装配的优点有: 自动装配可以大大地减少属性和构造器参数的指派. 自动装配也可以在解析对象时更新配置. 自动装配的 ...

随机推荐

  1. springcloud断路器Dashboard监控仪表盘的使用

    断路器Dashboard监控仪表盘的使用 在原有的orderserverfeignhystrix服务中使用 1.增加依赖仓库              <dependency> <g ...

  2. css — 选择器、盒子模型

    目录 1. css引入方式 2. css选择器 3. css的盒模型 css: 层叠样式表 1. css引入方式 行内样式 <div style='color:red;'>mjj</ ...

  3. 20191011-构建我们公司自己的自动化接口测试框架-Util的读取excel常用方法模块

    包括获取excel的sheet名字,设定excel的sheet,读excel,写excel等常规操作. from openpyxl import Workbook from openpyxl impo ...

  4. 初始STM32

    主要内容: 1.什么是STM32 STM32有什么 STM32怎么选型号 一:什么是STM32 ST— 意法半寻体,是一个公司名,即SOC厂商(ARM是IP厂商,STM32中内核由ARM设计,外设例如 ...

  5. egret 微信小游戏 错误

    1,使用jszip错误 (1)   t.createElementNS is not a function 修改:webapp-adapter.js,增加一个方法 createElementNS: f ...

  6. SVN操作出现locked错误解决办法

    SVN操作出现locked错误解决办法:在SVN中执行 commit 操作时,在更新过程中,中断过,或者因为其他原因导致SVN 出现 locked 异常. 解决办法:1. 选中出现异常的文件,右键 - ...

  7. MongoDB操作-备份和恢复

    Mongodb数据库操作-备份 恢复 导出 导入 mongodb数据备份和恢复主要分为二种:一种是针对库的mongodump和mongorestore,一种是针对库中表的mongoexport和mon ...

  8. awr报告没有数据11.2.0.3

    有个朋友,反馈AWR没有数据: 咨询版本:oracle企业版本11.2.0.3 SQL> select * from v$version; BANNER -------------------- ...

  9. 【爬虫集合】抖音API分析

    1. 分析接口 Charles注册码 Registered Name: https://zhile.io License Key: 48891cf209c6d32bf4 抖音API分析 抖音.猫眼网页 ...

  10. C# 在运行中拖拽,改变控件大小位置类(转载)

    原文地址:https://blog.csdn.net/zgke/article/details/3718989 copy的code /// <summary> /// 移动改变控件大小 / ...