zl程序教程

您现在的位置是:首页 >  其它

当前栏目

ApplicationContextAware中获取不到bean

获取 不到 bean
2023-09-11 14:16:49 时间

今天遇到一个非常奇怪的问题,写了一个工具类实现ApplicationContextAware接口来获取Spring上下文, 代码如下:

public class SpringContextHolder implements ApplicationContextAware {
	private static ApplicationContext applicationContext = null;
 
	/**
	 * 获取静态变量中的ApplicationContext.
	 */
	public static ApplicationContext getApplicationContext() {
		assertContextInjected();
		return applicationContext;
	}
 
	/**
	 * 从静态变量applicationContext中得到Bean, 自动转型为所赋值对象的类型.
	 */
	@SuppressWarnings("unchecked")
	public static <T> T getBean(String name) {
		assertContextInjected();
		return (T) applicationContext.getBean(name);
	}
 
	/**
	 * 从静态变量applicationContext中得到Bean, 自动转型为所赋值对象的类型.
	 */
	public static <T> T getBean(Class<T> requiredType) {
		assertContextInjected();
		return applicationContext.getBean(requiredType);
	}
 
	/**
	 * 清除SpringContextHolder中的ApplicationContext为Null.
	 */
	public static void clearHolder() {
		applicationContext = null;
	}
 
	/**
	 * 实现ApplicationContextAware接口, 注入Context到静态变量中.
	 */
	@Override
	public void setApplicationContext(ApplicationContext applicationContext) {
		SpringContextHolder.applicationContext = applicationContext;
	}
 
	/**
	 * 检查ApplicationContext不为空.
	 */
	private static void assertContextInjected() {
		Validate.validState(applicationContext != null,
				"applicaitonContext属性未注入, 请在applicationContext.xml中定义SpringContextHolder.");
	}
 
}

项目启动的时候,APPLICATION_CONTEXT是初始化了的,但是当定时任务调用的时候,获取到的就为null。 后来仔细排查,将Spring Boot Devtools依赖去了,就正常了。项目中用的是quartz,获取容器中的bean,获取不带。

查了下Spring Boot Devtools的热部署原理: 深层原理是使用了两个ClassLoader,一个Classloader加载那些不会改变的类(第三方Jar包), 另一个ClassLoader加载会更改的类,称为restart ClassLoader,这样在有代码更改的时候, 原来的restart ClassLoader被丢弃,重新创建一个restart ClassLoader,由于需要加载的类相比较少,所以实现了较快的重启时间。