一、介绍
1、问题引入
为了获取已被实例化的Bean对象,如果使用再次加载配置文件的方法,可能会出现一个问题,如一些线程配置任务, 会启动两份,产生了冗余.
ApplicationContext appContext = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService userService = (UserService)appContext.getBean("userService");
2、作用
Spring中ApplicationContextAware主要用来获取Spring上下文(ApplicationContext )已经实例化的Bean对象。
3、简介
当一个类实现了ApplicationContextAware接口,这个类就可以方便获取ApplicationContext中的所有bean,即这个类可以获取Spring配置文件中管理的所有Bean对象。
@Component
public class DateUtil implements ApplicationContextAware {
private static ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext arg0) throws BeansException {
applicationContext = arg0;
}
public static Object getObject(String id) {
Object object = applicationContext.getBean(id);
return object;
}
}
在Spring配置文件中, 将自定义类注册到Spring中,在类中可以灵活的获取ApplicationContext, Spring能自动执行setApplicationContext方法,获得ApplicationContext对象,ApplicationContext对象是由spring注入的,且Spring配置文件中存在该对象。
4、使用场景
ApplicationContextAware获取ApplicationContext上下文的情况,适用于当前运行的代码和已启动的Spring代码处于同一个Spring上下文,否则获取到的ApplicationContext是空的。
常见的使用场景有:
- 在监听器中, 去监听到事件的发生, 然后从Spring的上下文中获取Bean对象, 去修改保存相关的业务数据.
- 在某个工具类中, 需要提供处理通用业务数据的方法等
二、使用
1、使用方法
自定义类继承ApplicationContextAware即可,这个类一般加上@Component注解。
2、demo
比如在springboot+mybatis cache中,MybatisRedisCache需要获取到RedisTemplate,而MybatisRedisCache 是运行时加载,不能通过@Autowired自动装配,这时需要手动从Spring的Context上下文中获取bean。
Mybatis Cache(二)MybatisCache+Redis-CSDN博客
package com.pluscache.demo.util;
import org.springframework.aop.support.AopUtils;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
import java.lang.annotation.Annotation;
import java.util.Map;
@Component
public class ApplicationContextUtil implements ApplicationContextAware {
private static ApplicationContext applicationContext ;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
ApplicationContextUtil.applicationContext = applicationContext;
}
public static Object getBean(String name) {
return ApplicationContextUtil.applicationContext.getBean(name);
}
public static <T> T getBean(String name, Class<T> clazz) {
return applicationContext.getBean(name, clazz);
}
public static <T> T getBean(Class<T> clazz) {
return applicationContext.getBean(clazz);
}
public static <T> Map<String, T> getBeansOfType(Class<T> clazz) {
return applicationContext.getBeansOfType(clazz);
}
public static ApplicationContext getApplicationContext() {
return applicationContext;
}
public static <T extends Annotation> T getAnnotation(Object bean, Class<T> annotationClass) {
T annotation = bean.getClass().getAnnotation(annotationClass);
if (annotation == null) {
annotation = AopUtils.getTargetClass(bean).getAnnotation(annotationClass);
}
return annotation;
}
}
redisTemplate = (RedisTemplate) ApplicationContextUtil.getBean("redisTemplate");
版权归原作者 w_t_y_y 所有, 如有侵权,请联系我们删除。