java Spring @ConfigurationProperties未Map对象列表

qxsslcnc  于 2023-03-28  发布在  Java
关注(0)|答案(4)|浏览(172)

我面临着奇怪的问题与Spring属性Map器.我有yml包含值列表.我想转换为对象列表,为什么构建应用程序.所以,我用@ConfigurationProperties.有了这个,我能够Map简单的类型.当我使用这个复杂的类型(对象列表)它失败了。没有异常,但值列表是零,当我调试。请在下面找到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;
}
ttisahbt

ttisahbt1#

问题是PropertySource只支持属性文件,你不能从yml文件中读取值。你可以像这样更新它:

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

configuration/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

密码应该能用。

8iwquhpp

8iwquhpp2#

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

24.6.4 YAML缺点
YAML文件不能通过@PropertySource annotation加载,所以如果需要通过这种方式加载值,则需要使用属性文件。

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

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

所以你有两种可能:

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

mrphzbgm4#

我认为问题是在@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}

相关问题