Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 有没有办法在spring boot中验证@Value?_Java_Spring Boot_Spring Properties - Fatal编程技术网

Java 有没有办法在spring boot中验证@Value?

Java 有没有办法在spring boot中验证@Value?,java,spring-boot,spring-properties,Java,Spring Boot,Spring Properties,我正在使用@Value将属性文件中的参数注入应用程序中的变量 因为我不是应用程序中的唯一用户,而且我想确保注射安全,所以我需要在注射之前验证参数 示例: 属性文件: example.string=str1,str2 app.java @Value(${example.string}) public String example; 例如,本例中的预期行为是抛出异常,因为我假设数组case中的“,”id delimiter我认为在注入之前不能这样做,请尝试使用post-construct方法

我正在使用
@Value
将属性文件中的参数注入应用程序中的变量
因为我不是应用程序中的唯一用户,而且我想确保注射安全,所以我需要在注射之前验证参数

示例:

属性文件:

example.string=str1,str2

app.java

@Value(${example.string})
public String example;

例如,本例中的预期行为是抛出异常,因为我假设数组case中的“,”id delimiter

我认为在注入之前不能这样做,请尝试使用post-construct方法

你可以这样做:

@PostConstruct
public void validateValue() {
    if (someProperty.contains(",")) {
        throw new MyException("error");
    }
}

我认为你不能直接使用
@Value
。但您可以这样做,如果验证失败,启动时将失败:

@已验证
@配置属性(prefix=“my.prefix”)
公共类AppProperties{
//此处的验证注释
//@空空如也
//@MyCustomValidation
私有字符串exampleString;
//能手/二传手
}