【问题标题】:Spring - YML array property is nullSpring - YML 数组属性为空
【发布时间】:2017-05-15 09:55:58
【问题描述】:

我徒劳地试图从application.yml 中读取字符串数组。 Environment@Value 注释始终返回 null。

如果我读取一个项目,而不是整个数组,一切正常。

代码如下:

来源


引导应用程序和休息控制器

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

@RestController
class WithEnvCtrl {

    @Autowired
    private Environment env;

    @RequestMapping(value = "/with_env", method = { RequestMethod.GET, RequestMethod.POST }, produces = "application/json")
    public String test() {

        System.err.println(env.getProperty("this.is.array[0]"));
        System.err.println(env.getProperty("this.is.array", List.class));
        System.err.println(env.getProperty("this.is.array", String[].class));

        return env.getProperty("this.is.array[0]");
    }
}

@RestController
class WithValueAnnotation {

    @Value("${this.is.array[0]}")
    private String first;

    @Value("${this.is.array}")
    private List<String> list;

    @Value("${this.is.array}")
    private String[] array;

    @RequestMapping(value = "/with_value_annotation", method = { RequestMethod.GET, RequestMethod.POST }, produces = "application/json")
    public String test() {

        System.err.println(first);
        System.err.println(list);
        System.err.println(array);

        return first;
    }
}

application.yml 文件

this:
  is:
    array: 
      - "casa"
      - "pesenna"

结果


WithEnvCtrl.test 方法打印:
casa
null
null
null

WithValueAnnotation.test 方法正确地将变量 first 设置为数组的第一个元素 (casa)。但是属性listarray上的注解@Value会导致异常:

java.lang.IllegalArgumentException: Could not resolve placeholder 'this.is.array' in string value "${this.is.array}"

这是一个示例项目:property-array

非常感谢!

【问题讨论】:

  • 如果你想绑定整个列表,我认为你需要使用@ConfigurationProperties。查看文档中的“加载 YAML”一章:docs.spring.io/spring-boot/docs/1.4.3.RELEASE/reference/…
  • 添加注解@ConfigurationProperties(prefix="this.is")并定义属性private List&lt;String&gt; array,结果总是:array为null。
  • 我认为文档提到您还需要一个二传手。你有吗?
  • 通过get 方法而不是this.array 访问属性来解决:仅当我调用get 方法时,该属性才有效。非常感谢您的支持!
  • @Alex 在发布答案之前,我认为它适合全面测试解决方案。

标签: java spring spring-boot yaml spring-cloud


【解决方案1】:

解决者:

  • 使用注解@ConfigurationProperties
  • 声明一个与 yml 属性同名的属性;
  • 定义属性的get方法;
  • 初始化属性或定义set方法。

代码如下:

import java.util.List;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

@RestController
@ConfigurationProperties(prefix="this.is")
class WithValueAnnotation {

    private List<String> array;

    public List<String> getArray(){
        return this.array;
    }

    public void setArray(List<String> array){
        this.array = array;
    }

    @RequestMapping(value = "/test_cfg", method = { RequestMethod.GET,
            RequestMethod.POST }, produces = "application/json")
    public String test() {

        System.err.println(array);

        return array.toString();
    }
}

感谢@Quagaar。

【讨论】:

    猜你喜欢
    • 2019-03-04
    • 2018-08-21
    • 1970-01-01
    • 1970-01-01
    • 2017-09-07
    • 2020-02-14
    • 1970-01-01
    • 2011-01-05
    • 2021-03-28
    相关资源
    最近更新 更多