如何从Spring中通过PropertyPlaceholderConfigurer加载的属性文件加载键/值对

时间:2017-05-23 02:11:25

标签: spring spring-mvc spring-boot

我有一个属性文件,其键/值对如下所示:

#state/city/counties

fl.regionA.counties=abc,def,ghi,jkl
fl.regionB.counties=xyz,qrs,tuv,wxy

属性文件通过以下代码片段加载:

@Configuration
public class Config {

@Bean
public static PropertyPlaceholderConfigurer properties(){
...
}

如何在StateRegionMapping中加载StateRegionMappings列表:

public class StateRegionMapping{
   private List<String> counties;
   private String state;
   private String region;

   ...

}

2017年5月23日更新:

我的另一个备用键/值结构是这样的:

states.config[0].state=fl
states.config[0].name=regionA
states.config[0].counties=abc,def,ghi,jkl
states.config[1].state=fl
states.config[1].name=regionB
states.config[1].counties=xyz,qrs,tuv,wxy

2 个答案:

答案 0 :(得分:1)

如果我说得对,你想要配置一个状态列表,其中每个州都有一个区域,每个区域都有一个县列表。对于属性文件,这将如下所示:

states-config.states[0].name=fl
states-config.states[0].regions[0].name=regionA
states-config.states[0].regions[0].counties[0]=abc
states-config.states[0].regions[0].counties[1]=def
states-config.states[0].regions[0].counties[2]=ghi
states-config.states[0].regions[0].counties[3]=jkl
states-config.states[0].regions[1].name=regionB
states-config.states[0].regions[1].counties[0]=xyz
states-config.states[0].regions[1].counties[1]=qrs
states-config.states[0].regions[1].counties[2]=tuv
states-config.states[0].regions[1].counties[3]=wxy

使用YAML,这更具可读性:

states-config:
  states:
    - name: fl
      regions:
        - name: regionA
          counties:
            - abc
            - def
            - ghi
            - jkl
        - name: regionB
          counties:
            - xyz
            - qrs
            - tuv
            - wxy

所需的java配置如下所示:

@ConfigurationProperties("states-config")
public class Config {

    private List<State> states = new ArrayList<>();

    public List<State> getStates() {
        return states;
    }

    public void setStates(List<State> states) {
        this.states = states;
    }

    public static class State {
        private String name;
        private List<Region> regions = new ArrayList<>();

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }


        public List<Region> getRegions() {
            return regions;
        }

        public void setRegions(List<Region> regions) {
            this.regions = regions;
        }
    }

    public static class Region {
        private String name;
        private List<String> counties = new ArrayList<>();

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public List<String> getCounties() {
            return counties;
        }

        public void setCounties(List<String> counties) {
            this.counties = counties;
        }
    }

}

对于您的更新:这将使用您的StateRegionMapping类与以下配置一起使用:

@ConfigurationProperties("states")
public class Config {

    private List<StateRegionMapping> config = new ArrayList<>();

    public List<StateRegionMapping> getConfig() {
        return config;
    }

    public void setConfig(List<StateRegionMapping> config) {
        this.config = config;
    }
}

只需进行一项小修改:因为在您的StateRegionMapping课程中,该地区的名称为region,您必须在属性中使用此代码,而不是name

states.config[0].state=fl
states.config[0].region=regionA
....

答案 1 :(得分:0)

绝对有可能!试试这个。

这应该出现在您要加载的类中。

@Value("#{${passingscore.subjectwise}}")
private Map<String, String> passingScorePerSubjectMap;

应该存在于 application.properties 文件中,并定义如下所示的键值映射属性。

passingscore.subjectwise= {'Maths': '40','Physics':'50','Chemistry':'70'}