23、自动装配-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
*/
@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底层组件&原理的更多相关文章
- Spring9——通过用Aware接口使用Spring底层组件、环境切换
通过用Aware接口使用Spring底层组件 能够供我们使用的组件,都是Aware的子接口. ApplicationContextAware:实现步骤: (1)实现Applic ...
- 【Spring注解驱动开发】自定义组件如何注入Spring底层的组件?看了这篇我才真正理解了原理!!
写在前面 最近,很多小伙伴出去面试都被问到了Spring问题,关于Spring,细节点很多,面试官也非常喜欢问一些很细节的技术点.所以,在 Spring 专题中,我们尽量把Spring的每个技术细节说 ...
- Spring第八发—自动装配及让Spring自动扫描和管理Bean
依赖注入–自动装配依赖对象(了解即可) 对于自动装配,大家了解一下就可以了,实在不推荐大家使用.例子: byName:按名称装配,可以根据属性的名称,在容器中寻找跟该属性名相同的bean,如果没有找到 ...
- 在自定义组件中获取spring底层组件
要想在自定义组件中获取spring底层的各种组件,只需让自定义组件实现一系列接口即可,这些接口都是Aware的子接口.常见的有: 1. ApplicationContextAware——用于获取IOC ...
- 使用传统的三层架构出现的问题.引入Spring底层实现原理来解决(工厂模式+反射+XML配置文件/注解)
以前写的代码 mapper层 public interface PersonMapper { void selectPersonList(); } public class PersonMapperI ...
- 【spring 注解驱动开发】spring自动装配
尚学堂spring 注解驱动开发学习笔记之 - 自动装配 自动装配 1.自动装配-@Autowired&@Qualifier&@Primary 2.自动装配-@Resource& ...
- Spring笔记(2) - 生命周期/属性赋值/自动装配及部分源码解析
一.生命周期 @Bean自定义初始化和销毁方法 //====xml方式: init-method和destroy-method==== <bean id="person" c ...
- Spring注解驱动开发(三)-----自动装配
自动装配 概念 Spring利用依赖注入(DI),完成对IOC容器中中各个组件的依赖关系赋值. @Autowired-----自动注入 1.默认优先按照类型去容器中找对应的组件 application ...
- Spring注解开发系列Ⅴ --- 自动装配&Profile
自动装配: spring利用依赖注入和DI完成对IOC容器中各个组件的依赖关系赋值.自动装配的优点有: 自动装配可以大大地减少属性和构造器参数的指派. 自动装配也可以在解析对象时更新配置. 自动装配的 ...
随机推荐
- ota编译及差分包制作
OTA L 版本OTA build diff OTA升级的步骤如下: 1.new整个project. 2.Step1: ./vendor/mediatek/proprietary/scripts/si ...
- Java 最常见 200+ 面试题 + 全解析
本文分为十九个模块,分别是: Java 基础.容器.多线程.反射.对象拷贝.Java Web .异常.网络.设计模式.Spring/Spring MVC.Spring Boot/Spring Clou ...
- setting中executable for debug session对话框
- Python之数字的四舍五入(round(value, ndigits) 函数)
round(value, ndigits) 函数 print(round(1.23)) # 1 print(round(1.27)) # 1 print(round(1.23,1)) # 1.2 第二 ...
- MyBatis_02 框架
今日内容 动态SQL语句 Xml方式 注解方式 MyBatis的缓存 MyBatis的关联查询 MyBatis逆向工程 动态SQL语句 动态SQL是什么 就是相对与固定SQL.就是通过传入的参数不一样 ...
- IMPDPORA-27046,dump文件损坏
客户提出导入报错 一.报错如下 SYMPTOMS DataPump Import (IMPDP) fails with the following errors: ORA-: invalid oper ...
- 怎样对小数进行向上取整 / 向下取整 / 四舍五入 / 保留n位小数 / 生成随机数
1. 向上取整使用: Math.ceil() Math.ceil(0.1); Math.ceil(1.9); 2. 向下取整使用: Math.floor() Math.floor(0.1); Math ...
- 使用github经验
使用github经验 良好的使用习惯,就像是每天来看朋友圈一样,不一定每天都有东西要提交,但是一定要一直有一个 repository 在维护,持续的提交代码.同时也要注意自己的 repository的 ...
- hdu 4857 反向拓扑问题
尤其要注意拓扑的分层问题 不难理解 就是不怎么好想到 拓扑的思路这里就不累述了 #include <iostream> #include <cstdio> #include & ...
- C#使用HtmlAgilityPack快速爬虫
HtmlAgilityPack真是一把网抓利器,可以迅速地从网页抓到想要的文本或数据,使用起来十分方便,引用时在NuGet安装添加并在头部引用using HtmlAgilityPack;即可. 针对网 ...