java Spring Boot 属性 Yml/具有列表结构的属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/49248638/
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
Spring Boot Property Yml/Properties with List structure
提问by GSUgambit
I have looked all over stackoverflow and the net for a solution for this. No solution I have seen works because maybe none of the posts exactly fit my use case which contain a lists inside the file and also an object struture.
我已经在 stackoverflow 和网络上寻找了解决方案。我没有看到任何解决方案,因为可能没有一个帖子完全适合我的用例,其中包含文件内的列表和对象结构。
Here is a sample as a yaml
这是一个 yaml 示例
teddy.list:
-
name: Red
price: Five
-
name: Blue
price: One
-
name: Yellow
price: Two
-
name: Green
price: Three
Here is the same sample as a property file
这是与属性文件相同的示例
teddy.list[0].name=Red
teddy.list[0].price=Five
teddy.list[1].name=Blue
teddy.list[1].price=One
teddy.list[2].name=Yellow
teddy.list[2].price=Two
teddy.list[3].name=Green
teddy.list[3].price=Three
I want to be able to supply a teddy.yml or teddy.properties file to my application for configuration.
我希望能够向我的应用程序提供 teddy.yml 或 teddy.properties 文件以进行配置。
Here is my class for this:
这是我的课程:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
@Configuration
@PropertySource(name = "props", value = "classpath:teddy.yml", ignoreResourceNotFound = false)
@ConfigurationProperties(prefix = "teddy")
public class TeddyBearConfig {
@Autowired
Environment env;
@Value("${teddy.list}")
private TeddyBear[] teddyBears;
public TeddyBear[] getTeddyBears() {
return teddyBears;
}
public void setTeddyBears(TeddyBear[] teddyBears) {
this.teddyBears = teddyBears;
}
public static class TeddyBear {
private String name;
private String price;
public TeddyBear() {
}
public TeddyBear(String name, String price) {
this.name = name;
this.price = price;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
}
}
I've tried this setup, using the environment to try and access the properties, removing the prefix, declaring a bean of "PropertySourcesPlaceholderConfigurer".
我已经尝试过这个设置,使用环境来尝试访问属性,删除前缀,声明一个“PropertySourcesPlaceholderConfigurer”的bean。
With the current code, spring throws a IllegalStateException because it cannot convert java.lang.string to my TeddyBear class.
使用当前代码,spring 会抛出 IllegalStateException,因为它无法将 java.lang.string 转换为我的 TeddyBear 类。
采纳答案by pvpkiran
This should work.
这应该有效。
@Configuration
@PropertySource(name = "props", value = "classpath:teddy.properties", ignoreResourceNotFound = false)
@ConfigurationProperties(prefix = "teddy")
public class TeddyBearConfig {
private List<TeddyBear> list;
public List<TeddyBear> getList() {
return list;
}
public void setList(List<TeddyBear> list) {
this.list = list;
}
public static class TeddyBear {
private String name;
private String price;
public TeddyBear() {
}
public TeddyBear(String name, String price) {
this.name = name;
this.price = price;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
}
}
Update:
更新:
Above code works for the properties file you have given above.
If you wish to use yml file, you can do so. but there are a few points.
1. You yml structure isn't correct, it should be like this
上面的代码适用于您上面给出的属性文件。
如果你想使用 yml 文件,你可以这样做。但有几点。
1.你的yml结构不对,应该是这样的
teddy:
list:
-
name: Red
price: Five
-
name: Blue
price: One
-
name: Yellow
price: Two
-
name: Green
price: Three
2. After fixing your yml structure, (and also file name in your TeddyBearConfig), you will see that springboot doesn't complaint during startup, but list variable in TeddBearConfig will be null. This is a bug in the way springboot handles yml files through @PropertySource
.
2. 修复yml结构后(以及TeddyBearConfig中的文件名),您会看到springboot在启动时没有抱怨,但TeddBearConfig中的list变量将为空。这是 springboot 通过@PropertySource
.
3.If you move this yml content to application.yml
and remove @PropertySource
line in your config file, you would see that everything works perfectly fine.
3.如果您将此 yml 内容移动到application.yml
并删除@PropertySource
配置文件中的行,您会看到一切正常。
回答by Roland Roos
Watch out. Depending on Spring version above code may not work!
小心。根据 Spring 版本,上面的代码可能不起作用!
This may!
这可能!
private List<TeddyBear> list = new ArrayList<>;
public List<TeddyBear> getList() {
return list;
}
The trick is that the Spring factoring calls getList() and adds TeddyBears to the new ArrayList. On a null pointer there it nothing to add. Ditch the getter. Not used.
诀窍是 Spring 因子分解调用 getList() 并将 TeddyBears 添加到新的 ArrayList。在一个空指针上,它没有什么可添加的。抛弃吸气剂。不曾用过。
You only need further :
你只需要进一步:
@Autowired
TeddyBearConfig teddyBearConfig;
Last remark: If you want it to test under SpringBootTest, you may need some more tips.
最后备注:如果你想让它在 SpringBootTest 下测试,你可能需要更多的提示。
supply a test app as context. There must be a more elegant way, but I did it like this:
提供一个测试应用程序作为上下文。一定有更优雅的方式,但我是这样做的:
@SpringBootTest(classes = {TestApplication.class,..
@SpringBootApplication
public class TestApplication {
public static void main(String[] args) throws Throwable {
SpringApplication.run(TestApplication.class, args);
}
}
use TestPropertySource if your app.properties is under the TestSources path:
如果您的 app.properties 在 TestSources 路径下,请使用 TestPropertySource:
@TestPropertySource(locations="classpath:application.properties")
回答by Yogesh Badke
Since you are using ConfigurationProperties
annotation, instead of
由于您使用的是ConfigurationProperties
注释,而不是
@Value("${teddy.list}")
private TeddyBear[] teddyBears;
You can directly do
你可以直接做
private List<TeddyBear> list;
No need of @Value
annotation.
不需要@Value
注释。
Also, the variable name has to be list
because that is what you have provided into yml.
此外,变量名称必须是list
因为这是您提供给 yml 的内容。