0

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?

P.S.

My main idea that I want to extend class which uses hibernate by default and replace it with my new implementation.

1
  • It seems you can get this to work, but I am wondering whether this a good idea. Since you are still using the @Min annotation in the entity itself, you will have to set some value there (since it is required). At the same time you potentially use a different value via your external configuration file. I find this confusing and potentially very misleading. Commented May 21, 2015 at 8:59

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.