Using `@ConfigurationProperties` annotation on `@Bean` Method

前端 未结 4 1765
栀梦
栀梦 2020-12-14 16:38

Could someone give a MWE of how to use the @ConfigurationProperties annotation directly on a @Bean method?

I have seen countless examples o

4条回答
  •  轮回少年
    2020-12-14 16:44

    I found following solution: i.e. we have in application yaml couple of section and we are interesting in appConfig:

      appConfig:
      version: 1.0_alpha
      environment: ${spring.profiles}
      dbDriver: ${spring.datasource.driver-class-name}
      dbUrl: ${spring.datasource.url}
      keyCloak:
          serverOne:
              host: http://xx.xx.xxx.xxx:8080
              baseUrl: ${appConfig.keyCloak.serverOne.host}/auth/realms/master
              clientId: api-service-agent
              clientSecret: f00955443-d123-4cfe-90d3-e3ff3b214aaffe
              serviceUsername: service-user
              servicePassword: 1234567890
          serverTwo:
              host: http://xx.xxx.xxx.xxx:8080
              baseUrl: ${appConfig.keyCloak.serverTwo.host}/auth/realms/wissance
              clientId: api-service-agent
              clientSecret: a20ddf0-56fa-4991-85bc-114377eeffddcc
              serviceUsername: service-user
              servicePassword: 1234567890
          using: 
              baseUrl: ${appConfig.keyCloak.serverTwo.baseUrl}
              clientId: ${appConfig.keyCloak.serverTwo.clientId}
              clientSecret: ${appConfig.keyCloak.serverTwo.clientSecret}
              serviceUsername: ${appConfig.keyCloak.serverTwo.serviceUsername}
              servicePassword: ${appConfig.keyCloak.serverTwo.servicePassword}
    

    We would like to split common settings and using KeyCloak settings, so i implemented following scheme:

    I make following KeyCloakConfig class (without @ConfigurationProperties annotation) to store using authentication server settings:

    @Configuration
    public class KeyCloakConfig {
    
        public KeyCloakConfig(){
    
        }
    
        public KeyCloakConfig(String baseUrl, String clientId, String clientSecret, String username, String password) {
            this.baseUrl = baseUrl;
            this.clientId = clientId;
            this.clientSecret = clientSecret;
            this.username = username;
            this.password = password;
        }
    
        public String getBaseUrl(){
            return baseUrl;
        }
    
        public void setBaseUrl(String baseUrl){
            this.baseUrl = baseUrl;
        }
    
        public String getClientId(){
            return clientId;
        }
    
        public void setClientId(String clientId){
            this.clientId = clientId;
        }
    
        public String getClientSecret(){
            return clientSecret;
        }
    
        public void setClientSecret(String clientSecret){
            this.clientSecret = clientSecret;
        }
    
        public String getUsername(){
            return username;
        }
    
        public void setUsername(String username){
            this.username = username;
        }
    
        public String getPassword(){
            return password;
        }
    
        public void setPassword(String password){
            this.password = password;
        }
    
        @Value("${appConfig.keyCloak.using.baseUrl}")
        private String baseUrl;
    
        @Value("${appConfig.keyCloak.using.clientId}")
        private String clientId;
    
        @Value("${appConfig.keyCloak.using.clientSecret}")
        private String clientSecret;
    
        @Value("${appConfig.keyCloak.using.serviceUsername}")
        private String username;
    
        @Value("${appConfig.keyCloak.using.servicePassword}")
        private String password;
    }
    

    and AppConfig class that holds common settings like version, environment using DB driver & url and also KeyCloakConfig as property:

    @Configuration
    @EnableConfigurationProperties
    @ConfigurationProperties
    public class AppConfig {
    
    public AppConfig(){
    
        }
    
        public AppConfig(String apiVersion, String environment, String databaseDriver, String databaseUrl){
            this.apiVersion = apiVersion;
            this.environment = environment;
            this.databaseDriver = databaseDriver;
            this.databaseUrl = databaseUrl;
        }
    
        public String getEnvironment(){
            return environment;
        }
    
        public void setEnvironment(String environment) {
            this.environment = environment;
        }
    
        public String getDatabaseDriver(){
            return databaseDriver;
        }
    
        public void setDatabaseDriver(String databaseDriver) {
            this.databaseDriver = databaseDriver;
        }
    
        public String getDatabaseUrl(){
            return databaseUrl;
        }
    
        public void setDatabaseUrl(String databaseUrl) {
            this.databaseUrl = databaseUrl;
        }
    
        public String getApiVersion(){
            return apiVersion;
        }
    
        public void setApiVersion(String apiVersion) {
            this.apiVersion = apiVersion;
        }
    
        public KeyCloakConfig getKeyCloakConfig(){
            return keyCloakConfig;
        }
    
        public void setKeyCloakConfig(KeyCloakConfig keyCloakConfig){
            this.keyCloakConfig = keyCloakConfig;
        }
    
        @Value("${appConfig.version}")
        private String apiVersion;
    
        @Value("${appConfig.environment}")
        private String environment;
    
        @Value("${appConfig.dbDriver}")
        private String databaseDriver;
    
        @Value("${appConfig.dbUrl}")
        private String databaseUrl;
    
        @Autowired
        private KeyCloakConfig keyCloakConfig;
    }
    

提交回复
热议问题