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

前端 未结 4 1761
栀梦
栀梦 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;
    }
    
    0 讨论(0)
  • 2020-12-14 17:02

    24.8.1 Third-party Configuration

    As well as using @ConfigurationProperties to annotate a class, you can also use it on public @Bean methods. Doing so can be particularly useful when you want to bind properties to third-party components that are outside of your control.

    To configure a bean from the Environment properties, add @ConfigurationProperties to its bean registration, as shown in the following example:

    @ConfigurationProperties(prefix = "another")
    @Bean
    public AnotherComponent anotherComponent() {
        ...
    }
    

    Any property defined with the another prefix is mapped onto that AnotherComponent bean in manner similar to the preceding AcmeProperties example.

    0 讨论(0)
  • 2020-12-14 17:04
    spring.datasource.url = [url]
    spring.datasource.username = [username]
    spring.datasource.password = [password]
    spring.datasource.driverClassName = oracle.jdbc.OracleDriver
    
    @Bean
    @ConfigurationProperties(prefix="spring.datasource")
    public DataSource dataSource() {
        return new DataSource();
    }
    

    Here the DataSource class has proeprties url, username, password, driverClassName, so spring boot maps them to the created object.

    Example of the DataSource class:

    public class DataSource {
        private String url;
        private String driverClassName;
        private String username;
        private String password;
        //getters & setters, etc.
    }
    

    In other words this has the same effect as if you initialize some bean with stereotype annotations(@Component, @Service, etc.) e.g.

    @Component
    @ConfigurationProperties(prefix="spring.datasource")
    public class DataSource {
        private String url;
        private String driverClassName;
        private String username;
        private String password;
        //getters & setters, etc.
    }
    
    0 讨论(0)
  • 2020-12-14 17:07

    You can use @ConfigurationProperties as below

    Entity Model

    public class MY_ENTITY {
        private String prop1;
        private String prop2;
        // setter & getter & toString()
    }
    

    Bean Method

    @Configuration
    public class MyClass {
    
        @Bean
        @ConfigurationProperties(prefix = "my.entity")
        public MY_ENTITY getContract() {
            return new MY_ENTITY()
                    .setProp1("prop1111111")
                    .setProp2("prop2222222")
                    ;
        }
    
        @Bean(name = "contract2")
        @ConfigurationProperties(prefix = "my.entity2")
        public MY_ENTITY getContract2() {
            return new MY_ENTITY()
                    .setProp1("prop1111.2222")
                    .setProp2("prop2222.222")
                    ;
        }
    }
    

    application.properties

    my.entity.prop1=2120180023
    my.entity.prop2=CUSTOMER_NAME111
    
    my.entity2.prop1=9994494949
    my.entity2.prop2=CUSTOMER_NAME222
    

    SpringBootApplication

    @SpringBootApplication
    public class DemoApplication implements CommandLineRunner {
    
        @Autowired
        @Qualifier("contract2")
        private MY_ENTITY myEntity;
    
        public static void main(String[] args) throws Exception {
            SpringApplication.run(DemoApplication.class, args);
        }
    
        @Override
        public void run(String... args) throws Exception {
            System.out.println(myEntity);
        }
    }
    
    0 讨论(0)
提交回复
热议问题