Scrivio Interview library
ConceptFoundations6 min read · updated 12 November 2026

What are Spring's Aware interfaces, and why are they used less in application code today?

Aware interfaces, such as BeanNameAware, ApplicationContextAware, EnvironmentAware and ResourceLoaderAware, are callback interfaces that let a bean receive objects from the container: Spring calls a setter like setApplicationContext() after injecting the bean's properties and before its initialisation callbacks. We use them less today because most of those objects can simply be injected through the constructor, and implementing the interfaces ties our class to Spring's API.

Why this question is asked

Aware interfaces show up in older code, in framework code, and in a fair number of interview questions about the bean lifecycle. Knowing them helps us read existing applications, and knowing why they are less common now shows that we understand dependency injection well enough to choose the simpler option. In this answer, we will recap what the Aware interfaces are, see exactly when Spring calls them in the lifecycle, compare them with plain constructor injection, and look at the few cases where they are still the right tool.

1A Quick Recap of the Aware Interfaces

Spring's container manages our beans, but sometimes a bean wants something from the container itself: its own bean name, the application context, the environment with its properties, or a resource loader to read files. Before annotation-driven injection was common, the way to ask for these was to implement a marker-style interface whose name ends in Aware. Each interface has one setter, and Spring calls it with the object the bean asked for.

The main ones are BeanNameAware (setBeanName gives the bean its own name), BeanClassLoaderAware, BeanFactoryAware, ApplicationContextAware (setApplicationContext), EnvironmentAware (setEnvironment), ResourceLoaderAware, ApplicationEventPublisherAware and MessageSourceAware. They all extend an empty marker interface called Aware.

Java
@Component
public class ReportExporter implements BeanNameAware, ApplicationContextAware, EnvironmentAware {
private String beanName;
private ApplicationContext context;
private Environment env;
@Override public void setBeanName(String name) { this.beanName = name; } // "reportExporter"
@Override public void setApplicationContext(ApplicationContext ctx) { this.context = ctx; }
@Override public void setEnvironment(Environment env) { this.env = env; }
public String outputDir() {
return env.getProperty("reports.dir", "/tmp/reports"); // "/var/reports" if set
}
}

Notice that the fields cannot be final, because they are filled by setters after the object is constructed. That small detail is part of why the style has fallen out of favour.

2When Spring Calls Them in the Lifecycle

The order matters, because it tells us what a bean can rely on in each callback. Spring first creates the instance by calling the constructor, then injects the properties and fields (the @Autowired fields and setters). Right after that, it calls the Aware methods. The bean factory itself handles BeanNameAware, BeanClassLoaderAware and BeanFactoryAware. The ones that come from the application context, such as EnvironmentAware, ResourceLoaderAware, ApplicationEventPublisherAware and ApplicationContextAware, are called by a built-in BeanPostProcessor named ApplicationContextAwareProcessor, at the very start of the before-initialisation phase.

Only after the Aware callbacks does Spring run the initialisation callbacks: @PostConstruct methods, then afterPropertiesSet() for InitializingBean, then a custom init method. So inside @PostConstruct we can safely use the context or environment we received through Aware. Inside the constructor we cannot, because the setters have not been called yet.

Step throughWhere Aware callbacks sit in bean creation
  1. Call the constructorconstructor arguments injected
  2. Inject fields and setters@Autowired properties
  3. BeanNameAware, BeanClassLoaderAware, BeanFactoryAwarecalled by the bean factory
  4. EnvironmentAware ... ApplicationContextAwarecalled by ApplicationContextAwareProcessor
  5. @PostConstructthen afterPropertiesSet, then init method
  6. Bean is readyafter-initialisation post-processors, proxies

6 steps in order. Play them, or step through them one at a time.

Aware setters run after properties are set and before any initialisation callback.

3The Modern Alternative: Inject the Object Directly

Most objects the Aware interfaces provide are available for normal injection. The ApplicationContext, the BeanFactory, the ResourceLoader and the ApplicationEventPublisher are registered as resolvable dependencies, and the Environment is a bean in its own right. So the same class can ask for them in its constructor like any other dependency.

Java
@Component
public class ReportExporter {
private final Environment env;
private final ApplicationEventPublisher events;
public ReportExporter(Environment env, ApplicationEventPublisher events) {
this.env = env; // same object EnvironmentAware would have passed
this.events = events; // the application context, seen through a narrow interface
}
public String outputDir() {
return env.getProperty("reports.dir", "/tmp/reports"); // "/var/reports" if set
}
}

