Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read all properties values from the source properties file in Spring-cloud-config client

i have this spring-cloud-config client class and i can access the individual properties using the @Value annotation just fine. However, i am interested to know how to read ALL the properties values from a properties file without binding each of the property's key to a @Value annotation. Basically the idea is that i would like to read all the properties value from the properties file without even knowing anything about the properties defined in the file. Any idea how i can do that?

Client Class

@EnableAutoConfiguration                                                                       
@ComponentScan                                       
@RestController             
@RefreshScope                                           
public class ConfigDemoClientApplication  
{             
    @Value("${special}")            
    String special;

    @RequestMapping("/restaurant")
    public String hello()
    {
        return "Hello " + special;
    }

    public static void main(String[] args) {
        SpringApplication.run(ConfigDemoClientApplication.class, args);
    }
}

Sample Properties file

special: bargain!                                                                    
amount: 200                                                                           
city: New York

In this example, i would like to read all the 3 properties without defining a @Value annotation for each of them in my class. Is that possible?

Thanks for your help.

like image 450
Jake Avatar asked Jun 26 '16 04:06

Jake


1 Answers

I just solved you problem creating this applicationProps bean, that is a java.util.Properties object containing all the properties of the application.

The only think needed is an autowired Environment object.

Here's the code:

    @Autowired
    Environment env;

    //Load all the properties of the server and put them into a java Properties obj
    @Bean(name = "applicationProps")
    public Properties applicationProperties() {
        final Properties properties = new Properties();
        for(Iterator it = ((AbstractEnvironment) env).getPropertySources().iterator(); it.hasNext(); ) {
            PropertySource propertySource = (PropertySource) it.next();
            if (propertySource instanceof PropertiesPropertySource) {
                log.info("Adding all properties contained in " + propertySource.getName());
                properties.putAll(((MapPropertySource) propertySource).getSource());
            }
            if (propertySource instanceof  CompositePropertySource){
                properties.putAll(getPropertiesInCompositePropertySource((CompositePropertySource) propertySource));
            }
        }
        return properties;
    }

    private Properties getPropertiesInCompositePropertySource(CompositePropertySource compositePropertySource){
        final Properties properties = new Properties();
        compositePropertySource.getPropertySources().forEach(propertySource -> {
            if (propertySource instanceof MapPropertySource) {
                log.info("Adding all properties contained in " + propertySource.getName());
                properties.putAll(((MapPropertySource) propertySource).getSource());
            }
            if (propertySource instanceof CompositePropertySource)
                properties.putAll(getPropertiesInCompositePropertySource((CompositePropertySource) propertySource));
        });
        return properties;
    }

    @Autowired
    @Qualifier("applicationProps")
    Properties applicationProperties;

The recursive step in getPropertiesInCompositePropertySource method is needed because the properties fetched from the config server are recursively nested in a CompositePropertySource

Hope it helps

Greetings

like image 138
FrancescoM Avatar answered Oct 22 '22 11:10

FrancescoM