@PropertySource和地图绑定的问题

时间:2019-03-27 09:23:18

标签: spring spring-boot configuration

我的yml配置文件存在此特定问题。 我有一个多模块的Maven项目,如下所示:

app
 |-- core
 |-- web
 |-- app

我在core项目中有此配置文件

@Configuration
@PropertySource("core-properties.yml")
public class CoreConfig {

}

此映射:

@Component
@ConfigurationProperties(prefix = "some.key.providers.by")
@Getter
@Setter
public class ProvidersByMarket {

  private Map<String, List<String>> market;

}

这是我的core-properties.yml

some.key.providers:
  p1: 'NAME1'
  p2: 'NAME2'

some.key.providers.by.market:
  de:
    - ${some.key.providers.p1}
    - ${some.key.providers.p2}
  gb:
    - ${some.key.providers.p1}

例如,当我通过配置文件激活加载文件时,将文件重命名为application-core-properties.yml,然后-Dspring.profiles.active=core-properties 可以工作,但是如果我尝试加载文件通过@PropertySource("core-properties.yml"),它没有,并且出现以下错误:

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2019-03-27 10:07:36.397 -ERROR 13474|| --- [  restartedMain] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************

Description:

Failed to bind properties under 'some.key.providers.by.market' to java.util.Map<java.lang.String, java.util.List<java.lang.String>>:

    Reason: No converter found capable of converting from type [java.lang.String] to type [java.util.Map<java.lang.String, java.util.List<java.lang.String>>]

Action:

Update your application's configuration


Process finished with exit code 1

3 个答案:

答案 0 :(得分:0)

因为您没有相同的属性结构, 例子

spring:
    profiles: test
name: test-YAML
environment: test
servers: 
    - www.abc.test.com
    - www.xyz.test.com

---
spring:
    profiles: prod
name: prod-YAML
environment: production
servers: 
    - www.abc.com
    - www.xyz.com

配置类应为

@Configuration
@EnableConfigurationProperties
@ConfigurationProperties
public class YAMLConfig {

    private String name;
    private String environment;
    private List<String> servers = new ArrayList<>();

    // standard getters and setters

答案 1 :(得分:0)

我已经解决了实施以下here中所述的以下app.controller('tabOne', function($scope, dataService) { $scope.data = {}; $scope.load = function () { if(dataService.getFirstTabData()) { $scope.data = dataService.getFirstTabData(); } else { $scope.data = getDataFromAPI(); } }; var getDataFromAPI = function () { //get your data dataService.setFirstTabData(dataFromAPI); }; }); 的问题

PropertySourceFactory

答案 2 :(得分:0)

我遇到了类似的问题,发现了这样的解决方法:

$ dmesg
...
[ 1184.989896] Out of memory: Kill process 3101 (dd) score 204 or sacrifice child
[ 1184.989957] Killed process 3101 (dd) total-vm:2104672kB, anon-rss:2097192kB, file-rss:1456kB, shmem-rss:0kB
[ 1185.067934] oom_reaper: reaped process 3101 (dd), now anon-rss:0kB, file-rss:0kB, shmem-rss:0kB

到目前为止,我的解决方案是

diacritic:
  isEnabled: true
  chars: -> I wanted this to be parsed to map but it didn't work
    ą: a
    ł: l
    ę: e