Spring Boot: Retrieving configuration from a database

后端 未结 1 371
栀梦
栀梦 2020-12-18 09:56

Can anyone offer me some guidance on the best way for me to achieve this goal.

I\'d like to extend the Spring Boot Externalized Configuration so that I have a single

相关标签:
1条回答
  • 2020-12-18 10:35

    I have used the EnvironmentPostProcessor spring feature to do this.

    You need to create a class like this:

    public class ReadDbPropertiesPostProcessor implements EnvironmentPostProcessor {
        /**
         * Name of the custom property source added by this post processor class
         */
        private static final String PROPERTY_SOURCE_NAME = "databaseProperties";
        
        /**
         * Adds Spring Environment custom logic. This custom logic fetch properties from database and setting highest precedence
         */
        @Override
        public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
            Map<String, Object> propertySource = new HashMap<>();
    
            try {
                // Build manually datasource to ServiceConfig
                DataSource ds = DataSourceBuilder
                        .create()
                        .username(USERNAME) // replace with your config
                        .password(PASSWORD) // replace with your config
                        .url(DATASOURCE-URL)// replace with your config
                        .driverClassName(DRIVER) // replace with your config
                        .build();
    
                // Fetch all properties
                PreparedStatement preparedStatement = ds.getConnection().prepareStatement("SELECT name, value FROM propertyConfig WHERE service = ?");
                preparedStatement.setString(1, APP_NAME);
                
                ResultSet rs = preparedStatement.executeQuery();
    
                // Populate all properties into the property source
                while (rs.next()) {
                    String propName = rs.getString("name");
                    propertySource.put(propName, rs.getString("value"));
                }
                
                // Create a custom property source with the highest precedence and add it to Spring Environment 
                environment.getPropertySources().addFirst(new MapPropertySource(PROPERTY_SOURCE_NAME, propertySource));
            
            } catch (Exception e) {
                throw new RuntimeException("Error fetching properties from db");
            }
        }
    }
    

    Since you need to run this class at a very early stage of spring, you need to create the file spring.factories and register your environment post processor. This file needs to be located here:

    src/main/resources/META-INF/spring.factories
    

    In the content you need to set your class to the spring property:

    # Environment Post Processor
    org.springframework.boot.env.EnvironmentPostProcessor=com.your.package.ReadDbPropertiesPostProcessor
    
    0 讨论(0)
提交回复
热议问题