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

spring cloudalibaba 整合nacos动态配置的时候项目启动报错:

package pers.miracle.miraclecloud.system.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
 * @author lgn
 * @version 1.0
 * @date 2021/10/25 10:51
 */
@RestController
@RequestMapping("/nacos/nacosHelper")
@RefreshScope
public class NacosController {

    @Value("${common.name}")
    private String name;

    @Value("${common.info}")
    private String info;

    @RequestMapping("/getNacosInfo")
    public String get() {
        return "娱乐圈重磅曝光:"+name+info;
    }
}

Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'scopedTarget.nacosController': Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'name' in value "${name}"

检查一下配置文件:
bootstrap.yml:
Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException:
application.yml:
Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException:
打开nacos配置页面查看:
Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException:

查看一下 Data ID: 配置格式 配置内容书写格式是否正确:
Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException:
Data ID: 配置格式 是否跟 bootstrap.yml,application.yml:里面一致。

我的application.yml中的项目名是:miracle-system
命名空间是:gene
文件后缀是:yml
所以我的Data ID如下:

miracle-system-gene.yml

配置内容:
1.如果使用yaml格式的配置文件, 一定要注意Nocos中配置的书写格式, 注意空格, 如果不输入空格,
使用@Value(“common.name”)会找不到对应的配置参数, 启动服务的时候会报错
在尝试中意外发现可以使用@Value(“common:name”)来获取配置参数, 获取到的是name:zhans 这段字符串.

错误例子:

common:
   name:吴亦凡

正确例子:

common:
   name: 吴亦凡
   info: 大碗宽面!
上一篇:Linux 中换行符问题


下一篇:分布式事务系列---【配置 Seata-Client】