gpt4 book ai didi

java - 如何将一系列 application.properties 反序列化为 Java 中的 Map

转载 作者:行者123 更新时间:2023-12-01 21:57:43 24 4
gpt4 key购买 nike

我有 Spring Boot 应用程序,application.properties 看起来像这样

com.mycompany.schedule.test=welcome
com.mycompany.schedule.test2=hi there

我希望将它们反序列化为Map

我尝试了以下方法和其他变体,但似乎不起作用

  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;
}

}

关于java - 如何将一系列 application.properties 反序列化为 Java 中的 Map,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58729970/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com