1提出问题
在我们的web程序中,用spring来管理各个实例(bean), 有时在程序中为了使用已被实例化的bean, 通常会用到这样的代码:
ApplicationContext appContext = new ClassPathXmlApplicationContext("applicationContext-common.xml");
AbcService abcService = (AbcService)appContext.getBean("abcService");
但是这样就会存在一个问题:因为它会重新装载applicationContext-common.xml并实例化上下文bean,如果有些线程配置类也是在这个配置文件中,那么会造成做相同工作的的线程会被启动两次。一次是web容器初始化时启动,另一次是上述代码显示的实例化了一次。相当于重新初始化一遍!!!!这样就产生了冗余。
2解决方法
不用类似**new ClassPathXmlApplicationContext()**的方式,从已有的spring上下文取得已实例化的bean。通过ApplicationContextAware接口进行实现。当一个类实现了这个接口(ApplicationContextAware)之后,这个类就可以方便获得ApplicationContext中的所有bean。换句话说,就是这个类可以直接获取spring配置文件中,所有有引用到的bean对象。
3ApplicationContextAware的用法
package com.zs;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
/**
* @author zhaoshuai06 <zhaoshuai06@kuaishou.com>
* Created on 2021-06-08
*/
@Component
public class SpringUtils implements ApplicationContextAware {
private static ApplicationContext context;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
SpringUtils.context = applicationContext;
}
public static Object getObject(String id) {
Object object = null;
object = context.getBean(id);
return object;
}
}
在spring的配置文件中,注册方法类SpringUtils。之所以方法类SpringUtils能够灵活自如地获取ApplicationContext,就是因为spring能够为我们自动地执行了setApplicationContext。但是,spring不会无缘无故地为某个类执行它的方法的,所以,就很有必要通过注册方法类SpringUtils的方式告知spring有这样子一个类的存在,这里我们使用@Component来进行注册。
加载Spring配置文件时,如果Spring配置文件中所定义的Bean类实现了ApplicationContextAware 接口,那么在加载Spring配置文件时,会自动调用ApplicationContextAware 接口中的
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
SpringUtils.context = applicationContext;
}
方法,获得ApplicationContext对象,ApplicationContext对象是由spring注入的,前提必须在Spring配置文件中指定该类。
使用静态的成员ApplicationContext类型的对象。
4使用场景备注
从ApplicationContextAware获取ApplicationContext上下文的情况,仅仅适用于当前运行的代码和已启动的Spring代码处于同一个Spring上下文,否则获取到的ApplicationContext是空的。
5直接使用的代码
package com.kuaishou.is.e.sign.common.utils;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
@Order(Ordered.HIGHEST_PRECEDENCE)
@Component
public class SpringUtils implements ApplicationContextAware {
private static ApplicationContext context;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
SpringUtils.context = applicationContext;
}
public static ApplicationContext getContext() {
return context;
}
public static void setContext(ApplicationContext context) {
SpringUtils.context = context;
}
public static <T> T getBean(Class<T> clazz) {
return context.getBean(clazz);
}
public static Object getBean(String name) {
return context.getBean(name);
}
}