SpringBoot 中配置文件的信息怎么读取?

SpringBoot 定义了一个 properties 配置文件后,然后再定义了一个对应的实体类。

用 @Component 注解标注后,使用 @Value 映射到配置文件的 key 上时,取到的值 properties 的 名称,而不是具体的值,怎么回事儿呢?

如果你想直接properties里值直接匹配到实体加上
@ConfigurationProperties
@Component

province:
  name: 浙江省
  citys:
    - name: 杭州市
      remark: 省会
    - name: 温州市
      remark: 皮革厂倒闭了
@ConfigurationProperties(prefix = "province")
@Component
@Data
public class Province {

    private String name;
    private List<City> citys;

    @Data
    public static class City{
        private String name;
        private String remark;
    }
}
Properties properties = new Properties();
// 使用InPutStream流读取properties文件
BufferedReader bufferedReader = new BufferedReader(new FileReader("E:/config.properties"));
properties.load(bufferedReader);
// 获取key对应的value值
properties.getProperty(String key);

查看注解import是否正确,properties文件中格式是否正确,建议贴两张图我可以帮你看一下

@Value("${key}") 是取值的,直接用 @Value("key") 就是 key 字符串。