Spring XXXAware vs XXXProcessor pattern¶
In the Spring Framework, the XXXAware and XXXProcessor patterns represent two distinct mechanisms for interacting with the container's lifecycle and resources, specifically regarding the timing of their execution: postProcessBeforeInitialization, invokeAwareInterfaces, and postProcessAfterInitialization.^[600-developer__java__spring__spring-note.md]
Core Pattern Flow¶
The interaction between these patterns generally follows a specific sequence during the bean initialization process.^[600-developer__java__spring__spring-note.md]
- Post-Processing (Before): The container invokes
postProcessBeforeInitialization.^[600-developer__java__spring__spring-note.md] - Aware Interfaces: The container invokes
invokeAwareInterfaces.^[600-developer__java__spring__spring-note.md] - Post-Processing (After): The container invokes
postProcessAfterInitialization.^[600-developer__java__spring__spring-note.md]
XXXAware Interfaces¶
The Aware interfaces allow specific beans to be "aware" of, and subsequently obtain, underlying Spring infrastructure objects.^[600-developer__java__spring__spring-note.md] This is a callback-based approach where the bean is passively injected with dependencies.
Common Aware interfaces include:
* ApplicationContextAware: Provides access to the ApplicationContext.
* BeanFactoryAware: Provides access to the BeanFactory.
* EnvironmentAware: Provides access to the Environment.
* ServletContextAware: Provides access to the ServletContext in web applications.^[600-developer__java__spring__spring-note.md]
XXXProcessor Interfaces¶
Processor interfaces (typically BeanPostProcessor or its specialized variants like InstantiationAwareBeanPostProcessor) are used for active intervention in the bean lifecycle.^[600-developer__java__spring__spring-note.md] Unlike Aware interfaces, which simply inform a bean of its environment, processors can wrap, modify, or completely replace a bean before or after its initialization.
Typical processors related to this pattern include:
* ApplicationContextAwareProcessor: A specific internal processor responsible for invoking the callbacks defined in the Aware interfaces (e.g., checking if a bean implements ApplicationContextAware and setting the context).^[600-developer__java__spring__spring-note.md]
* ServletContextAwareProcessor: Similar to the ApplicationContextAwareProcessor, but specifically for invoking ServletContextAware callbacks within a web context.^[600-developer__java__spring__spring-note.md]
Relationship Summary¶
The key difference lies in the direction of the interaction: * XXXAware: The Bean implements the interface to receive resources (Passive/Consumer). * XXXProcessor: The Container uses the processor to inspect and modify the bean (Active/Producer).
In practice, an Aware interface is often implemented by a developer's bean, while the corresponding Processor is the internal mechanism (or a custom extension) that detects that interface and injects the required dependency.^[600-developer__java__spring__spring-note.md]
Related Concepts¶
- [[Spring Bean Lifecycle]]
- [[ApplicationContext]]
Sources¶
600-developer__java__spring__spring-note.md