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理解的更多相关文章

  1. spring aware 的个人理解

    今天学习到了spring aware ,特地百度了下这方面的知识,现在谈下我的理解. Spring的依赖注入的最大亮点就是你所有的Bean对Spring容器的存在是没有意识的.即你可以将你的容器替换成 ...

  2. Spring Aware 到底是什么?

    通过如下前序两篇文章: Spring Bean 生命周期之"我从哪里来"? Spring Bean 生命周期之"我要到哪里去"? 我们了解了 Spring Be ...

  3. 怎么回答面试官:你对Spring的理解?

    最近看了点Spring的源码,正好来稍微扯一扯,帮一部分培训班的朋友撕开一道口子,透透气.我自己都是看的培训班视频,所以也算培训班出身吧.所以下文开口闭口"培训班",不要觉得是我在 ...

  4. Spring知识点回顾(08)spring aware

    Spring知识点回顾(08)spring aware BeanNameAware 获得容器中的bean名称 BeanFactoryAware 获得当前的bean factory Applicatio ...

  5. Java框架-Spring MVC理解001

    Spring MVC理解 1.servlet--Spring MVC的本质 2.Spring MVC其实是一个工具,具体的理解可以分为两步:第一步,了解这个工具是怎么创建出来的:第二步,了解这个工具是 ...

  6. Spring Boot实战笔记(五)-- Spring高级话题(Spring Aware)

    一.Spring Aware Spring 依赖注入的最大亮点就是你所有的 Bean 对 Spring容器的存在是没有意识的.即你可以将你的容器替换成其他的容器,如Google Guice,这时 Be ...

  7. spring的理解

    看过<fate系列>的博友知道,这是一个七位英灵的圣杯争夺战争.今天主要来谈谈圣杯的容器概念,以便对spring的理解. 圣杯: 圣杯本身是没有实体的,而是将具有魔术回路的存在(人)作为“ ...

  8. mybatis中两种取值方式?谈谈Spring框架理解?

    1.mybatis中两种取值方式? 回答:Mybatis中取值方式有几种?各自区别是什么? Mybatis取值方式就是说在Mapper文件中获取service传过来的值的方法,总共有两种方式,通过 $ ...

  9. Spring Aware容器感知技术

    Spring Aware是什么 Spring提供Aware接口能让Bean感知Spring容器的存在,即让Bean可以使用Spring容器所提供的资源. Spring Aware的分类 几种常用的Aw ...

随机推荐

  1. C++ opencv 数字识别

    #include "cv.h" #include "highgui.h" #include "cxcore.h" #include < ...

  2. windows操作

    5.windows激活 数字权利许可工具激活 https://jingyan.baidu.com/article/9113f81b4d49232b3314c75e.html 4.网络连接不上 原因,v ...

  3. JS高级学习笔记(6)- 事件循环

    参考文章:深入理解JS引擎的执行机制        JavaScript 异步.栈.事件循环.任务队列 我的笔记:ES系列之Promise async 和 await Event Loop 前提 js ...

  4. localStorage中使用json

    function setLocalJson(name, json) { json = JSON.stringify(json); localStorage.setItem(name, json)} f ...

  5. SpringBoot+Shiro+DB (二)

    之前我们完成了Spring+Shiro的最基本配置搭建,现在我们再增加上DB,毕竟没有哪个系统会将用户.角色.权限等信息硬编码到代码里.DB选用myslq. 数据库准备 脚本如下.依然是两个用户:ad ...

  6. CodeForces - 446A DZY Loves Sequences(dp)

    题意:给定一个序列a,求最长的连续子序列b的长度,在至多修改b内一个数字(可修改为任何数字)的条件下,使得b严格递增. 分析: 1.因为至多修改一个数字,假设修改a[i], 2.若能使a[i] < ...

  7. h5-任意元素居中

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. Maven:Failure executing javac, but could not parse the error:javac: 无效的目标发行版: 1.8

    eclipse中对着项目maven——>>maven install时出现错误:Failure executing javac, but could not parse the error ...

  9. 在java中如何根据手机号查询号码归属地

    1.maven项目中配置 <dependency><groupId>com.googlecode.libphonenumber</groupId><artif ...

  10. HDU 2795 Billboard 线段树活用

    题目大意:在h*w 高乘宽这样大小的 board上要贴广告,每个广告的高均为1,wi值就是数据另给,每组数组给了一个board和多个广告,要你求出,每个广告应该贴在board的哪一行,如果实在贴不上, ...