If initializing bean via constructor dependency injection, Spring will do the candidates look-up by parameter type (essentially via BeanFactoryUtils#beanNamesForTypeIncludingAncestors) which is really slow if application getting huge. We are searching an approach to tell Spring to look-up candidates by constructor's parameter name.

I've read the code in ConstructorResolver but cannot find a way to achieve constructor dependency injection by parameter name. By the way there is a similar question on Stack Overflow: Spring autowire by name constructor arguments, there is just a work-around but not solution.

Can Spring support this feature if parameter name is available in runtime?

Affects: \

Comment From: HzjNeverStop

@snicoll Is there any progress on this issue ?constructor dependency injection in BeanFactoryPostProccesor will easy trigger FactoryBean‘s early init

Comment From: snicoll

@HzjNeverStop I'd argue you should not attempt to inject a bean in a BeanFactoryPostProcessor, as the purpose is to post-process the bean factory before it starts creating bean instance. If you need infrastructure bean, using the *Aware interface is what you should be doing. I am going to hide as this is unrelated to what the OP is asking.

Comment From: snicoll

I can see how getting bean name lookup could be interesting in some cases. We do have support for @Resource setter injection that has by-name semantic but we don't support this elsewhere. Perhaps a parameter name on the injection point could be an escape hatch for those who want to rely on this.

@jhoeller let's make up our mind on this?

Comment From: jhoeller

In a micro-benchmark with 3000 bean definitions with distinct bean classes and 3000 injection points requesting each of those beans, this new shortcut for parameter names matching the target bean names led to ~10x better autowiring performance. These assumptions will never consistently apply to real-life scenarios, but even for a fraction of the beans involved, the performance benefit should be noticeable.

Common parameter names and bean names are expected to match the convention already. Application setups can be explicitly designed for better autowiring performance through choosing consistent parameter names that match the target bean names, or alternatively through assigning aliases to the target beans that match the common parameter names declared in the application.