因为工具类一般都是static的 所以普通的放入是有没有大用 会报null异常(被折磨了半天)
直接上解决方案
@Slf4j
@Component //一定要加这个注入
public class FileUtil {
/**工具类注入bean
* @author zhangjunrong
* @date 2022/2/22 19:30
*/
@Autowired
private WebAppDataProperties webAppDataProperties;
private static FileUtil fileUtil ;
@PostConstruct
public void init() {
fileUtil = this;
fileUtil.webAppDataProperties= this.webAppDataProperties;
}
}
使用方法:
fileUtil.webAppDataProperties.getMappingLocation()
本文介绍了如何在Spring Boot中避免因静态工具类注入Bean导致的null异常,通过@Component注解并使用@Autowired自动装配webAppDataProperties属性。提供了解决方案和使用方法实例。
1831

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



