Get a bean from the ApplicationContext in Spring Boot (getBean)
Codemia Knowledge Hub ·
Inject the ApplicationContext and call getBean(beanName) or getBean(MyClass.class). For code outside Spring's management, such as static methods, implement ApplicationContextAware and keep a static reference. In most cases, though, plain dependency injection is the better choice.
What the application context does
The application context is the central registry for your application's components (beans). It starts when the Spring Boot application starts, discovers and manages beans automatically, injects dependencies based on metadata, and lets you retrieve beans manually when needed.
Ways to get a bean
1. Inject it with @Autowired
The most common way to get a bean is to have Spring inject it. @Autowired supports constructor, setter, and field injection:
Spring Boot injects MyRepository into MyService automatically.
2. Call ApplicationContext.getBean()
When you need to look up beans dynamically, or injection is impractical, use getBean() by name or by type:
3. Hold the context statically with ApplicationContextAware
For code outside the Spring context, such as static methods, store the context when Spring hands it over. The holder must itself be a Spring bean, hence @Component, or Spring never calls setApplicationContext:
Then retrieve beans without injecting the context:
Reach for dependency injection first, since it gives cleaner, more testable code.
4. Check that a bean exists first
This helps when handling multiple configurations or optional dependencies.
Guidelines for bean lookup
- Prefer dependency injection (
@Autowired) for cleaner, more maintainable code. - Keep
getBean()calls rare. Frequent direct calls toApplicationContext.getBean()can point to a design problem. - In tests, use
@SpringBootTestto have the application context managed for you. - Use Spring profiles to manage environment-specific beans and configuration.
| Aspect | Details |
| Injection Method | @Autowired for fields, setters, or constructors. |
| Dynamic Access | ApplicationContext.getBean() for flexible retrieval. |
| Static Access | Implement ApplicationContextAware for static contexts. |
| Condition Retrieval | Use containsBean() to check bean existence. |
| Coding Practice | Favor dependency injection for cleaner code. |
| Environment Profiles | Utilize profiles to manage configurations. |
Related reading
- How to get the parents of a Python class?
- How to inject different services at runtime based on a property with Spring without XML
- How to invoke the super constructor in Python?
- How to make a Generic Type Cast function
- How to get bearer token from header of a request in java spring boot?
- How to Get Current Pod in Kubernetes Java Application
- How to make a Java class that implements one interface with two generic types?
- How to make a Java Generic method static?
System design interview problems on this topic
- Design an API Rate Limiter: solution and walkthrough
- Design a Distributed Messaging System: solution and walkthrough
- Design an Online Payment Service: solution and walkthrough
- Design a Key Value Store: solution and walkthrough

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseObject-Oriented Design practice on Codemia
Turn requirements into classes, and defend the design, on the problems that come up in OOD rounds.