I annotate field like this:
@Min(100)
private Long cost;
To achieve dynamic update of min value I extended from hibernate validator class:
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);
}
}
Register bean, which will refresh min value from 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);
}
}
when I start my application I see the following message:
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
Message is clear for me but I don't understand how to fix it?
My main idea that I want to extend class which uses hibernate by default and replace it with my new implementation.
Add @Component
tag to CustomMinValidator
class.
@Component
public class CustomMinValidator extends MinValidatorForNumber
and also to initiate a bean, you need no-arg
constructor inside CustomMinValidator
class. Since, you don't have any other constructors, the default no-arg constructor should be sufficient.