0
votes

I want spring boot to read my config from entries in my application.properties but they are not activated. My application.properties contains these entries:

foo.user=nobody
foo.pass=notshared

I can see that this file is being read (I only have the file twice: once in the src-tree and once copied automatically in bin. The files are equal):

2015-03-23 21:55:18.199 DEBUG 25004 --- [main] o.s.b.c.c.ConfigFileApplicationListener  : Loaded config file 'classpath:/application.properties' 

I have a class called FooConfig:

@Component
@ConfigurationProperties(prefix = "foo")
public class FooConfig {
  private String user = "default";
  private String pass = "default";
...
}

I have getters and setters for both values. I AutoWire this config class into my code:

@Component
public class FooFunctions {
    @Autowired
    public FooFunctions(FooConfig fooConfig) {
        log.debug("user={}", fooConfig.getUser());
...

And the problem is that the default value "default" is printed and not the value from the application.properties. Any hints ? Unfortunately I can not use the actuator yet to display configuration properties because this is a non-web application. I try all of this on spring boot 1.2.2

1
Hi, don't you miss the @EnableConfigurationProperties annotation ? - Vyncent
Yes, I added the annotation and now it is working. But I notice a strange thing: the default constructor of FooConfig gets called twice and then the default value os changed to the value specified in the .properties file. Do you have an idea why this is done twice ? - Marged
I think it's because you declare FooConfig as both Component and ConfigurationProperties. Keeping only ConfigurationProperties should be sufficient. - Vyncent
Yes, that was it. Thanks. - Marged
can you create a "real answer", I am not able to accept your comments as a correct answer and I would like to give you the credit your answer deserves. - Marged

1 Answers

2
votes

In order to make

@ConfigurationProperties(prefix = "foo")

annotation work, your spring boot application must have

@EnableConfigurationProperties

annotation set.

There is no need to set @Component annotation on ConfigurationProperties bean.