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. CCCC L3-015. 球队“食物链”(dfs+剪枝)

    题意: 某国的足球联赛中有N支参赛球队,编号从1至N.联赛采用主客场双循环赛制,参赛球队两两之间在双方主场各赛一场. 联赛战罢,结果已经尘埃落定.此时,联赛主席突发奇想,希望从中找出一条包含所有球队的 ...

  2. tensorflow--保存加载模型

    s=mnist.train.next_batch(batch_size)print(xs.shape)print(ys.shape) # #从集合中取全部变量# tf.get_collection() ...

  3. Linux系统中的变量PATH

    PATH 作用 在Linux安装一些可执行文件通常要添加路径环境变量PATH,PATH环境变量通俗的讲就是把程序的路径"备案"到系统中,这样执行这些程序时就不需要输入完整路径,直接 ...

  4. Jquery获取html标签,包含该标签本身

    $(".test").prop("outerHTML"); 原生JS DOM里有一个内置属性 outerHTML,用来获取当前节点的html代码(包含当前节点) ...

  5. dubbo小教程

    dubbo小教程 先给出阿里巴巴dubbo的主页:http://code.alibabatech.com/wiki/display/dubbo/Home-zh 自己的demo下载地址:http://d ...

  6. JS的BOM对象

    BOM对象 (一)简介:BOM对象,即浏览器对象模型: 通过javascript的对象,操作和浏览器相关的操作 B:  Browser,浏览器 O: Object,对象 M: Model,模型 (1) ...

  7. 用Python在00:00给微信好友发元旦祝福语

    2019年的元旦即将来临,这里用Python撸一串简单的代码来实现定点给微信里的所有小伙伴发祝福语 环境说明 Python版本: 不限 第三方库: itchat, schedule 注:所有祝福语来源 ...

  8. dbcp连接池的一些方法

    创建连接 // 简写版: Connection conn =null; Statement st =null; conn = DBCP.getConnection(); st=conn.createS ...

  9. SHIDOU

    1. arp    i  指定网卡     a 查看arp表,显示主机名称和ip      n 查看arp表,并且用ip显示而不是主机名称   2. 119 ~/M2/image-installer- ...

  10. 文献阅读报告 - Situation-Aware Pedestrian Trajectory Prediction with Spatio-Temporal Attention Model

    目录 概览 描述:模型基于LSTM神经网络提出新型的Spatio-Temporal Graph(时空图),旨在实现在拥挤的环境下,通过将行人-行人,行人-静态物品两类交互纳入考虑,对行人的轨迹做出预测 ...