下面列出了怎么用org.hibernate.validator.messageinterpolation.ResourceBundleMessageInterpolator的API类实例代码及写法,或者点击链接到github查看源代码。
@PostConstruct
private void init() {
ValidatorProperties properties = ContextUtil.bean(ValidatorProperties.class);
HibernateValidatorConfiguration configuration = Validation.byProvider(HibernateValidator.class).configure();
//配置hibernate-validator消息插值源
MessageInterpolator interpolator = new ResourceBundleMessageInterpolator(new PlatformResourceBundleLocator(properties
.getMessageConfigPath()));
configuration.messageInterpolator(interpolator);
//配置快速失败
configuration.failFast(properties.getFailFast());
failFast = properties.getFailFast();
engine = configuration.buildValidatorFactory().getValidator();
}
@Override
public GeneralValidator getContext(final Class<?> type) {
final ResourceBundleLocator resourceBundleLocator = new PlatformResourceBundleLocator("messages");
final MessageInterpolator messageInterpolator = new ResourceBundleMessageInterpolator(resourceBundleLocator);
final Configuration<?> config = Validation.byDefaultProvider().configure()
.messageInterpolator(messageInterpolator);
final BootstrapConfiguration bootstrapConfiguration = config.getBootstrapConfiguration();
final boolean isExecutableValidationEnabled = bootstrapConfiguration.isExecutableValidationEnabled();
final Set<ExecutableType> defaultValidatedExecutableTypes = bootstrapConfiguration
.getDefaultValidatedExecutableTypes();
return new GeneralValidatorImpl(validatorFactory, isExecutableValidationEnabled,
defaultValidatedExecutableTypes);
}
@Bean
public LocalValidatorFactoryBean validator() {
PlatformResourceBundleLocator resourceBundleLocator =
new PlatformResourceBundleLocator(ResourceBundleMessageInterpolator.USER_VALIDATION_MESSAGES, null, true);
LocalValidatorFactoryBean factoryBean = new LocalValidatorFactoryBean();
factoryBean.setMessageInterpolator(new ResourceBundleMessageInterpolator(resourceBundleLocator));
return factoryBean;
}
@Override
public ValidationConfig getContext(final Class<?> type) {
return new ValidationConfig()
.constraintValidatorFactory(resourceContext.getResource(InjectingConstraintValidatorFactory.class))
.parameterNameProvider(new ParanamerParameterNameProvider())
.messageInterpolator(
new ResourceBundleMessageInterpolator(
buildBundleLocator(VALIDATION_MESSAGE_BUNDLE_NAME),
buildBundleLocator(Messages.BUNDLE_NAME),
mode.isProd()
)
);
}
public static MessageInterpolator buildMessageInterpolator(MessageSource messageSource) {
return new ResourceBundleMessageInterpolator(new MessageSourceResourceBundleLocator(messageSource));
}
public static MessageInterpolator buildMessageInterpolator(MessageSource messageSource) {
return new ResourceBundleMessageInterpolator(new MessageSourceResourceBundleLocator(messageSource));
}
private AbstractMessageInterpolator messageInterpolator() {
if (useResourceBundleMessageInterpolator()) {
return new ResourceBundleMessageInterpolator();
}
return new ParameterMessageInterpolator();
}
private AbstractMessageInterpolator messageInterpolator() {
if (useResourceBundleMessageInterpolator()) {
return new ResourceBundleMessageInterpolator();
}
return new ParameterMessageInterpolator();
}
public static MessageInterpolator buildMessageInterpolator(MessageSource messageSource) {
return new ResourceBundleMessageInterpolator(new MessageSourceResourceBundleLocator(messageSource));
}
public static MessageInterpolator buildMessageInterpolator(MessageSource messageSource) {
return new ResourceBundleMessageInterpolator(new MessageSourceResourceBundleLocator(messageSource));
}
protected ResourceBundleMessageInterpolator newResourceBundleMessageInterpolator() {
return new ResourceBundleMessageInterpolator(newResourceBundleLocator());
}