Spring Aware Interfaces¶
In the Spring Framework, Aware interfaces serve as specific callback mechanisms that allow beans to be "aware" of the Spring container or specific resources managed by it^[600-developer__java__spring__spring-note.md]. When a bean implements an Aware interface, it provides a way for the bean to look up or receive a reference to the corresponding infrastructure object from the ApplicationContext^[600-developer__java__spring__spring-note.md].
Processing Mechanism¶
The invocation of these interfaces is handled internally by specific processor beans, typically following the naming convention XXXAwareProcessor^[600-developer__java__spring__spring-note.md]. This logic generally occurs during the bean initialization lifecycle, between the postProcessBeforeInitialization and postProcessAfterInitialization phases^[600-developer__java__spring__spring-note.md].
For example, ApplicationContextAware is processed by ApplicationContextAwareProcessor, and ServletContextAware is processed by ServletContextAwareProcessor^[600-developer__java__spring__spring-note.md].
Common Interfaces¶
Spring provides a variety of Aware interfaces to access different parts of the container environment^[600-developer__java__spring__spring-note.md].
Core Container Interfaces¶
- ApplicationContextAware: Used to inject the
ApplicationContextitself^[600-developer__java__spring__spring-note.md]. - BeanFactoryAware: Provides access to the
BeanFactory^[600-developer__java__spring__spring-note.md]. - BeanNameAware: Allows the bean to inspect its own bean name in the container^[600-developer__java__spring__spring-note.md].
- BeanClassLoaderAware: Provides access to the ClassLoader used to load the bean classes^[600-developer__java__spring__spring-note.md].
Resource and Environment Interfaces¶
- ResourceLoaderAware: Provides access to the
ResourceLoaderfor loading resources (like files)^[600-developer__java__spring__spring-note.md]. - EnvironmentAware: Injects the
Environmentabstraction (properties, profiles)^[600-developer__java__spring__spring-note.md]. - EmbeddedValueResolverAware: Provides access to the resolver for strings (e.g.,
${...}placeholders)^[600-developer__java__spring__spring-note.md]. - MessageSourceAware: Provides access to the
MessageSourcefor internationalization (i18n)^[600-developer__java__spring__spring-note.md].
Web and Application Interfaces¶
- ApplicationEventPublisherAware: Allows the bean to publish application events^[600-developer__java__spring__spring-note.md].
- ServletContextAware: Provides access to the
ServletContextin a web application^[600-developer__java__spring__spring-note.md]. - ServletConfigAware: Provides access to the
ServletConfig^[600-developer__java__spring__spring-note.md].
Other Interfaces¶
- LoadTimeWeaverAware: Provides access to the
LoadTimeWeaver. - BootstrapContextAware: Provides access to the bootstrap context (often used in JCA or enterprise scenarios).
- NotificationPublisherAware: Used for publishing JMX notifications.
Related Concepts¶
- [[BeanPostProcessor]]
- [[ApplicationContext]]
- [[Spring Events]]