Injection constraint from bean definition, not from autoincrement end (bean is not injectable)

Is there any annotation / trick in Spring that marks @Configuration @Bean as injectable only on strictly qualified @Autowireds?

I would like my bean to be qualified in such a way that only those that specifically require it @Qualifer can inject it. It really is a control mechanism where it can be auto-aware, without any ambiguity or arbitrary decisions depending on the beans in the context.

This way my bean will never autoincrement as a side effect out of my control without me actively marking the injection, waiting for it

+3


source to share


1 answer


You can control the creation of the bean using the @Conditional annotation .

Also if you want real control over the use of the bean, you can create some annotation like @ConroledByQualifer and use that annotation instead of the standard spring component annotation or extension for one (e.g. service, repository ....). So spring cannot handle it for autowired as no one knows how to handle it.



You need to add a custom BeanPostProcessor that will work with @ConroledByQualifer - create it and inject. This way your custom bean will only be handled by the BeanPostProcessor for @ConroledByQualifer, not the custom spring BeanPostProcessors.

+1


source







All Articles