问题
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:
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.
来源:https://stackoverflow.com/questions/29220498/why-is-configurationproperties-not-overriding-defaults-in-my-case