Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/335.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

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启动验证_Java_Spring Boot_Validation - Fatal编程技术网

Java 使用环境变量进行Spring启动验证

Java 使用环境变量进行Spring启动验证,java,spring-boot,validation,Java,Spring Boot,Validation,我想将spring boot环境变量的值放入验证注释(@Min,@Max),但我不知道如何做。这是我的密码: public class MessageDTO { @Value("${validationMinMax.min}") private Integer min; @JsonProperty("Message_ID") @NotBlank(message = "messageId cannot be blank.") @Pat

我想将spring boot环境变量的值放入验证注释(@Min,@Max),但我不知道如何做。这是我的密码:

public class MessageDTO {

@Value("${validationMinMax.min}")
private Integer min;

@JsonProperty("Message_ID")
@NotBlank(message = "messageId cannot be blank.")
@Pattern(regexp = "\\w+", message = "messageId don't suits the pattern")
private String messageId;

@JsonProperty("Message_Type")
@NotBlank(message = "messageType cannot be blank")
private String messageType;

@JsonProperty("EO_ID")
@NotBlank(message = "eoId cannot be blank")
private String eoId;

@JsonProperty("UI_Type")
@NotNull(message = "uiType cannot be null")
@Min(1)
@Max(3)
private Integer uiType;
这是我的申请表。yml:

server:
  port: 8080 
spring:
  data:
    cassandra:
      keyspace-name: message_keyspace
      port: 9042
      contact-points:
        - localhost

validationMinMax:
  min: 1
  max: 3

我想将yml的字段“min”和“max”放入属性uiType的注释字段@min()和@max()。有人知道怎么做吗?提前感谢您的帮助

您可以使用自定义验证器编写自己的验证注释。在此验证器中,您可以自动连接SpringBean并注入配置属性:

@Target({TYPE,ANNOTATION\u TYPE})
@保留(运行时)
@约束(validatedBy={MyValidator.class})
@记录
public@interface MyValidationAnnotation{
字符串消息()默认为“”;
类[]组()默认值{};

CLASS有一个简单的答案,你不能。注释是静态元数据,因此不能被注入。对某些注释的支持来源于这样一个事实,即在读取注释时,表达式会被计算。然而,这只适用于spring管理的注释,因为这些不是这样的东西,这种支持是不存在的不可能。