It seems like that you excepted to load valid message dynamiclly .
you can add static constants which would define in a common constants class.
the code snippet is like following
step1. creat constants class
public class ValidationMessages {
public static final String LAST_NAME_REQUIRED = "user.lastname_required";
}
// In your POJO
@NotEmpty(message = "{" + ValidationMessages.LAST_NAME_REQUIRED + "}")
private String lastName;
step 2. load message from application
you can use @Value annotation on setmethod that inject value into a static field
@Component
public class ValidationMessages {
public static String LAST_NAME_REQUIRED = "user.lastname_required";
// the message which behind ':' is indicate default for the injection
@Value("${user.lastname_required:last_name is required}")
public void setLastname (String message) {
LAST_NAME_REQUIRED = message;
}
}
one more word ,
Define message in spring application.properties
,bind the validation message lifecycle scope with spring application that i think it is not the best practise in product environment. when ever you would like to change the message , if there is no hot refresh spring config in no shutdown model , it should refresh config via restart application , if it is frequently modified , it means that you would restart app in every modification.
The validation message should be a constants , not load from anywhere dynamiclly