首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

在spring-boot中读取properties.yml文件中的字符串数组数据

在Spring Boot中读取properties.yml文件中的字符串数组数据,可以通过使用@ConfigurationProperties注解和@Value注解来实现。

  1. 使用@ConfigurationProperties注解: @ConfigurationProperties注解可以将properties.yml文件中的属性值映射到一个Java对象中。首先,在Spring Boot的配置类中添加@ConfigurationProperties注解,并指定prefix属性为properties.yml文件中的前缀,例如:
代码语言:txt
复制
@ConfigurationProperties(prefix = "myapp")
public class MyAppProperties {
    private List<String> myArray;

    public List<String> getMyArray() {
        return myArray;
    }

    public void setMyArray(List<String> myArray) {
        this.myArray = myArray;
    }
}

然后,在需要使用该属性的地方注入该对象:

代码语言:txt
复制
@RestController
public class MyController {
    @Autowired
    private MyAppProperties myAppProperties;

    @GetMapping("/myarray")
    public List<String> getMyArray() {
        return myAppProperties.getMyArray();
    }
}

在properties.yml文件中,可以按照以下格式配置字符串数组:

代码语言:txt
复制
myapp.my-array:
  - value1
  - value2

推荐的腾讯云相关产品:腾讯云云服务器(CVM) 产品介绍链接地址:https://cloud.tencent.com/product/cvm

  1. 使用@Value注解: @Value注解可以直接将properties.yml文件中的属性值注入到一个变量中。在需要使用该属性的地方,使用@Value注解,并指定属性的key,例如:
代码语言:txt
复制
@RestController
public class MyController {
    @Value("${myapp.my-array}")
    private List<String> myArray;

    @GetMapping("/myarray")
    public List<String> getMyArray() {
        return myArray;
    }
}

在properties.yml文件中,可以按照以下格式配置字符串数组:

代码语言:txt
复制
myapp.my-array: value1,value2

推荐的腾讯云相关产品:腾讯云云服务器(CVM) 产品介绍链接地址:https://cloud.tencent.com/product/cvm

页面内容是否对你有帮助?
有帮助
没帮助

相关·内容

领券