1. 简要说明
实现了ApplicationContextAware
接口的Bean
,当Spring
容器初始化的时候,会自动的将ApplicationContext
注入进来,所以当一个类实现了这个接口(ApplicationContextAware
)之后,这个类就可以方便地获得ApplicationContext
中的所有Bean
。
从ApplicationContextAware
获取ApplicationContext
上下文的情况,仅仅适用于当前运行的代码和已启动的Spring
代码处于同一个Spring
上下文,否则获取到的ApplicationContext
是空的。
在Spring
项目中,只有两个类都是Spring
容器中的Bean
才可以互相进行依赖注入。但是在一些特殊场景下,当前类自己不是容器中的Bean
,但是却需要通过注入一个容器中的Bean
来实现调用这个Bean
中的方法。此时就可通过本文中的工具类来实现。
2. 相关源码
ApplicationContextAware 接口的源码:
package org.springframework.context;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.Aware;
/**
* 由任何希望被通知运行它的ApplicationContext的对象实现的接口。
*
* 实现这个接口是有意义的,例如当一个对象需要访问一组协作bean时。
* 请注意,通过bean引用进行配置比仅出于bean查找目的而实现此接口更可取。
*
* 如果一个对象需要访问文件资源,比如想要调用getResource,想要发布一个应用程序事件,
* 或者需要访问 MessageSource,这个接口也可以实现。然而,在这样一个特定的场景中,
* 最好实现更具体的ResourceLoaderAware、ApplicationEventPublisherAware或MessageSourceAware接口。
*
* 请注意,文件资源依赖关系也可以公开类型为org.springframework.core.io.Resource的bean属性,
* 由bean工厂通过String自动类型转换来填充。这样就不需要仅仅为了访问特定的文件资源而实现任何回调接口。
*
* link org.springframework.context.support.ApplicationObjectSupport
* 是一个实现这个接口的应用程序对象的方便的基类。
*
* 获取所有bean生命周期方法的列表查看org.springframework.beans.factory.BeanFactory。
*
* @author Rod Johnson
* @author Juergen Hoeller
* @author Chris Beams
* @see ResourceLoaderAware
* @see ApplicationEventPublisherAware
* @see MessageSourceAware
* @see org.springframework.context.support.ApplicationObjectSupport
* @see org.springframework.beans.factory.BeanFactoryAware
*/
public interface ApplicationContextAware extends Aware {
/**
* 设置运行此对象的ApplicationContext。
* 通常这个调用将用于初始化对象。
* 在填充普通bean属性之后,但在初始化回调
* 比如org.springframework.beans.factory.InitializingBean#afterPropertiesSet()
* 或自定义初始化方法之前调用。如果适用的话,在ResourceLoaderAware#setResourceLoader、
* ApplicationEventPublisherAware#setApplicationEventPublisher和MessageSourceAware
* 之后调用。
*
* @param 该对象使用的applicationContext对象
* @throws 在上下文初始化错误的情况下抛出的ApplicationContextException异常
* @throws 如果应用程序上下文方法引发BeansException
* @see org.springframework.beans.factory.BeanInitializationException
*/
void setApplicationContext(ApplicationContext applicationContext) throws BeansException;
}
Aware 接口的源码:
package org.springframework.beans.factory;
/**
* 一个标记超接口,指示一个bean有资格通过一个回调样式的方法由Spring容器通知一个特定的框架对象。
* 实际的方法签名由各个子接口确定,但通常应该只包含一个接受单个参数的返回void的方法。
*
* 注意,仅仅实现Aware并没有提供默认的功能。
* 相反,处理必须显式地进行,例如在
* org.springframework.beans.factory.config.BeanPostProcessor。
* 参考org.springframework.context.support.ApplicationContextAwareProcessor
* 是一个处理特定Aware接口回调的例子。
*
* @author Chris Beams
* @author Juergen Hoeller
* @since 3.1
*/
public interface Aware {
}
3. 代码示例
package com.example.demo.util;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
@Component
public class SpringContextUtils implements ApplicationContextAware {
/**
* 上下文对象实例
*/
private static ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
SpringContextUtils.applicationContext = applicationContext;
}
/**
* 获取applicationContext
*/
public static ApplicationContext getApplicationContext() {
//判断是否为null
if (applicationContext == null) {
throw new IllegalStateException("applicaitonContext未注入,请在applicationContext.xml中定义SpringContextHolder.");
}
return applicationContext;
}
/**
* 通过name获取Bean
*/
public static Object getBean(String name) {
return getApplicationContext().getBean(name);
}
/**
* 通过class获取Bean
*/
public static <T> T getBean(Class<T> clazz) {
return getApplicationContext().getBean(clazz);
}
/**
* 通过name和class获取Bean
*/
public static <T> T getBean(String name, Class<T> clazz) {
return getApplicationContext().getBean(name, clazz);
}
}
使用方法:
// 获取 Service
MyTestService myTestService = (MyTestService)SpringContextUtils.getBean("myTestServiceImpl");