Springboot @PropertySource @ConfigurationProperties 注入同类型不同名称bean

同类型一个实例


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();
    }
}

你可能感兴趣的:(Springboot @PropertySource @ConfigurationProperties 注入同类型不同名称bean)