问题
I was writing a Spring Boot Application. I wanted to know does Spring Boot automatically resolve message keys in javax and hibernate validation annotations. For example:
@NotEmpty(message = "${message.key}")
String name;
I have provided @PropertySource
in my application with message properties file and file is also in my classpath. The keys are resolving with @Value
but they are not being resolved in validation annotations.
What could be the reason for this?
Do I need configure a message source bean? Because I have seen examples working without configuring the message source bean.
回答1:
Are your messages in the correct place? Spring Boot automatically registers a MessageSource bean for you, so you should put your messages in the src/main/resources/messages.properties
file. If you have enabled the auto-configuration and also have hibernate-validator dependency on the classpath, everything should work out of the box.
Also @PropertySource
is related to application's configuration properties and not messages so the fact that it's not resolving them is to be expected ^^
来源:https://stackoverflow.com/questions/39149358/does-spring-boot-automatically-resolve-message-keys-in-javax-and-hibernate-valid