Injection of autowired dependencies failed; nested exception is java.lang.Il

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
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

black job

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值