This version has final fields, its dependencies are visible in the constructor, and a unit test can create it with new ReportExporter(mockEnv, mockPublisher) without any Spring at all. It also lets us ask for the narrowest type we need, such as ApplicationEventPublisher instead of the whole ApplicationContext, which keeps the class from reaching for anything it likes.

Even better, for configuration values we often do not need the Environment at all: a @ConfigurationProperties class or a @Value constructor parameter gives the bean exactly the setting it needs.

4Why Implementing Aware Couples the Class to Spring

Implementing ApplicationContextAware means the class imports Spring types in its signature, cannot be constructed fully without Spring calling the setter, and usually ends up holding the whole context. Once a class holds the context, it is tempting to call getBean() from inside it, which turns the class into a hidden service locator whose real dependencies are invisible. Constructor injection makes the dependencies explicit and lets the container detect missing beans or cycles at startup.

There are still good uses. BeanNameAware is the only simple way for a bean to learn its own name, which is useful for logging or metrics in components that are registered several times. Framework and library code, such as a custom FactoryBean or a component that must work in plain bean factories, often uses BeanFactoryAware or BeanClassLoaderAware. In ordinary application services, though, constructor injection is the better default.

Aware interface or injection?
Aware interfaceGives the beanModern alternative
BeanNameAwareits own bean namenone simple; still useful
ApplicationContextAwarethe application contextinject ApplicationContext, or better a narrow type
EnvironmentAwarethe Environmentinject Environment or @ConfigurationProperties
ResourceLoaderAwarea ResourceLoaderinject ResourceLoader
ApplicationEventPublisherAwarean event publisherinject ApplicationEventPublisher
BeanFactoryAwarethe bean factoryinject ObjectProvider for lazy lookups
Only the bean name has no direct injection equivalent.

5Follow-up questions

Is the order between @PostConstruct and Aware callbacks guaranteed?

Yes. The Aware methods handled by the bean factory run before any BeanPostProcessor, and ApplicationContextAwareProcessor is registered by the context before the annotation post-processors that handle @PostConstruct, so the context-related Aware setters also run first. In practice we can rely on having the Aware objects inside @PostConstruct, afterPropertiesSet and init methods.

Can a bean get its own name without BeanNameAware?

Not through a simple injection. Spring does not offer the bean name as an injectable value. If we only need a stable identifier, we can pass it as a property or derive it from a configuration value instead. For library components that are registered many times under different names, BeanNameAware remains a reasonable and small dependency on Spring.

Does implementing ApplicationContextAware cause any problems with proxies?

Not directly, because the setter is called on the target object before any proxy is created around it. The real risk is design: a bean that holds the context tends to call getBean() at runtime, which hides dependencies and can pull in beans before they are ready. Injecting the specific dependency, or an ObjectProvider when a lazy lookup is needed, avoids that.

6Conclusion

In this answer, we saw that Aware interfaces are callbacks through which Spring hands a bean container objects such as its name, the application context, the environment or a resource loader. Spring calls them after properties are set and before @PostConstruct and other initialisation callbacks. Today most of these objects can be injected through the constructor, which keeps fields final, makes dependencies visible and avoids coupling the class to Spring, so Aware interfaces are mainly left to framework code and to the rare case of a bean that needs its own name.

What to say in the interviewAbout 70 seconds aloud

Open with

Aware interfaces are callback interfaces like BeanNameAware, ApplicationContextAware, EnvironmentAware and ResourceLoaderAware. If a bean implements one, Spring calls its setter to hand it a container object: the bean's name, the context, the environment and so on.

Then, if they want more

In the lifecycle, Spring creates the instance, injects properties, then calls the Aware setters, the bean-factory ones directly and the context ones through ApplicationContextAwareProcessor, and only then runs @PostConstruct, afterPropertiesSet and init methods. So the objects are available during initialisation but not in the constructor.

We use them less in application code now because almost all of these objects can be injected directly: ApplicationContext, ResourceLoader, ApplicationEventPublisher and Environment can all be constructor parameters. That gives final fields, visible dependencies and easy unit tests, and it lets me ask for a narrow type like ApplicationEventPublisher instead of the whole context.

Implementing Aware ties the class to Spring and often leads to getBean calls, which is a hidden service locator. I would still use BeanNameAware when a component really needs its own name, and Aware interfaces are fine in framework or library code.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "What are Spring's Aware interfaces, and why are they used less in application code today?": https://scrivio-interview-library.netlify.app/spring-boot/bean-lifecycle/967f1639f3/