我已经在整个stackoverflow和网络中寻找了解决方案。我没有看到任何可行的解决方案,因为也许所有帖子都不完全适合我的用例,其中包含文件内的列表以及对象结构。

这是一个 sample 作为yaml

teddy.list:
    -
      name: Red
      price: Five
    -
      name: Blue
      price: One
    -
      name: Yellow
      price: Two
    -
      name: Green
      price: Three

这是与属性文件相同的示例
teddy.list[0].name=Red
teddy.list[0].price=Five
teddy.list[1].name=Blue
teddy.list[1].price=One
teddy.list[2].name=Yellow
teddy.list[2].price=Two
teddy.list[3].name=Green
teddy.list[3].price=Three

我希望能够向我的应用程序提供teddy.yml或teddy.properties文件以进行配置。

这是我的类(class):
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;

@Configuration
@PropertySource(name = "props", value = "classpath:teddy.yml", ignoreResourceNotFound = false)
@ConfigurationProperties(prefix = "teddy")
public class TeddyBearConfig {

    @Autowired
    Environment env;

    @Value("${teddy.list}")
    private TeddyBear[] teddyBears;

    public TeddyBear[] getTeddyBears() {
        return teddyBears;
    }

    public void setTeddyBears(TeddyBear[] teddyBears) {
        this.teddyBears = teddyBears;
    }

    public static class TeddyBear {
        private String name;
        private String price;

        public TeddyBear() {

        }

        public TeddyBear(String name, String price) {
            this.name = name;
            this.price = price;
        }

        public String getName() {
            return name;
        }

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

        public String getPrice() {
            return price;
        }

        public void setPrice(String price) {
            this.price = price;
        }
    }
}

我已经尝试了此设置,使用环境尝试访问属性,删除了前缀,并声明了“PropertySourcesPlaceholderConfigurer” bean。

使用当前代码,spring抛出IllegalStateException,因为它无法将java.lang.string转换为我的TeddyBear类。

最佳答案

这应该工作。

@Configuration
@PropertySource(name = "props", value = "classpath:teddy.properties", ignoreResourceNotFound = false)
@ConfigurationProperties(prefix = "teddy")
public class TeddyBearConfig {

  private List<TeddyBear> list;

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

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

  public static class TeddyBear {
    private String name;
    private String price;

    public TeddyBear() {

    }

    public TeddyBear(String name, String price) {
      this.name = name;
      this.price = price;
    }

    public String getName() {
      return name;
    }

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

    public String getPrice() {
      return price;
    }

    public void setPrice(String price) {
      this.price = price;
    }
  }
}

更新:

上面的代码适用于您上面给出的属性文件。
如果您想使用yml文件,可以这样做。但有几点。
1.您的yml结构不正确,应该是这样
teddy:
  list:
    -
      name: Red
      price: Five
    -
      name: Blue
      price: One
    -
      name: Yellow
      price: Two
    -
      name: Green
      price: Three

2.修复了yml结构(以及TeddyBearConfig中的文件名)之后,您会看到springboot在启动过程中不会发出任何投诉,但是TeddBearConfig中的list变量将为null。这是springboot通过@PropertySource处理yml文件的方式中的一个错误。

3.如果将此yml内容移动到application.yml并删除配置文件中的@PropertySource行,您会看到一切正常。

10-07 20:57