gpt4 book ai didi

java - 如何将另一个属性文件中的值注入(inject)到 ValidationMessages.properties 中?

转载 作者:行者123 更新时间:2023-12-02 05:01:36 25 4
gpt4 key购买 nike

我需要更改 hibernate-validator 使用的 @NotNull 消息。

已使用 ValidationMessages.properties 中的一行成功完成此操作,例如:javax.validation.constraints.NotNull.message=我的验证消息

但是,这并不是我们所需要的。将省略有关原因的详细信息,但所有这些消息都应源自单个 messages.properties 文件,其目的不仅用于验证,还用于其他消息。

假设 messages.properties 最初包含一个属性 CCCI_0001=my generic message。是否可以以某种方式将此属性值从 messages.properties 替换为 ValidationMessages.properties 中的占位符?

最佳答案

您可以做的是使用自定义的 MessageInterpolator 引导您的 ValidatorFactory。如果您愿意使用 Hibernate Validator 特定功能,则可以使用 ResourceBundleLocator - 请参阅 http://docs.jboss.org/hibernate/stable/validator/reference/en-US/html_single/#section-custom-message-interpolation 。它看起来有点像这样:

Validator validator = Validation.byDefaultProvider()
.configure()
.messageInterpolator(
new ResourceBundleMessageInterpolator(
new PlatformResourceBundleLocator( "MyMessages" )
)
)
.buildValidatorFactory()
.getValidator();

关于java - 如何将另一个属性文件中的值注入(inject)到 ValidationMessages.properties 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28225718/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com