1

After switching to Spring Boot, I'm having an issue getting the list of properties available for @Value lookup.

I'm using multiple properties files (classpath:application.context, and /config/applicaton.context for overrrides). That means that reading the classpath:application.context file isn't an option because of the overrides.

classpath:application.properties:

foo.bar.someProp=apple
bar.baz.someProp=peaches
baz.foo.someProp=pumpkin

/config/application.properties:

bar.baz.someProp=cheese

Application.java:

public class Application {

    @Value("${foo.bar.someProp}")
    private String someProp;

    @Value("${bar.baz.someProp}")
    private String someProp2;

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

There's no way for me to know that the value of bar.baz.someProp is "cheese" or that baz.foo.someProp exists.

I've seen examples using @ConfigurationProperties but I'd have to know the properties in advance, which I don't.

Ideally I'd just like a map of the properties available to @Value lookups that I've added with the properties files.

I know the management console can show the values of individual properties files, but I can't (not allowed to) activate it and I can't figure out how it's getting the list of properties to mimic the behavior.

Thanks!

Update: 2015-09-08

Ideally there would be a method like this:

public Map<String,Object) getEffectiveProperties() {
    ....          
}

Map<String, Object> allProperties = getEffectiveProperties();

for (Map.Entry<String, Integer> entry : allProperties.entrySet()) {
    String key = entry.getKey().toString();
    Object value = entry.getValue();
    System.out.println("key: " + key + ", value: " + value.toString() );
}

The output would be:

key: foo.bar.someProp, val: apple 
key: bar.baz.someProp, val: cheese
key: baz.foo.someProp, val: pumpkin
Avi Bay
  • 33
  • 2
  • 5
  • What do you mean "a map of the properties available to @Value lookups " ? Do you want to know which properties are looked up in the application or something else. Also "There's no way for me to know that the value of bar.baz.someProp is "cheese" ", can't you get the value or what ? Can you add some details about what you really needed ? – Gokhan Oner Sep 05 '15 at 21:28
  • I want to know what the value of each property is that I've added. I updated the question with an example. – Avi Bay Sep 08 '15 at 18:04
  • Think that duplicates http://stackoverflow.com/questions/23506471/spring-access-all-environment-properties-as-a-map-or-properties-object – emamedov Sep 08 '15 at 18:09
  • Unfortunately, it doesn't. I did try this though. The answer to that question will directly read the values from a specific properties file without regards to other property files in the that may [override the value](http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-external-config-application-property-files) of the specific property you're reading. – Avi Bay Sep 08 '15 at 18:27
  • 1
    Probably this is what you need: https://stackoverflow.com/questions/23506471/spring-access-all-environment-properties-as-a-map-or-properties-object – user8681446 Sep 27 '17 at 08:00

0 Answers0