Sub topic 7
Aware interfaces
This chapter assumes you know the construction and initialization steps from the previous chapters. It also assumes you can write a @Component and run a Spring application.
What an Aware interface does
A bean normally knows nothing about the container that created it. Sometimes a bean needs to know its own name, or it needs the ApplicationContext. An Aware interface is a callback interface with one setter. If your bean implements it, the container calls that setter and passes the object you asked for.
Common Aware interfaces:
| Interface | Setter | What you receive |
|---|---|---|
| `BeanNameAware` | `setBeanName` | The bean's name in the container |
| `BeanFactoryAware` | `setBeanFactory` | The owning `BeanFactory` |
| `ApplicationContextAware` | `setApplicationContext` | The `ApplicationContext` |
| `EnvironmentAware` | `setEnvironment` | The `Environment` (properties and profiles) |
| `ResourceLoaderAware` | `setResourceLoader` | A loader for files and classpath resources |
| `ApplicationEventPublisherAware` | `setApplicationEventPublisher` | A publisher for application events |
A working example
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
import jakarta.annotation.PostConstruct;
@Component
public class AwareBean implements BeanNameAware, ApplicationContextAware {
public AwareBean() {
System.out.println("constructor");
}
@Override
public void setBeanName(String name) {
System.out.println("setBeanName: " + name);
}
@Override
public void setApplicationContext(ApplicationContext ctx) {
System.out.println("setApplicationContext: " + ctx.getDisplayName());
}
@PostConstruct
void postConstruct() {
System.out.println("@PostConstruct");
}
}Start a context that scans this class. The output is:
constructor
setBeanName: awareBean
setApplicationContext: org.springframework.context.annotation.AnnotationConfigApplicationContext@...
@PostConstructThe default bean name is the class name with a lowercase first letter. The exact display name depends on how you start the context.
Where Aware callbacks run
The Aware callbacks run after the constructor and after dependency injection. They run before any initialization callback. Two different mechanisms call them:
- The bean factory itself calls
BeanNameAware,BeanClassLoaderAwareandBeanFactoryAware. - A built-in
BeanPostProcessor, theApplicationContextAwareProcessor, calls the context-related ones. These areEnvironmentAware,ResourceLoaderAware,ApplicationEventPublisherAwareandApplicationContextAware. It does this in its before-initialization step. That step runs before the@PostConstructprocessing.
When to avoid them
Aware interfaces tie your class to Spring. A class that implements ApplicationContextAware and calls getBean hides its dependencies. This is the service locator pattern. It makes the class harder to test, because you must supply a context.
Prefer these alternatives:
- Declare the real dependency as a constructor parameter.
- Inject
ApplicationContext,EnvironmentorApplicationEventPublisherdirectly through the constructor. Spring can inject these types without an Aware interface. - Use
ObjectProvider<T>when you need a bean lazily or repeatedly.
Aware interfaces still make sense in framework or infrastructure code. A BeanPostProcessor that needs the context is one example. Library classes that must work without injection support are another.
Exercise
A bean implements EnvironmentAware and has a @PostConstruct method. Which runs first, and why?
Show answer
setEnvironment runs first. The ApplicationContextAwareProcessor calls it in its before-initialization step. That step comes before the @PostConstruct processing, so @PostConstruct runs afterward.