Springboot@PropertySource@ConfigurationProperties同じタイプの異なる名前beanを注入
2683 ワード
同じタイプのインスタンス
同じタイプの複数のインスタンス
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
/**
* @author Klooop
* @date 2018/9/1
*/
//
@PropertySource(value = "classpath:config/account.properties")
//
@ConfigurationProperties(prefix = "account")
// Spring id
@Component
public class AccountConfig {
/**
* id : strategy1
* classType : com.xxx.impl
*/
private String id;
private String classType;
private TypeParamBean typeParam;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getClassType() {
return classType;
}
public void setClassType(String classType) {
this.classType = classType;
}
public TypeParamBean getTypeParam() {
return typeParam;
}
public void setTypeParam(TypeParamBean typeParam) {
this.typeParam = typeParam;
}
public static class TypeParamBean {
/**
* platform : abc
* secretKey : sdf
* accessKet : adsfk
*/
private String platform;
private String secretKey;
private String accessKet;
public String getPlatform() {
return platform;
}
public void setPlatform(String platform) {
this.platform = platform;
}
public String getSecretKey() {
return secretKey;
}
public void setSecretKey(String secretKey) {
this.secretKey = secretKey;
}
public String getAccessKet() {
return accessKet;
}
public void setAccessKet(String accessKet) {
this.accessKet = accessKet;
}
}
}
同じタイプの複数のインスタンス
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
/**
* @author Klooop
* @date 2018/9/1
*/
//
@Configuration
//
@PropertySource(value = "classpath:config/account.properties")
public class AccountConfigManager {
// id
@Bean
//
@ConfigurationProperties(prefix = "account.aa")
public AccountConfig aaAccount() {
return new AccountConfig();
}
@Bean
@ConfigurationProperties(prefix = "account.bb")
public AccountConfig bbAccount() {
return new AccountConfig();
}
}