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

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对象的属性中。

posted @ 2023-06-28 16:16  大树2  阅读(891)  评论(0编辑  收藏  举报