场景描述:
我的项目是 springboot 项目
在工具类里面使用@Autowired 注入service,dao,或者 restTemplate 都是 null,
同样,使用 @Value 引用配置文件中的变量也是 null

解决方法:
首先,要在工具类上加上注解 @Configuration
然后定义一个自身的 static 变量,
public static SpeechTranscriberWithMicrophoneDemo speech;
@PostConstruct
public void init() {
speech = this;
}
最后,使用的时候:
speech.summaryParagraphsMapper;
speech.writeFileUrl;
分析:
空闲时间补充
本文介绍在SpringBoot项目中,如何解决工具类使用@Autowired注解注入Service、DAO或restTemplate等组件时出现null的问题。通过在工具类上添加@Configuration注解,并使用@PostConstruct初始化静态变量,实现正确注入。
824

被折叠的 条评论
为什么被折叠?



