美文网首页
SpringBoot 读取 YML 配置

SpringBoot 读取 YML 配置

作者: 朱穆朗玛 | 来源:发表于2021-10-14 10:03 被阅读0次

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"));

相关文章

网友评论

      本文标题:SpringBoot 读取 YML 配置

      本文链接:https://www.haomeiwen.com/subject/gpvyoltx.html