为什么@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-tree 中,一次在 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屋!