Home » Java » java – Beans created by ImportBeanDefinitionRegistrar during autoconfiguration are not eligible for all bean postprocessors-Exceptionshub

java – Beans created by ImportBeanDefinitionRegistrar during autoconfiguration are not eligible for all bean postprocessors-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment

Questions:

I have implemented ImportBeanDefinitionRegistrar to create bean definitions from external source. It has to be registrar since I do not know in advance how many bean definitions will be created.

When I use the registrar on my Application class like this:

@SpringBootApplication
@Import(GenesysRegistrar.class)
public class IntegrationServer {
...
}

everything works fine.

I wanted to make the import automatic for every application that uses the JAR that contains the registrar so I have created following class:

@Configuration
@Import(GenesysRegistrar.class)
public class GenesysAutoConfiguration {
    /**/
}

and registered it in META-INF/spring.factories under key org.springframework.boot.autoconfigure.EnableAutoConfiguration.

Now auto-configuration works, but I get following messages in log:

Bean 'xxx' of type [XXX] is not eligible for getting processed by all BeanPostProcessors

What is the difference between importing the registry from aplication class and from autoconfiguration class? I have found that following post processors are created AFTER my beans:

methodValidationPostProcessor
persistenceExceptionTranslationPostProcessor
webServerFactoryCustomizerBeanPostProcessor
errorPageRegistrarBeanPostProcessor

I have tried to put @AutoConfigureOrder(Integer.MAX_VALUE) on my auto-configuration class, but it does not change anything.

Any ideas how can I fix the order so beans from definitions created by my registrar are processed after all post processors? Why Spring creates them before all post processors? Any why the issue does not happen when using @Import on application class?

How to&Answers:

Early initialized beans are dependencies injected to other bean factoy by constructor injection.

If the bean factory uses property injection instead of construcotr injection it does not happen.

Question is if constructor injection on factory bean is “bad practise” – have not found anything that prohibits it.

Created ticket to spring-boot (as it happens only during boot auto-configuraiton):
Issue #20219