简体   繁体   English

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

[英]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我有 Spring 引导应用程序, application.properties看起来像这样

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

I would like these to be deserialized into a Map我希望将这些反序列化为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;
  }

Option 1:选项1:

In your configuration class read the application.properties file using @PropertySource annotation as below.在您的配置 class 中,使用 @PropertySource 注释读取 application.properties 文件,如下所示。

MyAppConfiguration.java 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 {

}

Then in your POJO (or any other spring component), you can Environment class to get the properties.然后在您的 POJO(或任何其他 spring 组件)中,您可以 Environment class 获取属性。

MyPojo.java 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;
    }

}

Please note that, with this option you are reading the application.properties file twice.请注意,使用此选项,您将读取 application.properties 文件两次。 If you are ok with it, you can choose option 1. Otherwise, you can choose option 2.如果你没问题,你可以选择选项1。否则,你可以选择选项2。

Option 2:选项 2:

MyAppConfiguration.java MyAppConfiguration.java

import org.springframework.context.annotation.Configuration;

@Configuration
public class MyAppConfiguration {

}

MyPojo.java 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;
    }

}

Edit:编辑:

Sorry, Earlier I misunderstood your question.对不起,之前我误解了你的问题。 If you know the property prefix, then you can use @ConfigurationProperties as shown below.如果您知道属性前缀,则可以使用@ConfigurationProperties,如下所示。 The above options are to read all properties without knowing the property prefix.以上选项是在不知道属性前缀的情况下读取所有属性。

MyAppConfiguration.java 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 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;
    }

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 How can I map properties from spring application.properties to java object using lombok, when multiple dot(.) and hyphen(-) is there? - How can I map properties from spring application.properties to java object using lombok, when multiple dot(.) and hyphen(-) is there? 我应该如何为Spring创建一个Java application.properties文件? - How should I create a Java application.properties file for Spring? 如何阅读Java代码中的application.properties - How to read application.properties in Java code 如何在 Spring Boot 应用程序的 application.properties 文件中设置 MyBatis 配置属性? - How do I set MyBatis configuration properties in an application.properties file in a Spring Boot application? 如何通过 map <string,map<string,integer> &gt; 使用 application.properties </string,map<string,integer> - How to pass a map<String,Map<String,Integer>> with application.properties 如何将test / java / application.properties中的某些字段链接到main / java / application.properties中的相同字段? - How to link some field in test/java/application.properties to the same field in main/java/application.properties? 如何在SpringBoot application.properties文件中对映射进行软编码,而不是对硬编码进行映射? - How to softcode map in SpringBoot application.properties file instead of hardcoding? 如何更改 application.properties 中的属性? - How can i change properties in application.properties? 在 Apache Tomcat 上部署 .war 时如何使用给定的 application.properties 文件? - How do I use a given application.properties file when deploying a .war on Apache Tomcat? Spring-Boot:如何在@ImportResource中引用application.properties - Spring-Boot: How do I reference application.properties in an @ImportResource
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM