java Spring Boot - 从属性文件注入映射
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43098009/
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 - inject map from properties file
提问by gstackoverflow
property file looks like this:
属性文件如下所示:
url1=path_to_binary1
url2=path_to_binary2
According thisI tried following approach:
根据这个我尝试以下方法:
@Component
@EnableConfigurationProperties
public class ApplicationProperties {
private Map<String, String> pathMapper;
//get and set
}
and in another component I autowired ApplicationProperties:
在另一个组件中,我自动装配了 ApplicationProperties:
@Autowired
private ApplicationProperties properties;
//inside some method:
properties.getPathMapper().get(appName);
produces NullPointerException
.
产生NullPointerException
.
How to correct it?
如何纠正?
update
更新
I have correct according user7757360 advice:
根据 user7757360 的建议,我有正确的建议:
@Component
@EnableConfigurationProperties
@ConfigurationProperties(prefix="app")
public class ApplicationProperties {
and properties file:
和属性文件:
app.url1=path_to_binary1
app.url2=path_to_binary2
Still doesn't work
还是不行
Update 2
更新 2
@Component
@EnableConfigurationProperties
@ConfigurationProperties(prefix="app")
public class ApplicationProperties {
private Map<String, String> app;
and inside application.properties
:
和里面application.properties
:
app.url1=path_to_binary1
app.url2=path_to_binary2
Still doesn't work
还是不行
采纳答案by kchrusciel
NullPointerException
is probably from empty ApplicationProperties
.
NullPointerException
大概是从空ApplicationProperties
。
All custom properties should be annotated @ConfigurationProperties(prefix="custom")
.
After that, on your main class (class with main method) you must add @EnableConfigurationProperties(CustomProperties.class)
.
For autocomplete you can use:
所有自定义属性都应进行注释@ConfigurationProperties(prefix="custom")
。之后,在您的主类(具有 main 方法的类)上,您必须添加@EnableConfigurationProperties(CustomProperties.class)
. 对于自动完成,您可以使用:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
If you use @ConfigurationProperties
without prefix you use only field name. Field name in you properites. In your case path-mapper
, next you specific key
and value
. Example:
如果@ConfigurationProperties
不带前缀使用,则仅使用字段名称。您属性中的字段名称。在你的情况下path-mapper
,接下来你具体key
和value
。例子:
path-mapper.key=value
Remeber after changes in your own properites you need to reload application. Example:
请记住,在您自己的属性发生更改后,您需要重新加载应用程序。例子:
回答by lrv
it would be helpful if you can give a more specific example for property file. You should have the same prefix in the url1 and url2 and then you can use
如果您可以为属性文件提供更具体的示例,将会很有帮助。您应该在 url1 和 url2 中具有相同的前缀,然后您可以使用
@ConfigurationProperties(prefix="my")
@ConfigurationProperties(prefix="my")
as in
如
my.pathMapper.url1=path_to_binary1
my.pathMapper.url2=path_to_binary2
my.pathMapper.url1=path_to_binary1
my.pathMapper.url2=path_to_binary2
@Component
@EnableConfigurationProperties
@ConfigurationProperties(prefix="my")
public class ApplicationProperties {
private Map<String, String> pathMapper;
//get and set for pathMapper are important
}
在https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html#boot-features-external-config-loading-yaml查看更多
回答by Chandan Kumar
There are two things need you need to feed map from properties file. First you need to have a class which has the configuration and target fields to hold data from properties file.
从属性文件中提供地图需要做两件事。首先,您需要有一个包含配置和目标字段的类来保存属性文件中的数据。
@Configuration
@PropertySource("classpath:myprops.properties")
@ConfigurationProperties("props")
@Component
public class Properties{
private Map<String,String> map = new HashMap<String,String>();
// getter setter
}
Secondly define the properties file named myprops.properties with all properties as props
其次定义名为 myprops.properties 的属性文件,其中所有属性都为 props
props.map.port = 443
props.map.active = true
props.map.user = aUser
回答by Lizardman fourtwenty
Have the your.properties file under src/main/resources
and either have it as a
将 your.properties 文件放在下面src/main/resources
并将其作为
@Configuration
@PropertySource("classpath:your.properties")
public class SpringConfig(){}
or have it as a PropertyPlaceholderConfigurer
in your Spring yourApplicationContext.xml
.
或者PropertyPlaceholderConfigurer
在你的 Spring 中使用它yourApplicationContext.xml
。
Then use the property values like
然后使用属性值,如
@Value("app.url1")
String path_to_binary1;
@Value("app.url2")
String path_to_binary2;
// ...
System.out.println(path_to_binary1+path_to_binary2);