2 回答

TA貢獻1865條經驗 獲得超7個贊
你必須使用SpEL(彈簧表達式語言),這是一個非常強大的工具:)
比你可以做這樣的事情:
@Value("#{${my.param} > PARAM1_MIN || ${my.param} < PARAM1_MAX ? ${my.param} : DEFAULT_PARAM1"})
private int param;
或者可能更具可讀性:
@Value("#{checker.inRange(${my.param})}")
private int param;
使用組件:
@Component("checker")
private class PropertyChecker {
public int inRange(int param) {
......
}
}
一個不錯的教程在這里:https://www.baeldung.com/spring-expression-language 或:https://www.baeldung.com/spring-value-annotation

TA貢獻2037條經驗 獲得超6個贊
可以使用一些驗證約束,例如 、和包 Java 驗證 API 中的許多其他約束@Max
@Min
@NotEmpty
請參閱春季文檔。
下面是一個示例:
public class User {
@NotNull(message = "Name cannot be null")
private String name;
@AssertTrue
private boolean working;
@Size(min = 10, max = 200, message
= "About Me must be between 10 and 200 characters")
private String aboutMe;
@Min(value = 18, message = "Age should not be less than 18")
@Max(value = 150, message = "Age should not be greater than 150")
private int age;
@Email(message = "Email should be valid")
private String email;
// standard setters and getters
}
添加回答
舉報