1

我在我的小项目中使用 Spring Boot 1.4.2。我的配置类如下

@Component
@PropertySource("classpath:global1.yml")
@ConfigurationProperties
public class GlobalProperties {
    private String name;

    private List<Menu> menus = new ArrayList<>();

    public String getName() {
        return name;
    }

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

    public List<Menu> getMenus() {
        return menus;
    }

    public void setMenus(List<Menu> menus) {
        this.menus = menus;
    }

    @Override
    public String toString() {
        return "GlobalProperties{" +
                ", name='" + name + '\'' +
                ", menus=" + menus + '\'' +
                '}';
    }
}

global1.yml

name: "helloworld"
menus:
    - title: Home
      name: Home
      path: /
    - title: Login
      name: Login
      path: /login

如果我没有menus在 YAML 文件中添加列表,则代码很好。但是有了上面的文件,我得到了

Property: target.menus
Value: 
Reason: Failed to convert property value of type 'java.lang.String' to required type 'java.util.List' for property 'menus'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'java.lang.String' to required type 'com.test.Menu' for property 'menus[0]': no matching editors or conversion strategy found

另外,如果我将所有这些属性都放在application.yml. 一切正常。请解释并帮助我解决这个问题。

4

1 回答 1

2

我刚刚想通了。根据此处的文档:Externalized Configuration,我不能有 2 个 yaml 文件,并且 yaml 文件无法通过@PropertySource.

于 2018-01-27T01:01:33.307 回答