Affects: 6.0.4


Autowire doesn't seem to work in ConstraintValidators when running in native mode. I've added hints but they didn't resolve the issue. The only thing that helps is to register AutowiredAnnotationBeanPostProcessor after the application starts for instance in CommandLineRunner i.e.

   @Bean
    CommandLineRunner registerAutowiredBeanPostProcessor(AbstractBeanFactory abstractBeanFactory) {
        return args -> {
            if (abstractBeanFactory.getBeanPostProcessors().stream().noneMatch(AutowiredAnnotationBeanPostProcessor.class::isInstance)) {
                abstractBeanFactory.addBeanPostProcessor(new AutowiredAnnotationBeanPostProcessor());
            }
        };
    }

Otherwise the following exception is thrown:

java.lang.NoSuchMethodException: com.example.springbootautowireissue.validator.DemoConstraintValidator.<init>()
    at java.base@17.0.6/java.lang.Class.getConstructor0(DynamicHub.java:3585) ~[com.example.springbootautowireissue.SpringBootAutowireIssueApplication:na]
    at java.base@17.0.6/java.lang.Class.getDeclaredConstructor(DynamicHub.java:2754) ~[com.example.springbootautowireissue.SpringBootAutowireIssueApplication:na]
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:79) ~[com.example.springbootautowireissue.SpringBootAutowireIssueApplication:6.0.4]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1300) ~[com.example.springbootautowireissue.SpringBootAutowireIssueApplication:6.0.4]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1198) ~[com.example.springbootautowireissue.SpringBootAutowireIssueApplication:6.0.4]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:561) ~[com.example.springbootautowireissue.SpringBootAutowireIssueApplication:6.0.4]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:521) ~[com.example.springbootautowireissue.SpringBootAutowireIssueApplication:6.0.4]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:321) ~[com.example.springbootautowireissue.SpringBootAutowireIssueApplication:6.0.4]
    at org.springframework.validation.beanvalidation.SpringConstraintValidatorFactory.getInstance(SpringConstraintValidatorFactory.java:56) ~[na:na]
    at org.hibernate.validator.internal.engine.constraintvalidation.ClassBasedValidatorDescriptor.newInstance(ClassBasedValidatorDescriptor.java:84) ~[na:na]
    at org.hibernate.validator.internal.engine.constraintvalidation.AbstractConstraintValidatorManagerImpl.createAndInitializeValidator(AbstractConstraintValidatorManagerImpl.java:89) ~[com.example.springbootautowireissue.SpringBootAutowireIssueApplication:8.0.0.Final]

Not sure is there a better (supported) way to enable autowiring of validators in native mode?

I've attached a project and if you execute:

curl http://localhost:8080/demo/validate\?value\=value

you will see the error spring-autowire-issue.zip

Comment From: jhoeller

This looks like a duplicate of #29823. With the underlying change in #29855, this should be addressed for 6.0.5 now. Feel free to give a recent 6.0.5 snapshot a try, the release itself is coming up this week.

Comment From: agrancaric

Ok, thanks for a quick reply. I didn't see the issue has been solved already I will give it a try.

Comment From: agrancaric

Tried 6.0.5 snapshot and it works in the example. Unfortunately I cannot try it in the real application since there seems to be an issue with @ConfigurationProperties classes


Exception in thread "main" java.lang.IllegalArgumentException: Code generation is not supported for bean definitions declaring an instance supplier callback : Root bean: class [net.croz.nrich.encrypt.starter.properties.NrichEncryptProperties]; scope=singleton; abstract=false; lazyInit=null; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodNames=null; destroyMethodNames=null