Backend Development 34 min read

Analysis of Spring Bean Instantiation Strategies in AbstractAutowireCapableBeanFactory

This article dissects the core bean creation logic of Spring's AbstractAutowireCapableBeanFactory, detailing how createBeanInstance delegates to obtainFromSupplier, instantiateUsingFactoryMethod, autowireConstructor, and fallback default constructors, while explaining the role of Supplier, ConstructorResolver, argument resolution, and caching mechanisms.

Selected Java Interview Questions
Selected Java Interview Questions
Selected Java Interview Questions
Analysis of Spring Bean Instantiation Strategies in AbstractAutowireCapableBeanFactory

The article begins by identifying the entry point of bean creation in Spring's AbstractAutowireCapableBeanFactory#createBeanInstance() , which sequentially attempts four strategies: obtaining an instance from a Supplier , using a named factory method, invoking a constructor with autowiring, and finally falling back to the default no‑arg constructor.

Supplier Instantiation – If a bean definition provides an instanceSupplier , the method obtainFromSupplier() is called. It temporarily sets the current bean name in a thread‑local, invokes instanceSupplier.get() to obtain the object, wraps it in a BeanWrapper , and restores the previous bean name.

Factory Method Instantiation – When a factoryMethodName is present, instantiateUsingFactoryMethod() creates a ConstructorResolver which selects the appropriate factory method, resolves its arguments (including cached values and autowired dependencies), and finally calls instantiate() to create the bean instance.

The ConstructorResolver performs sophisticated argument resolution: it parses constructor‑arg entries, handles indexed and generic arguments, resolves placeholders, performs type conversion, and supports autowiring by type. It also caches resolved constructors and arguments to speed up prototype bean creation.

Constructor Autowiring – If no supplier or factory method applies, Spring attempts to autowire a constructor via autowireConstructor() . It determines candidate constructors (including those supplied by SmartInstantiationAwareBeanPostProcessor ), sorts them by visibility and parameter count, calculates a type‑difference weight for each candidate, and selects the best match. Ambiguities result in a clear exception.

Default Constructor – When all other strategies fail, instantiateBean() uses the bean's default constructor through the SimpleInstantiationStrategy . It obtains the no‑arg constructor via reflection, makes it accessible, and creates the instance with BeanUtils.instantiateClass() , which also handles Kotlin optional parameters and primitive defaults.

Overall, the article provides a step‑by‑step walkthrough of how Spring resolves bean creation paths, manages argument caching, and ensures correct dependency injection across various instantiation mechanisms.

JavaSpringdependency injectionConstructor InjectionFactory MethodBeanFactory
Selected Java Interview Questions
Written by

Selected Java Interview Questions

A professional Java tech channel sharing common knowledge to help developers fill gaps. Follow us!

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.