主要封装一些获取 bean 的方法,代码如下:
@Component
public class SpringUtils implements ApplicationContextAware {
/**
* 上下文对象实例
*/
private static ApplicationContext context = null;
@Override
@Autowired
public void setApplicationContext(@NotNull ApplicationContext applicationContext) throws BeansException {
context = applicationContext;
log.info("Inject applicationContext succeeded");
}
public static ApplicationContext getApplicationContext() {
if (context == null) {
throw new IllegalArgumentException("ApplicationContext wasn't injected");
}
return context;
}
}
在其他 bean 的构造方法里面调用SpringUtils.getApplicationContext。
奇怪的是,在 idea 启动正常,打包部署的时候就会抛错IllegalArgumentException。
网上一推解法都试过了,都没用。 请求大佬支援。