如何使用继承自定义标准的hibernate验证?

时间:2015-05-19 19:52:20

标签: java spring validation customization hibernate-validator

我注释了这样的字段:

@Min(100)
private Long cost;

为了实现min值的动态更新,我从hibernate验证器类扩展了:

public class CustomMinValidator extends MinValidatorForNumber {
    public void initialize(final Long minValue) {
        Min min = new Min() {
            @Override
            public String message() {
                return null;  //To change body of implemented methods use File | Settings | File Templates.
            }

            @Override
            public Class<?>[] groups() {
                return new Class<?>[0];
            }

            @Override
            public Class<? extends Payload>[] payload() {
                return null;
            }

            @Override
            public long value() {
                return minValue;
            }

            @Override
            public Class<? extends Annotation> annotationType() {
                return null;
            }
        };
        super.initialize(min);
    }
}

注册bean,它将从config:

刷新最小值
@Component
public class MinValidatorRefresher {
    @Autowired
    MessageSource messageSource;
    @Autowired
    CustomMinValidator customMinValidator;

    @PostConstruct
    private void refreshMinValue() {
        Long minValue;
        try {
            minValue = Long.valueOf(messageSource.getMessage("Terminal.MinValue", null, Locale.getDefault()));
        } catch (NumberFormatException e) {
            return;
        }
        customMinValidator.initialize(minValue);
    }
}

当我启动我的应用程序时,我看到以下消息:

Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.terminal.domain.validation.validators.CustomMinValidator com.terminal.configuration.MinValidatorRefresher.customMinValidator; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.terminal.domain.validation.validators.CustomMinValidator] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:509)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:290)
    ... 65 more
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.terminal.domain.validation.validators.CustomMinValidator] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1118)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:967)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:862)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:481)
    ... 67 more

消息对我来说很清楚,但我不明白如何解决它?

P.S。

我的主要想法是我想扩展默认使用hibernate的类,并用我的新实现替换它。

1 个答案:

答案 0 :(得分:1)

@Component标记添加到CustomMinValidator类。

@Component
public class CustomMinValidator extends MinValidatorForNumber

并且还要启动bean,您需要no-arg类中的CustomMinValidator构造函数。因为,你没有任何其他构造函数,默认的no-arg构造函数就足够了。