Injection of autowired dependencies failed; nested exception is java.lang.Il
今天在学习nacos统一配置管理时,使用了@value注解,用来注入nacos中的配置属性,发现读取不到,代码如下:
@RestController
@RequestMapping("/user")
public class UserController {
// 注入nacos中的配置属性
@Value("${pattern.dateformat}"
private String dateformat;
// 编写controller,通过日期格式化器来格式化现在时间并返回
@GetMapping("now")
public String now(){
return LocalDate.now().format(
DateTimeFormatter.ofPattern(dateformat, Locale.CHINA)
); }
// ... 略
}
启动服务时发现报以下错误:
Injection of autowired dependencies failed; nested exception is java.lang.Il
经过多方面检查,发现是环境问题,我bootstrap.yml中写给我的统一配置管理配置的是dev开发环境,配置如下:
spring:
application:
name: userservice
profiles:
active: dev # 环境
cloud:
nacos:
server-addr: localhost:8848 # nacos服务地址
config:
file-extension: yaml # 后缀名
而我的服务没有配置为开发环境,从而导致,注入不进属性,从而报错,给bootstrap.yml最后面加入开发环境名称即可。代码如下:
spring:
application:
name: userservice
profiles:
active: dev # 环境
cloud:
nacos:
server-addr: localhost:8848 # nacos服务地址
config:
file-extension: yaml # 后缀名
namespace: f9a4df31-fd8a-41f5-9aec-007487f4f6b1 # dev环境 注:开发环境的唯一UUid
解决Spring Boot启动失败:Autowired依赖注入失败
在学习Nacos统一配置管理时遇到问题,尝试使用@Value注解注入配置属性,但启动服务时出现'Injection of autowired dependencies failed'错误。经过检查发现,服务的环境配置与bootstrap.yml中的dev环境不匹配,导致属性无法注入。解决方案是确保服务配置文件中指定正确的环境,例如在bootstrap.yml中添加对应的active环境配置,如'profiles.active: dev',以匹配Nacos的配置。
1万+

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



