gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 11:31:39 25 4
gpt4 key购买 nike

我这样注释字段:

@Min(100)
private Long cost;

为了实现最小值的动态更新,我从 hibernate validator 类扩展:

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,它将刷新配置中的最小值:

@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

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

附注

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

最佳答案

@Component标签添加到CustomMinValidator类。

@Component
public class CustomMinValidator extends MinValidatorForNumber

为了启动一个 bean,您需要在 CustomMinValidator 类中使用 no-arg 构造函数。由于您没有任何其他构造函数,因此默认的无参数构造函数应该足够了。

关于java - 如何使用继承自定义标准 hibernate 验证?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30334911/

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