SpringBoot使用yaml封装全部数据到Environment对象时,在控制类中使用@Autowired,出现Could not autowire. No beans of 'Environment' type found.

SpringBoot使用Environment对象,读取yaml中的全部数据时,出现Could not autowire. No beans of 'Environment' type found.错误。

// 使用自动装配将所有的数据封装到一个对象Environment中
@Autowired
private Environment env;

原因是因为Environment对象的包导错了。

本来应该是导这个包:

import org.springframework.core.env.Environment;
import org.springframework.beans.factory.annotation.Autowired;

结果我导成下面这个包了:

import org.omg.CORBA.Environment;
import org.springframework.beans.factory.annotation.Autowired;

所以导致Environment对象自动注入不成功。

posted @ 2022-04-09 09:40  放逐梦想  阅读(312)  评论(0)    收藏  举报