springboot 将对象数组转成yaml配置文件中的配置

在 Spring Boot 中,可以使用 @ConfigurationProperties 注解和 YAML 或 properties 配置文件来配置复杂对象,如您的 CdnAccount 对象数组。以下是一个示例,展示如何将这些对象数组的数据放入配置文件中,并将其映射到 Java 对象中。

1. 定义 CdnAccount

首先,定义 CdnAccount 类:

public class CdnAccount {
    private String poolName;
    private String secretId;
    private String secretKey;

    // Getters and Setters
    public String getPoolName() {
        return poolName;
    }

    public void setPoolName(String poolName) {
        this.poolName = poolName;
    }

    public String getSecretId() {
        return secretId;
    }

    public void setSecretId(String secretId) {
        this.secretId = secretId;
    }

    public String getSecretKey() {
        return secretKey;
    }

    public void setSecretKey(String secretKey) {
        this.secretKey = secretKey;
    }
}

2. 定义包含 CdnAccount 数组的配置类

创建一个配置类,用于读取配置文件中的 CdnAccount 对象数组:

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

import java.util.List;

@Configuration
@ConfigurationProperties(prefix = "cdn")
public class CdnAccountProperties {
    private List<CdnAccount> accounts;

    public List<CdnAccount> getAccounts() {
        return accounts;
    }

    public void setAccounts(List<CdnAccount> accounts) {
        this.accounts = accounts;
    }
}

3. 在配置文件中定义对象数组

application.ymlapplication.properties 文件中定义 CdnAccount 对象数组的数据。使用 YAML 格式:

application.yml
cdn:
  accounts:
    - poolName: pool1
      secretId: secretId1
      secretKey: secretKey1
    - poolName: pool2
      secretId: secretId2
      secretKey: secretKey2
    - poolName: pool3
      secretId: secretId3
      secretKey: secretKey3
application.properties
cdn.accounts[0].poolName=pool1
cdn.accounts[0].secretId=secretId1
cdn.accounts[0].secretKey=secretKey1
cdn.accounts[1].poolName=pool2
cdn.accounts[1].secretId=secretId2
cdn.accounts[1].secretKey=secretKey2
cdn.accounts[2].poolName=pool3
cdn.accounts[2].secretId=secretId3
cdn.accounts[2].secretKey=secretKey3

4. 使用配置类

在需要使用 CdnAccount 对象数组的地方,自动注入 CdnAccountProperties 配置类:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CdnAccountService {

    private final CdnAccountProperties cdnAccountProperties;

    @Autowired
    public CdnAccountService(CdnAccountProperties cdnAccountProperties) {
        this.cdnAccountProperties = cdnAccountProperties;
    }

    public void printCdnAccounts() {
        cdnAccountProperties.getAccounts().forEach(account -> {
            System.out.println("Pool Name: " + account.getPoolName());
            System.out.println("Secret ID: " + account.getSecretId());
            System.out.println("Secret Key: " + account.getSecretKey());
        });
    }
}

5. 示例应用程序

将服务类中的方法调用添加到应用程序启动类中,以查看效果:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication implements CommandLineRunner {

    private final CdnAccountService cdnAccountService;

    @Autowired
    public DemoApplication(CdnAccountService cdnAccountService) {
        this.cdnAccountService = cdnAccountService;
    }

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

    @Override
    public void run(String... args) throws Exception {
        cdnAccountService.printCdnAccounts();
    }
}

这样,当您运行 Spring Boot 应用程序时,它会读取配置文件中的 CdnAccount 对象数组,并打印每个对象的详细信息。这个示例展示了如何将对象数组的数据放入 Spring Boot 配置文件中,并在应用程序中使用这些配置。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值