java - How do I deserialize a series of application.properties to a Map in Java
问题描述
I have Spring Boot Application, with application.properties
that look like so
com.mycompany.schedule.test=welcome
com.mycompany.schedule.test2=hi there
I would like these to be deserialized into a Map
I have tried the following, and other variants, but it doesnt seem to work
private Map<String, String> schedules;
@Value("${com.mycompany.schedule}")
public void setSchedules(Map<String, String> values) {
this.schedules = values;
}
解决方案
选项1:
在您的配置类中,使用 @PropertySource 注释读取 application.properties 文件,如下所示。
MyAppConfiguration.java
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
@Configuration
@PropertySource(name = "customPropertySource", value = "classpath:application.properties")
public class MyAppConfiguration {
}
然后在您的 POJO(或任何其他 spring 组件)中,您可以使用 Environment 类来获取属性。
MyPojo.java
import java.util.Map;
import javax.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.io.support.ResourcePropertySource;
import org.springframework.stereotype.Component;
@Component
public class MyPojo {
@Autowired
private ConfigurableEnvironment env;
private Map<String, Object> schedules;
@PostConstruct
public void properties() {
Object propSourceObj = env.getPropertySources().get("customPropertySource");
if (propSourceObj instanceof ResourcePropertySource) {
ResourcePropertySource propSource = (ResourcePropertySource) propSourceObj;
setSchedules(propSource.getSource());
}
System.out.println("Schedules: " + getSchedules());
}
public Map<String, Object> getSchedules() {
return schedules;
}
public void setSchedules(Map<String, Object> schedules) {
this.schedules = schedules;
}
}
请注意,使用此选项,您将读取 application.properties 文件两次。如果你没问题,你可以选择选项1。否则,你可以选择选项2。
选项 2:
MyAppConfiguration.java
import org.springframework.context.annotation.Configuration;
@Configuration
public class MyAppConfiguration {
}
MyPojo.java
import java.util.Map;
import javax.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.env.OriginTrackedMapPropertySource;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.stereotype.Component;
@Component
public class MyPojo {
@Autowired
private ConfigurableEnvironment env;
private Map<String, Object> schedules;
@PostConstruct
public void properties() {
Object propSourceObj = env.getPropertySources().get("applicationConfig: [classpath:/application.properties]");
if (propSourceObj instanceof OriginTrackedMapPropertySource) {
OriginTrackedMapPropertySource propSource = (OriginTrackedMapPropertySource) propSourceObj;
setSchedules(propSource.getSource());
}
System.out.println("Schedules: " + getSchedules());
}
public Map<String, Object> getSchedules() {
return schedules;
}
public void setSchedules(Map<String, Object> schedules) {
this.schedules = schedules;
}
}
编辑:
对不起,之前我误解了你的问题。如果您知道属性前缀,则可以使用@ConfigurationProperties,如下所示。以上选项是在不知道属性前缀的情况下读取所有属性。
MyAppConfiguration.java
import javax.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MyAppConfiguration {
@Autowired
private MyCompanyConfigurationProperties myCompanyConfProps;
@PostConstruct
public void testProperteis() {
System.out.println("My Properties: " + myCompanyConfProps.getSchedule());
}
}
MyCompanyConfigurationProperties.java
import java.util.HashMap;
import java.util.Map;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix = "com.mycompany")
public class MyCompanyConfigurationProperties {
private Map<String, String> schedule = new HashMap<String, String>();
public Map<String, String> getSchedule() {
return schedule;
}
public void setSchedule(Map<String, String> schedule) {
this.schedule = schedule;
}
}
推荐阅读
- c++ - 函数模板特化生成链接错误
- python - 函数如何在这个 for 循环之外执行?
- python - 如何在绘制分布曲线时在 x_axis 中绘制月/年?
- html - 如何防止网格列随着文本的增加而改变大小(html/css)?
- bash - 逐行比较 4 个文件,看它们是否匹配
- java - 如何在 Spring Boot 项目中制作谷歌自动登录功能?
- google-apps-script - Gscript 中的新段落
- jekyll - Jekyll+Github 页面:如何使用 `remote_theme` 进行主题代码的特定修订
- javascript - 从数组中淡入随机引用
- ruby-on-rails - 如何在 Ruby on Rails 上使用 3D stl 查看器