Java @ConfigurationProperties前缀不工作
.yml文件Java @ConfigurationProperties前缀不工作,java,spring,spring-boot,Java,Spring,Spring Boot,.yml文件 cassandra: keyspaceApp:junit solr: keyspaceApp:xyz 豆子 主方法文件 @EnableAutoConfiguration @ComponentScan @PropertySource("application.yml") public class CommonDataApplication { public static void main(String[] args) { Configura
cassandra:
keyspaceApp:junit
solr:
keyspaceApp:xyz
豆子
主方法文件
@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);
}
}
测试用例
@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 ****");
}
}
它没有将junit设置为keyspaceApp,而是设置xyz
看起来prefix=“cassandra”不工作我不知道“xyz”是从哪里来的(可能您没有显示整个应用程序.yml?)。不过,您通常不会在
@ConfigurationProperties
中绑定@Value
(它无法知道您的前缀是什么)。您是否真的在任何地方@EnableCongigurationProperties
?您是否正在使用SpringApplication
创建应用程序上下文?看起来您正在尝试使用springboot功能
因此,为了使其正常工作,您必须对代码进行一些更改:
首先,您的CommonDataApplication
类应该具有@EnableConfigurationProperties
注释,例如
@EnableAutoConfiguration
@ComponentScan
@PropertySource("application.yml")
@EnableConfigurationProperties
public class CommonDataApplication {
public static void main(String[] args) {
// ...
}
}
我认为您不需要@PropertySource(“application.yml”)
注释,因为application.yml
(以及application.properties
和application.xml
)是Spring Boot使用的默认配置文件
您的CassandraClientNew
类不需要有@Value
注释前缀keyspaceApp
属性。您的keyspaceApp
必须具有setter方法
@Component
@ConfigurationProperties(prefix="cassandra")
public class CassandraClientNew {
private String keyspaceApp;
public void setKeyspaceApp(final String keyspaceApp) {
this.keyspaceApp = keyspaceApp;
}
}
顺便说一句,如果您正在使用List
或Set
s并初始化集合(例如List values=new ArrayList();
),则只需要getter。如果集合未初始化,则还需要提供setter方法(否则将引发异常)
我希望这会有帮助。
这只适用于我的SB1.5.4版本。就这么简单。更多信息请参见上面的帖子
@配置
@ConfigurationProperties(prefix=“mail”)
公共类配置属性
一般答案
1.在属性文件(application.properties或application.yml)中
2.申报你的班级:
@组件
@ConfigurationProperties(prefix=“a”,ignoreUnknownFiels=false)
公共类MyClassA{
public MyClassB theB;//这个名字实际上没有任何意义
//它可以是任何东西
公共无效设置B(MyClassB theB){
this.theB=theB;
}
公共静态MyClassB{
公共字符串;
公共无效集合(字符串集合){
this.theC=theC;
}
}
}
3.公开声明!这是至关重要的!
确保在上述类中声明了这些公共方法。确保它们有“public”修饰符。
就这样。
最后说明
- 类中的属性名称对Spring没有任何意义。它只使用公共setter。我宣布他们是公开的,而不是在这里宣布公众人物。您的属性可能有任何访问修饰符
- 请注意属性“ignoreUnknownFields”。其默认值为“true”。当它为“false”时,如果文件“application.yml”中的任何属性未绑定到任何类属性,它将引发异常。它将在调试期间对您有很大帮助
@Component
@ConfigurationProperties(prefix="cassandra")
public class CassandraClientNew {
private String keyspaceApp;
public void setKeyspaceApp(final String keyspaceApp) {
this.keyspaceApp = keyspaceApp;
}
}
# In application.yaml
a:
b:
c: some_string
// In MyClassA
public void setTheB(MyClassB theB) {
this.theB = theB;
}
// In MyClassB
public void setTheC(String theC) {
this.theC = theC;
}