Spring @Resource注入报空指针异常

430 阅读1分钟

今天在项目中使用@Resource进行依赖注入时给我报了一个空指针异常:

报错示例代码:

@Resource
private static DemoService demoService;

image-20220106001811496

解决办法:使用SpringContextHolder获取Bean

private static final DemoService DEMO_SERVICE = SpringContextHolder.getBean(DemoService.class);