Java @ConfigurationProperties 前缀不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27416327/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
@ConfigurationProperties prefix not working
提问by plzdontkillme
.yml file
.yml 文件
cassandra:
keyspaceApp:junit
solr:
keyspaceApp:xyz
Bean
豆角,扁豆
@Component
@ConfigurationProperties(prefix="cassandra")
public class CassandraClientNew {
@Value("${keyspaceApp:@null}") private String keyspaceApp;
Main method file
主要方法文件
@EnableAutoConfiguration
@ComponentScan
@PropertySource("application.yml")
public class CommonDataApplication {
public static void main(String[] args) {
ConfigurableApplicationContext context = new SpringApplicationBuilder(CommonDataApplication.class)
.web(false).headless(true).main(CommonDataApplication.class).run(args);
}
}
TestCase
测试用例
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = CommonDataApplication.class)
@IntegrationTest
@EnableConfigurationProperties
public class CassandraClientTest {
@Autowired
CassandraClientNew cassandraClientNew;
@Test
public void test(){
cassandraClientNew.getSession();
System.out.println(" **** done ****");
}
}
Instead of setting junit as the keyspaceApp it sets xyz.
它没有将 junit 设置为 keyspaceApp,而是设置了 xyz。
Looks like prefix="cassandra" not working
看起来 prefix="cassandra" 不起作用
采纳答案by Tom
It looks like you are trying to use Spring Boot Typesafe Configuration Propertiesfeature.
看起来您正在尝试使用 Spring Boot 类型安全配置属性功能。
So in order to have it working correctly, you have to add a few changes to your code:
因此,为了使其正常工作,您必须对代码进行一些更改:
First of all, your CommonDataApplication
class should have @EnableConfigurationProperties
annotation e.g.
首先,你的CommonDataApplication
班级应该有@EnableConfigurationProperties
注释,例如
@EnableAutoConfiguration
@ComponentScan
@PropertySource("application.yml")
@EnableConfigurationProperties
public class CommonDataApplication {
public static void main(String[] args) {
// ...
}
}
I do not believe you need @PropertySource("application.yml")
annotation as application.yml
(as well as application.properties
and application.xml
) is a default configuration file used by Spring Boot.
我认为您不需要@PropertySource("application.yml")
注释,因为application.yml
(以及application.properties
和application.xml
)是 Spring Boot 使用的默认配置文件。
Your CassandraClientNew
class does not need to have @Value
annotation prefixing keyspaceApp
property. And your keyspaceApp
has to have a setter method.
您的CassandraClientNew
类不需要具有@Value
注释前缀keyspaceApp
属性。而且你keyspaceApp
必须有一个 setter 方法。
@Component
@ConfigurationProperties(prefix="cassandra")
public class CassandraClientNew {
private String keyspaceApp;
public void setKeyspaceApp(final String keyspaceApp) {
this.keyspaceApp = keyspaceApp;
}
}
BTW, if you are using List
's or Set
s and you initialise collections (e.g. List<String> values = new ArrayList<>();
), then only getter is required. If a collection is not initialised then you need to provide a setter method too (otherwise an exception will be thrown).
顺便说一句,如果您使用List
's 或Set
s 并且您初始化集合(例如List<String> values = new ArrayList<>();
),则只需要 getter。如果一个集合没有初始化,那么你也需要提供一个 setter 方法(否则会抛出异常)。
I hope that will help.
我希望这会有所帮助。
回答by Dave Syer
I don't know where the "xyz" came from (maybe you aren't showing your whole application.yml?). You don't normally bind with @Value
in @ConfigurationProperties
though (it has no way of knowing what your prefix is). Did you actually @EnableCongigurationProperties
anywhere? Are you using SpringApplication
to create the application context?
我不知道“xyz”来自哪里(也许你没有展示你的整个 application.yml?)。您通常不会绑定@Value
in @ConfigurationProperties
(它无法知道您的前缀是什么)。你真的去@EnableCongigurationProperties
哪儿了吗?您是否正在使用SpringApplication
创建应用程序上下文?
回答by Breton F.
http://www.baeldung.com/configuration-properties-in-spring-boot
http://www.baeldung.com/configuration-properties-in-spring-boot
This only works for me with SB 1.5.4-RELEASE. Simple with just this. See above post for more info.
这仅适用于 SB 1.5.4-RELEASE。就这么简单。有关更多信息,请参阅上面的帖子。
@Configuration
@ConfigurationProperties(prefix = "mail")
public class ConfigProperties
@Configuration
@ConfigurationProperties(prefix = "mail")
public class ConfigProperties
回答by armansimonyan13
General answer
一般回答
1. In your properties file (application.properties or application.yml)
1. 在您的属性文件中(application.properties 或 application.yml)
# In application.yaml
a:
b:
c: some_string
2. Declare your class:
2. 声明你的类:
@Component
@ConfigurationProperties(prefix = "a", ignoreUnknownFiels = false)
public class MyClassA {
public MyClassB theB; // This name actually does not mean anything
// It can be anything
public void setTheB(MyClassB theB) {
this.theB = theB;
}
public static MyClassB {
public String theC;
public void setTheC(String theC) {
this.theC = theC;
}
}
}
3. Declare public setters! And this is crucial!
3. 声明公共二传手!而这一点至关重要!
Make sure to have these public methods declared in the above classes. Make sure they have "public"modifier.
确保在上述类中声明了这些公共方法。确保他们有“public”修饰符。
// In MyClassA
public void setTheB(MyClassB theB) {
this.theB = theB;
}
// In MyClassB
public void setTheC(String theC) {
this.theC = theC;
}
That's it.
就是这样。
Final notes
最后的笔记
- The property names in your classes do not mean anything to Spring. It only uses public setters. I declared them public not to declare public getters here. Your properties may have any access modifiers.
- Pay attention to the attribute "ignoreUnknownFields". Its default value is "true". When it is "false" it will throw exception if any of your properties in file "application.yml" was not bound to any class property. It will help you a lot during debugging.
- 类中的属性名称对 Spring 没有任何意义。它只使用公共二传手。我宣布他们是公开的,而不是在这里宣布公开的吸气剂。您的属性可能有任何访问修饰符。
- 注意属性“ignoreUnknownFields”。它的默认值为“true”。当它为“false”时,如果文件“application.yml”中的任何属性未绑定到任何类属性,它将抛出异常。它将在调试过程中为您提供很多帮助。