Spring - YML数组属性为null

时间:2016-12-31 10:16:42

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

我徒劳地试图从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)。但是,属性@Valuelist上的注释array会导致异常:

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

以下是一个示例项目:property-array

非常感谢提前!

1 个答案:

答案 0 :(得分:3)

解决方案:

  • 使用注释@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。