Spring Boot中如何注入含内部Map的Map<String, PlatformAccount> Bean?
实现Spring Boot中嵌套Map的配置注入
步骤1:定义子账户POJO
先创建SubAccount类,用来存储子账户的名称和密钥:
public class SubAccount { private String name; private String secret; // 必须提供无参构造器 public SubAccount() {} // Getter和Setter方法 public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSecret() { return secret; } public void setSecret(String secret) { this.secret = secret; } }
步骤2:修改PlatformAccount类,添加子账户Map
在原有PlatformAccount中新增subaccounts字段,类型为Map<String, SubAccount>,并提供对应的Getter和Setter方法:
public class PlatformAccount { private String name; private String secret; // 修正原代码中字段与配置不匹配的问题(原写为email,配置用的是secret) private Map<String, SubAccount> subaccounts = new TreeMap<>(); // 初始化避免空指针 // 无参构造器 public PlatformAccount() {} // Getter和Setter方法 public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSecret() { return secret; } public void setSecret(String secret) { this.secret = secret; } public Map<String, SubAccount> getSubaccounts() { return subaccounts; } public void setSubaccounts(Map<String, SubAccount> subaccounts) { this.subaccounts = subaccounts; } }
步骤3:保持配置类不变
原有的配置类无需修改,Spring Boot的@ConfigurationProperties会自动识别嵌套的Map结构,将platform.accounts.xxx.subaccountN的配置映射到对应PlatformAccount的subaccounts集合中:
import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import java.util.TreeMap; @Configuration public class PlatformConfig { @ConfigurationProperties(prefix = "platform.accounts") @Bean public Map<String, PlatformAccount> platformAccounts() { return new TreeMap<>(); } }
步骤4:验证配置映射
你提供的application.properties配置可以直接使用,Spring会自动完成嵌套映射:
platform.accounts.acbot71.name=acbot71 platform.accounts.acbot71.secret=secret1 platform.accounts.acbot71.subaccount1.name=sub1 platform.accounts.acbot71.subaccount1.secret=secret1 platform.accounts.acbot71.subaccount2.name=sub2 platform.accounts.acbot71.subaccount2.secret=secret2 platform.accounts.acbot72.name=acbot72 platform.accounts.acbot72.secret=secret2 platform.accounts.acbot72.subaccount1.name=sub1 platform.accounts.acbot72.subaccount1.secret=secret1
测试注入效果
通过依赖注入Map<String, PlatformAccount>即可验证配置是否生效:
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @Component public class AccountTestComponent { private final Map<String, PlatformAccount> platformAccounts; @Autowired public AccountTestComponent(Map<String, PlatformAccount> platformAccounts) { this.platformAccounts = platformAccounts; } public void printAccountInfo() { platformAccounts.forEach((mainKey, mainAccount) -> { System.out.println("主账户:" + mainAccount.getName()); mainAccount.getSubaccounts().forEach((subKey, subAccount) -> { System.out.println(" 子账户:" + subAccount.getName() + ",密钥:" + subAccount.getSecret()); }); }); } }
内容的提问来源于stack exchange,提问作者it.drive.view
相关产品推荐
相关产品推荐

