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("applicationContext 未注入");
}
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");
4. 扩展问题
工作中遇到了一个问题,是这样的:
一个老项目中通过继承org.hibernate.cache.spi.support.RegionFactoryTemplate
抽象类,实现了一个使用 Redis 做为 Spring JPA 缓存(Hibernate 二级缓存)的功能,但是里面关于 Redis 的参数配置是写死在一个配置文件中的,没有区分环境,每次本地运行得修改该配置文件中的 Redis 地址,麻烦的很,我就想着区分下环境,把相关 Redis 的配置迁移到各个环境的 application.properties 中,然后通过属性获取来实现 Factory 的初始化。通过启动日志发现在 RegionFactoryTemplate 子类的构造函数执行的时候,Spring 的 bean 还没有初始化,各种 @Value 注解的参数都没获取到,@Configuration 注解的类通过 @Autowired 也未获取到。但是通过这个工具类的 getBean 方法可以获取到,解决了问题。
package com.example.demo.config;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.aop.framework.AopContext;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
/**
* spring工具类
*/
@Component
public final class SpringUtils implements BeanFactoryPostProcessor, ApplicationContextAware {
/**
* Spring应用上下文环境
*/
private static ConfigurableListableBeanFactory beanFactory;
private static ApplicationContext applicationContext;
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
SpringUtils.beanFactory = beanFactory;
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
SpringUtils.applicationContext = applicationContext;
}
/**
* 获取bean对象
*/
public static <T> T getBean(String name) throws BeansException {
return (T) beanFactory.getBean(name);
}
/**
* 获取类型为requiredType的对象
*/
public static <T> T getBean(Class<T> clz) throws BeansException {
return beanFactory.getBean(clz);
}
/**
* 如果BeanFactory包含一个与所给名称匹配的bean定义,则返回true
*/
public static boolean containsBean(String name) {
return beanFactory.containsBean(name);
}
/**
* 判断以给定名字注册的bean定义是一个singleton还是一个prototype。 如果与给定名字相应的bean定义没有被找到,将会抛出一个异常(NoSuchBeanDefinitionException)
*/
public static boolean isSingleton(String name) throws NoSuchBeanDefinitionException {
return beanFactory.isSingleton(name);
}
public static Class<?> getType(String name) throws NoSuchBeanDefinitionException {
return beanFactory.getType(name);
}
/**
* 如果给定的bean名字在bean定义中有别名,则返回这些别名
*/
public static String[] getAliases(String name) throws NoSuchBeanDefinitionException {
return beanFactory.getAliases(name);
}
/**
* 获取AOP代理对象
*/
public static <T> T getAopProxy(T invoker) {
return (T) AopContext.currentProxy();
}
/**
* 获取当前的环境配置,无配置返回null
*/
public static String[] getActiveProfiles() {
return applicationContext.getEnvironment().getActiveProfiles();
}
/**
* 获取当前的环境配置,当有多个环境配置时,只获取第一个
*/
public static String getActiveProfile() {
final String[] activeProfiles = getActiveProfiles();
return ArrayUtils.isNotEmpty(activeProfiles) ? activeProfiles[0] : null;
}
}