1、Spring中使用applicationContext对象

public class SpringContextUtil implements ApplicationContextAware
{
private static ApplicationContext applicationContext; @Override
public void setApplicationContext(ApplicationContext applicationContext)
{
applicationContext = applicationContext;
} public static ApplicationContext getApplicationContext()
{
return applicationContext;
}
}

2、为什么继承ApplicationContextAware就可以使用applicationContext对象?

使用后置处理器

3、什么是后置处理器?
      后置处理器主要是对bean进行增强,包括在bean初始化前和初始化后进行增强,如修改bean属性、对bean的方法进行代理等。

public interface BeanPostProcessor {
// bean初始化前增强
default Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
return bean;
}
// bean初始化后增强
default Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
return bean;
}

4、后置处理器怎样调用setApplicationContext赋值

内置后置处理器:org.springframework.context.support.ApplicationContextAwareProcessor

class  ApplicationContextAwareProcessor implements BeanPostProcessor 

public Object postProcessBeforeInitialization(final Object bean, String beanName) throws BeansException {

        else {
invokeAwareInterfaces(bean);
} return bean;
} private void invokeAwareInterfaces(Object bean) {
if (bean instanceof Aware) {
// 对继承于ApplicationContextAware的bean调用setApplicationContext方法进行赋值
if (bean instanceof ApplicationContextAware) {
((ApplicationContextAware) bean).setApplicationContext(this.applicationContext);
}
}
}

5、参考资料

https://blog.csdn.net/baomw/article/details/84262006

Spring中为什么继承了ApplicationContextAware接口就可以使用ApplicationContext对象?的更多相关文章

  1. 【Java EE 学习 50】【Spring学习第二天】【使用注解的DI实现】【spring中的继承】【动态代理伪hibernate实现】

    一.使用注解的DI实现 1.@Resource 使用该注解能够实现引用型属性的DI实现,该注解能够根据属性名和属性类型自动给属性赋值.一般使用@Resource(name="student& ...

  2. 如何手动获取Spring容器中的bean(ApplicationContextAware 接口)

    ApplicationContextAware 接口的作用 先来看下Spring API 中对于 ApplicationContextAware 这个接口的描述:   即是说,当一个类实现了这个接口之 ...

  3. Spring中使用两种Aware接口自定义获取bean

    在使用spring编程时,常常会遇到想根据bean的名称来获取相应的bean对象,这时候,就可以通过实现BeanFactoryAware来满足需求,代码很简单: @Servicepublic clas ...

  4. Spring中好玩的注解和接口

    测试中: 一.unit中集中基本注解,是必须掌握的. @BeforeClass – 表示在类中的任意public static void方法执行之前执行 @AfterClass – 表示在类中的任意p ...

  5. 逆向工程生成的mybatis中mapper文件。mapper接口,实例化成对象

    逆向工程生成的mybatis中mapper文件中,*mapper文件只是接口,而不是类文件.但是却可以通过spring的容器获得实例. 例如: //1.获得mapper代理对象,从spring容器获得 ...

  6. Java中的继承抽象类和接口

    一.总结 1.使用extends关键字继承,eg: class Student extends Persion { ...}; 2.Java编程规范中类的首字母大写,方法的首字母小写单词首字母代谢,e ...

  7. Spring 中的国际化Message的简单例子(ApplicationContext) 不跟框架集成的版本

    首先,建立一个描述message的XML文件,名为messages.xml     <?xml version="1.0" encoding="UTF-8" ...

  8. 在Spring中使用静态工厂时发生的无法得到对象的问题

    因为我测试时,使用的是之前的包Spring 3.x一类的jar包,发现我的代码毫无问题,但是就是运行不出来,后面去Spring官网上发现,JDK6+至少都要使用Spring4.x了,而为了配合JDK8 ...

  9. Spring中的BeanPostProcessor详解

    Spring中的BeanPostProcessor详解 概述 BeanPostProcessor也称为Bean后置处理器,它是Spring中定义的接口,在Spring容器的创建过程中(具体为Bean初 ...

随机推荐

  1. Java 之 线程的生命周期(线程状态)

    一.线程的生命周期 (1)新建状态 new 好了一个线程对象,此时和普通的 Java对象并没有区别. (2)就绪 就绪状态的线程是具备被CPU调用的能力和状态,也只有这个状态的线程才能被CPU调用.即 ...

  2. 安装socketio出现module 'importlib._bootstrap' has no attribute 'SourceFileLoader' 错误

    安装socketio出现module 'importlib._bootstrap' has no attribute 'SourceFileLoader' 错误 执行: pip install --u ...

  3. pandas 之 多层索引

    In many applications, data may be spread across a number of files or datasets or be arranged in a fo ...

  4. mysqldump 备份

    1.  直接备份某个库或表 ,或多个库多个表mysqldump -uroot -pPassword [database name] > [dump file]mysqldump -uroot - ...

  5. H3C 802.1x认证接入过程

  6. Android PKMS服务

    它的作用? 关于PKMS的全称是啥应该咱们不陌生,PackageManagerService,和AMS一样是Android系统的核心服务,它主要负责系统中Package的管理,应用程序的安装.卸载.信 ...

  7. C++网站学习

    0.C++   一个专门做C++的网站 一.以下内容来自LEARN C++ 的<更好编写C++程序的5个建议>部分 1.C++的一些标准: Coding Standards C++ Cor ...

  8. Spring Boot 之:Actuator 监控

    在 Spring Boot 2.x 中为了安全,Actuator 只开放了两个端点 /actuator/health 和 /actuator/info.可以在配置文件中设置打开. Actuator 默 ...

  9. Gym100739H Hard Molecules

    Hard Molecules 给定一个连通图中每个点的度数,求一个满足条件的图,图可以有重边,不能有自环. n<=5000, di<=109 题解 如果不要求图连通,那么只需要判断 \[ ...

  10. 不重装nginx实现动态添加模块

    如果项目在提供服务的过程中,因为需求使然,需要对nginx进行模块的动态添加,可以按照如下流程进行操作 一.查看nginx当前已经安装了那些模块 1) 进入nginx执行文件目录 cd   /usr/ ...