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
Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