Spring中AutowiredAnnotationBeanPostProcessor与构造函数注入的疑问
Great question! This is a super common point of confusion with Spring's bean lifecycle, so let's unpack exactly how this works.
First, let's clear up a key misunderstanding: while AutowiredAnnotationBeanPostProcessor is indeed a BeanPostProcessor, it also implements the InstantiationAwareBeanPostProcessor interface—an extension that gives it hooks into the bean instantiation process (before the bean is fully created), not just the post-initialization phase where regular BeanPostProcessors operate.
Here's the step-by-step breakdown of how constructor injection with @Autowired works:
Step 1: Detect candidate constructors
When Spring starts creating a bean, it first asksAutowiredAnnotationBeanPostProcessorto inspect the bean class and find any constructors marked with@Autowired(or@Inject,@Value—it handles all these). This happens via thedetermineCandidateConstructorsmethod, which is part of theInstantiationAwareBeanPostProcessorcontract.Step 2: Select the right constructor
- If there's exactly one constructor annotated with
@Autowired, Spring will use this one, even if it's not a no-arg constructor. - If multiple constructors are annotated, Spring will use autowiring rules (like matching parameter types,
@Primarybeans, or@Qualifierannotations) to pick the best fit. - Bonus: Since Spring 4.3, if a bean class has only one constructor (regardless of whether it's annotated), Spring will automatically use it for constructor injection—no
@Autowiredneeded!
- If there's exactly one constructor annotated with
Step 3: Instantiate the bean with dependencies
Once the target constructor is selected, Spring resolves all the required parameters for that constructor (finding the corresponding beans in the application context). It then invokes the constructor directly with those dependencies, creating a fully initialized bean instance where the constructor-injected dependencies are already set.Step 4: Post-instantization processing
After the bean is instantiated, the regularBeanPostProcessorlifecycle kicks in. This is whenAutowiredAnnotationBeanPostProcessorhandles field and setter injections (the part most people associate with it), but the constructor injection was already completed during instantiation.
Let's use a quick code example to make this concrete:
@Component public class OrderService { private final InventoryRepository inventoryRepo; @Autowired public OrderService(InventoryRepository inventoryRepo) { this.inventoryRepo = inventoryRepo; } }
When Spring creates OrderService, it first detects the annotated constructor via determineCandidateConstructors, fetches the InventoryRepository bean from the context, passes it into the constructor, and creates the OrderService instance with the dependency already wired in. The post-processing steps (like checking for field injections) happen after this point, but the constructor work is done.
To sum it up: Constructor injection bypasses the "post-creation" BeanPostProcessor step because AutowiredAnnotationBeanPostProcessor uses its InstantiationAwareBeanPostProcessor capabilities to hook into the bean creation process early, resolving dependencies before the bean is even fully instantiated.
内容的提问来源于stack exchange,提问作者Roman Roman

