繁体   English   中英

Spring @ConfigurationProperties不映射对象列表

[英]Spring @ConfigurationProperties not mapping list of objects

我面临着Spring属性映射器的奇怪问题。 我有yml包含值列表。 我想将其转换为对象列表,以构建应用程序。 因此,我使用了@ConfigurationProperties。 有了这个我就可以映射简单的类型。 当我使用它来复杂类型(对象列表)时,它失败了。 也不例外,但是当我调试时值列表为零。 请在下面找到yml和java文件。 我尝试使用spring 2.0.0,2.0.1,2.0.2,2.0.3没有成功。 有人可以解决这个问题吗?

application.yml

acme:
  list:
    - name: my name
      description: my description
    - name: another name
      description: another description

AcmeProperties.java

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
@ConfigurationProperties("acme")
@PropertySource("classpath:configuration/yml/application.yml")
public class AcmeProperties {

    private final List<MyPojo> list = new ArrayList<>();

    public List<MyPojo> getList() {
        return this.list;
    }

    static class MyPojo {
        private String name;
        private String description;

        public String getName() {
            return name;
        }

        public String getDescription() {
            return description;
        }
    }
}

使用setter和getter方法:

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@ConfigurationProperties(prefix = "acme")
@PropertySource("classpath:configuration/yml/application.yml")
public class AcmeProperties {

    private List<MyPojo> list;

    public List<MyPojo> getList() {
        return list;
    }

    public void setList(List<MyPojo> list) {
        this.list = list;
    }

    public static class MyPojo {
        private String name;
        private String description;

        public String getName() {
            return name;
        }

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

        public String getDescription() {
            return description;
        }

        public void setDescription(String description) {
            this.description = description;
        }
    }
}

该类的用法:

@Autowired
public HomeController(AppProperties appProperties, AcmeProperties acmeProperties) {
    this.appProperties = appProperties;
    this.acmeProperties = acmeProperties;
}

问题是PropertySource仅支持属性文件,无法从yml文件读取值。 您可以像这样更新它:

@Component
@ConfigurationProperties("acme")
@PropertySource("classpath:/configuration/yml/test.properties")
class AcmeProperties {

配置/ YML / test.properties

acme.list[0].name=my name
acme.list[0].description=my description
acme.list[1].name=another name
acme.list[1].description=another description

和代码应该工作。

根据Spring Boot文档 (重点是我的):

24.6.4 YAML的缺点

无法使用@PropertySource批注加载YAML文件 因此,在需要以这种方式加载值的情况下,需要使用属性文件。

但是您向@PropertySource提供了yml文件:

@Component
@ConfigurationProperties(prefix = "acme")
@PropertySource("classpath:configuration/yml/application.yml")
public class AcmeProperties { ...}  

因此,您有两种可能性:

  • 这个yml文件是Spring Boot配置文件属性文件:在执行应用程序之前启用此配置文件,因此删除@PropertySource
  • 这个yml文件不是Spring Boot配置文件属性文件:请使用属性文件而不是yml文件。

尝试使用@ConfigurationProperties(prefix = "acme")

基于: https : //aykutakin.wordpress.com/2016/02/26/injecting-list-with-spring-from-yaml/

我认为问题是找不到在@PropertySource(“ classpath:configuration / yml / application.yml”)中指定的资源。 -尝试调试代码时犯同样的错误。.-您能否在org.springframework.context.annotation.ConfigurationClassParser#processPropertySource中放置一个断点,并从this.resourceLoader.getResource(resolvedLocation).getFile( )。

就我而言,我有:C:\\ Users \\ user12 \\ Desktop \\ hibernate \\ out \\ production \\ classes \\ configuration \\ application.yml

那里没有application.yml ...

我认为您看不到异常,因为spring在此方法中具有if(ignoreResourceNotFound){..} else {throw ex}

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM