Java 读取配置文件application.yml的对象及数组数据

发布时间 2023-06-28 16:26:41作者: 大树2

Java 读取配置文件的对象及数组数据

application.yml 文件里的配置数据读取:
1.对象/map集合
aliyun:
oss:
endpoint : https://oss-cn-hangzhou.aliyuncs.com
accessKeyId : LTAI4GCH1vX8DKqJ8xd6n***
accessKeySecret : yBsh8weHOpq7uhCArrVHwIiB***
bucketName: product-image

2.数组/list/set集合
hobby:
list:
- java
- c#
- python
- go

-------------01------------------------------------
@Data
@Component
@ConfigurationProperties(prefix = "aliyun.oss")
public class AliOSSProperties {

//@Value("${aliyun.oss.endpoint}")
private String endpoint;
private String accessKeyId;
private String accessKeySecret;
private String bucketName;

}

@Autowired
private AliOSSProperties aliyunossProperties;

String endpoint = aliyunossProperties.getEndpoint();

----------------------02-------------------------
@Component
@Data
@ConfigurationProperties(prefix = "hobby")
public class HobbyProperties {

private  String[] list;

}

@Autowired
private HobbyProperties hobbyProperties;

String[] hobys=hobbyProperties.getList();


@Value注解只能一个一个的进行外部属性的注入。
@ConfigurationProperties可以批量的将外部的属性配置注入到bean对象的属性中。