在 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.yml
或 application.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 配置文件中,并在应用程序中使用这些配置。