为什么在我的情况下@ConfigurationProperties不覆盖默认值? [英] Why is @ConfigurationProperties not overriding defaults in my case?
问题描述
我希望spring boot从我的application.properties中的条目读取我的配置,但是它们没有被激活.我的application.properties包含以下条目:
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
我可以看到该文件正在被读取(我只有两次文件:一次在src树中,一次自动在bin中复制.这些文件相等):
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'
我有一个名为FooConfig的类:
I have a class called FooConfig:
@Component
@ConfigurationProperties(prefix = "foo")
public class FooConfig {
private String user = "default";
private String pass = "default";
...
}
我有两个值的getter和setter方法. 我将这个配置类自动连接到我的代码中:
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());
...
问题是打印的是默认值"default",而不是来自application.properties的值. 有什么提示吗?不幸的是,我还不能使用执行器来显示配置属性,因为这是一个非Web应用程序. 我会在Spring Boot 1.2.2上尝试所有这些操作
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
推荐答案
为了制作
@ConfigurationProperties(prefix = "foo")
注释工作,您的spring boot应用程序必须具有
annotation work, your spring boot application must have
@EnableConfigurationProperties
注释集.
无需在 ConfigurationProperties bean上设置 @Component 注释.
There is no need to set @Component annotation on ConfigurationProperties bean.
这篇关于为什么在我的情况下@ConfigurationProperties不覆盖默认值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!