Spring ApplicationContext Access from a Bean
Fast recall
Quick revision
Core answer
Understanding of the container itself and practical knowledge of accessing it from within managed beans
Using ApplicationContext.getBean() to manually look up dependencies instead of leveraging proper dependency injection
Explain that directly injecting ApplicationContext implements the service locator anti-pattern and should only be used for framework-level operations like dynamic bean lookup or event publishing
Interview-ready guide
Interview answer
A plugin registry that must resolve beans dynamically may justify a typed context lookup. A PaymentService that simply needs PaymentGateway should inject PaymentGateway directly; calling context.getBean(PaymentGateway.class) hides the requirement and makes plain tests harder. If it only publishes events, inject ApplicationEventPublisher rather than the whole context.
Spring ApplicationContext Access from a Bean
Direct Answer
ApplicationContext is Spring's application-level container API for beans, events, resources, messages, environment, and lifecycle. A managed bean can receive it through constructor injection, @Autowired, or ApplicationContextAware. Direct context lookup is useful for framework/infrastructure cases, but ordinary business code should usually inject the specific dependency or ObjectProvider instead of turning the context into a service locator.
Code Example
// Method 1: Direct injection (simpler, preferred for one-time use)
@Service
public class ReportService {
private final ApplicationContext context;
public ReportService(ApplicationContext context) {
this.context = context; // Spring injects the container itself
}
public void generateDynamicReport(String reportType) {
// Dynamic bean lookup based on runtime input
var generator = context.getBean(reportType + "Generator", ReportGenerator.class);
generator.generate();
}
}
// Method 2: ApplicationContextAware interface (for framework code)
@Component
public class BeanRegistryService implements ApplicationContextAware {
private ApplicationContext context;
@Override
public void setApplicationContext(ApplicationContext ctx) {
this.context = ctx; // Spring calls this during bean initialization
}
public List<String> listAllBeans() {
return List.of(context.getBeanDefinitionNames());
}
}
Common Mistakes & Pitfalls
- Service Locator Anti-Pattern: Freshers inject ApplicationContext and call getBean() everywhere to fetch dependencies instead of declaring them as constructor parameters. This hides dependencies, makes testing harder (can't mock easily), and defeats the purpose of DI. Spring should inject what you need — don't go hunting for beans yourself.
- Overusing for Static Access: Storing ApplicationContext in a static field to access beans from non-Spring classes (like utility methods). This creates a global singleton, breaks testability, and couples your code to Spring. If a class needs Spring beans, make it a Spring bean too.
- Ignoring Constructor Injection: Using ApplicationContext to fetch optional dependencies instead of using Optional<T> or @Autowired(required=false) on a constructor parameter, which is cleaner and more explicit.
Check your understanding
Answer these without looking back at the article.
- What is the Service Locator anti-pattern?
- What is ApplicationContextAware?
- How is ApplicationContext different from BeanFactory?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.