Spring SpEL - Expression Language to create Map of String and Custom Object

前端 未结 2 1972
忘了有多久
忘了有多久 2021-01-17 02:38

I\'m using Spring Boot example to read the following from the properties file.

sub.region.data={\\
    AF: {\'subRegionCd\' : \'34\', \'subR         


        
2条回答
  •  孤城傲影
    2021-01-17 03:28

    Looks like you made a mistake after 'subRegionDesc',, I think you mean using colon, not a comma here

    With spring boot I suggest you to use ConfigurationProperties, instead of @Value.

    For example, in this case you have to:

    1. put @EnableConfigurationProperties(SubRegionConfig.class) to one of your spring configuration class.

    2. Create config class:

      @ConfigurationProperties(prefix = "sub.region")
      public static class SubRegionConfig {
          private Map data;
          //getters and setters
      } 
      
    3. Use .yml instead of .properties, like that:

      sub:
        region:
         data:
           AF:
            subRegionCd: '34'
            subRegionName: 'Southern Asia'
            subRegionDesc: ''
            subRegionStatus: 'A'
      
    4. After that you can get every properties you want from SubRegionConfing

      @Autowired
      private SubRegionConfig subRegionConfig;
      

    ConfigurationsProperties is more complex, but more flexible and preferrable to use in most cases.

提交回复
热议问题