扩展Hibernate的ResourceBundleMessageInterpolator

时间:2018-01-18 13:43:31

标签: java spring hibernate interpolation

我想扩展这个课程:textView.setRotation(-120); imageView.setRotation(-120);

并覆盖此方法:

org.hibernate.validator.messageinterpolation.ResourceBundleMessageInterpolator

我创建了public String interpolate(String message, Context context, Locale locale) { ... }

MyResourceBundleMessageInterpolator

我也知道我必须在public class MyResourceBundleMessageInterpolator extends ResourceBundleMessageInterpolator { @Override public String interpolate(String message, Context context, Locale locale) { return super.interpolate(message, context, locale); } } 对象中注册我的消息插补器:

ValidatorFactory

但问题是我不知道接下来该做什么。如何告诉Hibernate使用这个@Component public class MyResourceBundleMessageInterpolatorConfigurer extends ResourceBundleMessageInterpolator { @PostConstruct public void init() { ValidatorFactory validatorFactory = Validation.byDefaultProvider() .configure() .messageInterpolator(new MyResourceBundleMessageInterpolator()) .buildValidatorFactory(); } } 对象?

我使用的是Hibernate 4.3.2 + Spring 4.3.13

1 个答案:

答案 0 :(得分:0)

您不需要在ValidatorFactory对象中注册消息插值器。唯一的下一步是覆盖LocalValidatorFactoryBean类中的默认@Configuration

@Configuration
public class MyValidationConfig
{
    @Bean
    public static LocalValidatorFactoryBean myValidatorFactory ()
    {
        LocalValidatorFactoryBean factoryBean = new LocalValidatorFactoryBean();
        factoryBean.setMessageInterpolator(messageInterpolator());
        return factoryBean;
    }

    private static MessageInterpolator messageInterpolator ()
    {
        return new MyResourceBundleMessageInterpolator();
    }
}