YML文件
nfs:
domain: xxx
filePath: xxx
oss:
endpoint: xxx
accessKeyId: xxx
accessKeySecret: xxx
bucketName: xxx
policyUrl: xxx
#sts授权模式
sts:
regionId: xxx
accessKeyId: xxx
accessKeySecret: xxx
roleArn: xxx
roleSessionName: xxx
读取方式1
@Value("${nfs.domain}")
private String domain;
@Value("${nfs.filePath}")
private String filePath;
读取方式2
新建NfsProperties
@Data
@Component
@ConfigurationProperties(prefix = "nfs")
public class NfsProperties {
private String domain;
private String filePath;
}
新建OssProperties
@Data
@Component
@ConfigurationProperties(prefix = "oss")
public class OssProperties {
private String endpoint;
private String accessKeyId;
private String accessKeySecret;
private String bucketName;
private String policyUrl;
private Map<String, String> sts = new HashMap<>();
}
pom.xml配置
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
使用
@Autowired
private NfsProperties nfsProperties;
@Autowired
private OssProperties ossProperties;
LOGGER.info("nfsProperties domain[{}]", nfsProperties.getDomain());
LOGGER.info("nfsProperties filePath[{}]", nfsProperties.getFilePath());
LOGGER.info("ossProperties endpoint[{}]", ossProperties.getEndpoint());
LOGGER.info("ossProperties accessKeyId[{}]", ossProperties.getAccessKeyId());
LOGGER.info("ossProperties accessKeySecret[{}]", ossProperties.getAccessKeySecret());
LOGGER.info("ossProperties bucketName[{}]", ossProperties.getBucketName());
LOGGER.info("ossProperties policyUrl[{}]", ossProperties.getPolicyUrl());
LOGGER.info("sts regionId[{}]", ossProperties.getSts().get("regionId"));
LOGGER.info("sts accessKeyId[{}]", ossProperties.getSts().get("accessKeyId"));
LOGGER.info("sts accessKeySecret[{}]", ossProperties.getSts().get("accessKeySecret"));
LOGGER.info("sts roleArn[{}]", ossProperties.getSts().get("roleArn"));
LOGGER.info("sts roleSessionName[{}]", ossProperties.getSts().get("roleSessionName"));
网友评论