因为项目里面用到了动态获取Bean的方式,所以考虑实现AppplicationContextAware的方式来获取Spring上下文。
但是最近服务器上经常出现 SpringUitl.getBean空指针的问题.,而且不是经常出现 代码如下:
package com.jingchen.ccsp.util;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
@Component
public class SpringUtil implements ApplicationContextAware{
private static ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext arg0) throws BeansException {
if(applicationContext == null) applicationContext = arg0;
}
public static Object getBean(String name) {
return applicationContext.getBean(name);
}
}
看了下实现,并没问题,网上也查了下,说是用注解的方式注入SpringUtil,在以jar的方式启动SpringBoot项目那么SpringUtil类必须放到启动类同级包目录。
不然有时候会获