0
点赞
收藏
分享

微信扫一扫

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

飞鸟不急 2023-07-01 阅读 55

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



举报

相关推荐

0 条评论