先看下ApplicationContextAware的源码:

 
 
  1. package org.springframework.context;
  2. import org.springframework.beans.BeansException;
  3. import org.springframework.beans.factory.Aware;
  4. public abstract interface ApplicationContextAware extends Aware
  5. {
  6. public abstract void setApplicationContext(ApplicationContext paramApplicationContext)
  7. throws BeansException;
  8. }
我们可以看到,ApplicationContextAware继承了Aware接口,我们在看下这个接口中怎么定义的:
 
  1. package org.springframework.beans.factory;
  2. public abstract interface Aware
  3. {
  4. }
 
网上查了一下,继承了ApplicationContextAware接口的类,在加载spring配置文件时,会自动调用接口中的setApplicationContext方法,并可自动获得ApplicationContext对象,前提是在spring配置文件中指定了这个类。
 
看下这段代码:
 
  1. public class SpringContextUtils implements ApplicationContextAware
  2. {
  3. private static ApplicationContext appContext;
  4. public void setApplicationContext(ApplicationContext applicationContext)
  5. throws BeansException
  6. {
  7. SpringContextUtils.appContext = applicationContext;
  8. }
  9. public static ApplicationContext getApplicationContext()
  10. {
  11. checkApplicationContext();
  12. return appContext;
  13. }
  14. @SuppressWarnings("unchecked")
  15. public static <T> T getBean(String beanName)
  16. {
  17. checkApplicationContext();
  18. return (T)appContext.getBean(beanName);
  19. }
  20. private static void checkApplicationContext()
  21. {
  22. if (null == appContext)
  23. {
  24. throw new IllegalStateException("applicaitonContext未注入");
  25. }
  26. }
  27. }

spring配置文件中定义:

 
  1. <!-- 定义Spring工具类 -->
  2. <bean class="com.njxph.utils.SpringContextUtils" />

以上,就可以获取bean了。

 
但是我有以下几点疑问:
 
1、ApplicationContextAware继承了Aware接口,但是Aware接口是空的,什么都没定义,为什么ApplicationContextAware还要继承Aware接口呢?
 
2、为什么继承了ApplicationContextAware接口的类,在加载spring配置文件时,会自动调用接口中的setApplicationContext方法呢?

ApplicationContextAware学习--存疑问题的更多相关文章

  1. 【11-10】spring学习笔记-ApplicationContextAware

    package util; /** * @author aloha_world_ * @date 2016年11月10日 下午7:50:08 * @version v1.00 * @descripti ...

  2. Quartz 学习记录1

    原因 公司有一些批量定时任务可能需要在夜间执行,用的是quartz和spring batch两个框架.quartz是个定时任务框架,spring batch是个批处理框架. 虽然我自己的小玩意儿平时不 ...

  3. Spring MVC 学习 -- 创建过程

    Spring MVC 学习 -- 创建过程 Spring MVC我们使用的时候会在web.xml中配置 <servlet> <servlet-name>SpringMVC< ...

  4. 0030 Java学习笔记-面向对象-垃圾回收、(强、软、弱、虚)引用

    垃圾回收特点 垃圾:程序运行过程中,会为对象.数组等分配内存,运行过程中或结束后,这些对象可能就没用了,没有变量再指向它们,这时候,它们就成了垃圾,等着垃圾回收程序的回收再利用 Java的垃圾回收机制 ...

  5. MySQL Nested-Loop Join算法学习

    不知不觉的玩了两年多的MySQL,发现很多人都说MySQL对比Oracle来说,优化器做的比较差,其实某种程度上来说确实是这样,但是毕竟MySQL才到5.7版本,Oracle都已经发展到12c了,今天 ...

  6. Spring学习总结(六)——Spring整合MyBatis完整示例

    为了梳理前面学习的内容<Spring整合MyBatis(Maven+MySQL)一>与<Spring整合MyBatis(Maven+MySQL)二>,做一个完整的示例完成一个简 ...

  7. Caffe学习系列(15):添加新层

    如何在Caffe中增加一层新的Layer呢?主要分为四步: (1)在./src/caffe/proto/caffe.proto 中增加对应layer的paramter message: (2)在./i ...

  8. SpringMvc学习心得(五)控制器产生与构建

    SpringMvc学习心得(五)控制器产生与构建 标签: springspring mvc框架 2016-03-22 15:29 140人阅读 评论(0) 收藏 举报  分类: Spring(4)  ...

  9. Android:学习AIDL,这一篇文章就够了(下)

    前言 上一篇博文介绍了关于AIDL是什么,为什么我们需要AIDL,AIDL的语法以及如何使用AIDL等方面的知识,这一篇博文将顺着上一篇的思路往下走,接着介绍关于AIDL的一些更加深入的知识.强烈建议 ...

随机推荐

  1. leetcode179

    class Solution { public: string largestNumber(vector<int>& nums) { int n=nums.size(); vect ...

  2. js实现点击按钮弹出上传文件的窗口

    转自:https://www.jb51.net/article/100916.htm 1.详细描述 在页面上设置一个“选择文件”按钮,点击该按钮,会弹出本地磁盘信息用于选择文件. 2.代码 ? 1 2 ...

  3. margin-top和padding-top

    padding- top 在原有的基础上进一步的扩张 margin - top 在原有的位置上发生上下的平移 <!DOCTYPE html> <html lang="en& ...

  4. as3 运算与检查String 是否能够正确转换成数 值

    如果忘了对一个Number 型变量初始化,那么这个变量参与的任何数学运算的结果都是NaN:如果最终结果赋值给有声明类型的变量,那么为该变量的默认值(仅限uint ,int). var a:Number ...

  5. MAC CURL : Error:35 SSL certificate problem: Couldn't understand the server certificate format

    起因,使用极光推送最新的版本,里面curl使用https请求,而导致证书出错.一看就懵逼了,从来没遇到过这样的问题,二话不说直接百度,然后就更加懵逼了,搜出来的没有中文内容,对于我这种英文渣来说,简直 ...

  6. 【342】Linear Regression by Python

    Reference: 用scikit-learn和pandas学习线性回归 首先获取数据存储在 pandas.DataFrame 中,获取途径(CSV 文件.Numpy 创建) 将数据分成 X 和 y ...

  7. ubuntu 软件包系统已损坏 解决方法

    sudo apt-get clean sudo apt-get -f install sudo apt-get upgrade

  8. Electron mouse events 参数解析

    1.https://electronjs.org/docs/api/web-contents 2.通常用: monitorEvents(document.body, 'mouse')  检测正常的值: ...

  9. MVVM 的理解

    MVVM 是 Model - View - ViewModel 的缩写 可以看到他和之前的MVC很像,的确有人称之为一个加强优化版的MVC. 是一种模块化开发代码分层的思想或者框架! MVVM 的优点 ...

  10. configparser模块 logging模块

    configparser模块 固定格式的配置文件 有一个对应的模块去帮你做这个文件的字符串处理 config = configparser.Configparser() config.read(“ex ...