前面几篇文章简单的介绍了spring中bean注解以及怎么在spring实现bean自动注入对应的bean,未查看的同学可以点击查看:
-
深入理解spring注解之@Bean注解
-
三分钟了解spring-bean生命周期之初始化和销毁的三种方式
-
三分钟学会@Autowired@Qualifier@Primary注解
-
一分钟学会spring注解之@Autowired注解(二)
了解spring容器的IOC(依赖注入)的同学应该知道,我们的所有的bean对于spring容器是无意识的,啥叫无意识呢,就是你完全可以把spring容器替换成其他的容器而不需要改变你的代码,并且bean之间也是没有耦合的,既然这样,那问题就来,假如现在我们需要对spring有意识,换句话说就是现在我们的业务可能需要用到spring底层的一些功能那么我们该怎么实现?这个其实优秀的spring框架早就帮我们想到了,那就是spring提供了一系列的xxxAware接口供我们自己来实现使用
首先我来看一张图如下:
从这个图中我们可以看到spring给我们提供了多达12种Aware接口,这边我们主要演示一下BeanNameAware,ResourceLoaderAware,ApplicationContextAware,BeanFactoryAware的使用,其他的xxxAware感兴趣的同学可以自己下去试试
首先我们新建一个MyAware如下:
/**
* 定义一个继承了BeanNameAware,
* ResourceLoaderAware,
* ApplicationContextAware,
* BeanFactoryAware
* 的bean
* @author zhangqh
* @date 2018年5月19日
*/
public class MyAware implements BeanNameAware,ResourceLoaderAware,ApplicationContextAware,BeanFactoryAware{
private String beanName;
private ResourceLoader loader;
private ApplicationContext applicationContext;
private BeanFactory beanFactory;
// BeanFactoryAware中的方法
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
this.beanFactory = beanFactory;
}
// ApplicationContextAware中的方法
public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException {
this.applicationContext = applicationContext;
}
// ResourceLoaderAware中的方法
public void setResourceLoader(ResourceLoader resourceLoader) {
this.loader = resourceLoader;
}
public void printResult() throws IOException {
System.out.println("BeanNameAware Bean的名称是:" + beanName);
Resource resource = loader.getResource("classpath:test.txt");
System.out.println("----------------------------------------" );
System.out.print("ResourceLoaderAware ResourceLoader加载的内容是:");
String line = null;
BufferedReader reader = new BufferedReader(new InputStreamReader(
resource.getInputStream()));
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
reader.close();
Environment environment = applicationContext.getEnvironment();
System.out.println("----------------------------------------" );
System.out.println("ApplicationContextAware 系统是:" + environment.getProperty("os.name"));
boolean flow = beanFactory.isSingleton(beanName);
System.out.println("----------------------------------------" );
System.out.println("BeanFactoryAware bean【" + beanName+" 】是单例嘛:"+flow);
}
// BeanNameAware中的方法
public void setBeanName(String name) {
this.beanName = name;
}
}
src\main\resources目录下边我们有一个test.txt文件内容是:
i'm test data
测试方法如下:
AnnotationConfigApplicationContext applicationContext2 = new AnnotationConfigApplicationContext(MainScanConfig.class);
MyAware myAware = (MyAware) applicationContext2.getBean("myAware");
myAware.printResult();
运行结果如下:
BeanNameAware Bean的名称是:myAware
----------------------------------------
ResourceLoaderAware ResourceLoader加载的内容是:i'm test data
----------------------------------------
ApplicationContextAware 系统是:Windows 7
----------------------------------------
BeanFactoryAware bean【myAware 】是单例嘛:true
从以上测试结果可以发现我们需要注入spring相关的服务已经成功的加入到了我们自定义的MyAware中了
好了,看了这么的我们稍微简单的总结下xxxAware接口
-
都是Aware接口的子接口,即都继承了Aware接口
-
父接口Aware中没有定义任何方法
-
接口内均定义了一个set方法,set参数就是我们需要获取的对象
以上是今天文章的所有内容,欢迎大家吐槽
推荐阅读
全网最全技术视频和电子书免费发放
更多优质文章请关注以下公众号查阅:
相关推荐
使用了ApplicationContextAware接口,获取spring管理的bean; 多项目整合夸spring容器获取bean的实现方式。
通过aware接口,可以对spring相应资源(可能包含相关核心资源)进行操作(一定要慎重) 首先创建一个类,实现ApplicationContextAware接口 , 该借口需要实现 setApplicationContext方法,该方法的参数由容器传递...
NULL 博文链接:https://ylxy3058.iteye.com/blog/2225314
主要介绍了Spring Aware标记接口使用案例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
主要介绍了Java编程实现Aware接口自定义获取bean的两种方式,通过BeanFactoryAware和ApplicationContextAware,具有一定参考价值,需要的朋友可以了解下。
不使用XML定义档进行 Bean设置 Aware 相关介面 BeanPostProcessor BeanFactoryPostProcessor PropertyPlaceholderConfigurer PropertyOverrideConfigurer CustomEditorConfigurer ...
Self-Aware Computing Systems ISBN-10 书号: 3319474723 ISBN-13 书号: 9783319474724 Edition 版本: 1st ed. 2017 出版日期: 2017-01-23 pages 页数: (722 ) $219.99 This book provides formal and informal ...
Wi-Fi_Aware_Specification_v3.2
CVPR 2022 Image Dehazing Transformer with Transmission-Aware 3D Position Embedding 源代码,很有学习价值,大佬任文琦团队的最新去雾杰作,从3D定位的角度去思考去雾。CVPR 2022 Image Dehazing Transformer ...
Since sequential information plays an ...art sequential recommendation methods and context-aware recommendation methods on two public datasets, i.e., the Taobao dataset and the Movielens-1M dataset.
Context-Aware Saliency Detection的Matlab代码,可直接运行,图片自己放,
本文件是论文《Tag-Aware Personalized Recommendation Using a Deep-Semantic Similarity Model with Negative Sampling》的原文翻译,是我通过 Google 翻译及我自己的理解翻译而来的。在翻译的内容中有很多英文...
UPF based power aware (PA) verifcation adopts several power dissipation reduction techniques based on the target design implementation and UPF power specifcation or intent, as discussed in Chap....
Become aware of the seamlessness and power of Spring by referencing the easy-to- understand sample application we provide. Learn how to replace common EJB features with Spring alternatives, including...
Two types of knowledge, triples from knowledge graphs and texts from unstructured documents, have been studied for knowledge aware open-domain conversation generation, in which triple attributes or ...
3.1 Spring Aware .... ..................................... 48 3.2 多线程 .... .... ......... 51 3.3 计划任务 .... .... ..... 54 3.4 条件注解@Conditional .... .................... 56 3.5 组合注解与元...
3.1 Spring Aware .... ..................................... 48 3.2 多线程 .... .... ......... 51 3.3 计划任务 .... .... ..... 54 3.4 条件注解@Conditional .... .................... 56 3.5 组合注解与元...
显著性检测经典论文:Context-Aware Saliency Detection 自己编写的matlab代码实现,包扩所有函数
Context-aware composition of mobile service
Wi-Fi CERTIFIED Wi-Fi Aware™ devices leverage Wi-Fi to discover people, information, or services, based on physical context and tailored to individual preferences. The technology behind Wi-Fi Aware...