对Spring aware理解

aware翻译过来时就是意识到,我对他的理解就是spring的感知器。是不是很诡异这个名字起得^_^
先来看看aware接口的结构

spring提供了许多的aware,Aware.java也只是做一个标志,他并没有定义任何的方法

spring提供的这些aware只展示红框框起来的额三个
一、BeanNameAware
从名字的定义上来看他就是bean对象名字的感知器,他可以获取到bean对象的名字的,目前我只知道在记录日志的时候确实好用,其他的功能有待挖掘。。。。
package com.lhf.aware;
import org.springframework.beans.factory.BeanNameAware;
public class DomeBeanNameAware implements BeanNameAware {
private String beanName;
@Override
public void setBeanName(String name) {
this.beanName = name;
}
public void test(){
System.out.println("bean的名称:"+beanName);
}
}
》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》
package com.lhf.aware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan("com.lhf.aware")
public class Config {
@Bean
public DomeBeanNameAware domeBeanNameAware(){
return new DomeBeanNameAware();
}
}
》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》
package com.lhf.aware;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class App {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Config.class);
DomeBeanNameAware bean = context.getBean(DomeBeanNameAware.class);
bean.test();
context.close();
}
}
使用很简单,正如上边代码所示,只需要实现BeanNameAware接口并重写setBeanName方法即可,该方法的入参就是bean对象的名称。
二、ApplicationContextArare
spring容器的感知器,可以获取到spring的容器对象
package com.lhf.aware; import org.springframework.beans.BeansException;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.stereotype.Component; @Component
public class DomeApplicationContextAware<T> implements ApplicationContextAware, InitializingBean, DisposableBean { private ApplicationContext context; @Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.context = applicationContext;
if (context instanceof GenericApplicationContext) {
// 注销spring容器
((GenericApplicationContext) applicationContext).registerShutdownHook();
}
} @Override
public void afterPropertiesSet() throws Exception {
System.out.println("bean对象被创建了");
} @Override
public void destroy() throws Exception {
System.out.println("bean对象被销毁了");
}
} 》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》 package com.lhf.aware; import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration; @Configuration
@ComponentScan("com.lhf.aware")
public class Config { } 》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》 package com.lhf.aware; import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class App {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Config.class);
context.getBean(DomeApplicationContextAware.class); // 为了证明不是这里关闭的吧下边的一行注释
// context.close();
}
}
同样的使用ApplicationContextAware同样是实现该接口,并重写set方法。在这个东东很秀,要知道,非spring的对象是不能直接使用spring的bean对象的,而通过这个就可以用了。
三、BeanFactoryAware
类似的,beanFactory的感知器用法和上边两种一样,都是实现BeanFactoryAware,重写set方法,代码就不贴了。
总结aware的功能还是很方便的,而且使用起来也很简单粗暴。博客写的不好,大佬勿喷,欢迎吐槽
对Spring aware理解的更多相关文章
- spring aware 的个人理解
今天学习到了spring aware ,特地百度了下这方面的知识,现在谈下我的理解. Spring的依赖注入的最大亮点就是你所有的Bean对Spring容器的存在是没有意识的.即你可以将你的容器替换成 ...
- Spring Aware 到底是什么?
通过如下前序两篇文章: Spring Bean 生命周期之"我从哪里来"? Spring Bean 生命周期之"我要到哪里去"? 我们了解了 Spring Be ...
- 怎么回答面试官:你对Spring的理解?
最近看了点Spring的源码,正好来稍微扯一扯,帮一部分培训班的朋友撕开一道口子,透透气.我自己都是看的培训班视频,所以也算培训班出身吧.所以下文开口闭口"培训班",不要觉得是我在 ...
- Spring知识点回顾(08)spring aware
Spring知识点回顾(08)spring aware BeanNameAware 获得容器中的bean名称 BeanFactoryAware 获得当前的bean factory Applicatio ...
- Java框架-Spring MVC理解001
Spring MVC理解 1.servlet--Spring MVC的本质 2.Spring MVC其实是一个工具,具体的理解可以分为两步:第一步,了解这个工具是怎么创建出来的:第二步,了解这个工具是 ...
- Spring Boot实战笔记(五)-- Spring高级话题(Spring Aware)
一.Spring Aware Spring 依赖注入的最大亮点就是你所有的 Bean 对 Spring容器的存在是没有意识的.即你可以将你的容器替换成其他的容器,如Google Guice,这时 Be ...
- spring的理解
看过<fate系列>的博友知道,这是一个七位英灵的圣杯争夺战争.今天主要来谈谈圣杯的容器概念,以便对spring的理解. 圣杯: 圣杯本身是没有实体的,而是将具有魔术回路的存在(人)作为“ ...
- mybatis中两种取值方式?谈谈Spring框架理解?
1.mybatis中两种取值方式? 回答:Mybatis中取值方式有几种?各自区别是什么? Mybatis取值方式就是说在Mapper文件中获取service传过来的值的方法,总共有两种方式,通过 $ ...
- Spring Aware容器感知技术
Spring Aware是什么 Spring提供Aware接口能让Bean感知Spring容器的存在,即让Bean可以使用Spring容器所提供的资源. Spring Aware的分类 几种常用的Aw ...
随机推荐
- 执行 composer update 命令的时候报 Your requirements could not be resolved to an installable set of packages. 错误
Your requirements could not be resolved to an installable set of packages. 以上原因:不匹配composer.json要求的版 ...
- Python Learning Day7
破解极验滑动验证 博客园登录url: https://account.cnblogs.com/signin?returnUrl=https%3A%2F%2Fwww.cnblogs.com%2F 代码逻 ...
- stm32h7 开发板学习
按键和 IO 之间连接一个 1K 电阻,可以防止当 IO 被配置为高电平输出的时候,按下按键,导致 VDD 和 GND 直接连通.
- 在mysql中计算百分比
通过查找资料,得到了如下解决方法: 用到了concat()和left() 两个函数 1.CONCAT(str1,str2,...) 返回来自于参数连结的字符串.如果任何参数是NULL, 返回NULL. ...
- LIS是什么?【通讯】
Ⅲ最后一点,通讯. 从字面意义来看,通讯是一种沟通形式,信息交互的媒介.在LIS中,通讯主要指的是仪器通讯,也就是仪器与电脑-LIS系统的信息交互方式,也可以称为仪器接口. 在LIS中,通讯是最基础也 ...
- js 关联数组
踩得坑: JS ,通过 new Array()创建了一个数组: var param = new Array();param["key1"] = value1;param[&quo ...
- .NET技术-2.0. 操作数据库-Dapper
.NET技术-2.0. 操作数据库-Dapper 项目参见: 1. 为什么选择Dapper 1) 性能优越: 其实在各大网站上,我们大概都会看到这样的一个对比效果图,在超过500次poco seria ...
- 十一、GUI设计-记事本程序
"""记事本程序""" from tkinter import *from tkinter.filedialog import *from ...
- 怎么调出原生态launcher
adb shell am start -n com.android.launcher3/.Launcher
- vue项目开始 首页 part1
stylus 优点:css之中使用一些变量,方便我们快速编写css 项目中我们使用css开发的辅助工具帮助我们开发网站样式 安装:终端打开我们项目的文件夹 npm install stylus --s ...