Spring Boot
Application Context
Bean Management
Java
Dependency Injection

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:

java
1@Service
2public class MyService {
3
4    private final MyRepository myRepository;
5
6    @Autowired
7    public MyService(MyRepository myRepository) {
8        this.myRepository = myRepository;
9    }
10
11    // Other methods
12}

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:

java
1import org.springframework.beans.factory.annotation.Autowired;
2import org.springframework.context.ApplicationContext;
3import org.springframework.stereotype.Component;
4
5@Component
6public class BeanRetriever {
7
8    @Autowired
9    private ApplicationContext applicationContext;
10
11    public Object retrieveBean(String beanName) {
12        return applicationContext.getBean(beanName);
13    }
14
15    public <T> T retrieveBean(Class<T> beanClass) {
16        return applicationContext.getBean(beanClass);
17    }
18}

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:

java
1@Component
2public class StaticContextHolder implements ApplicationContextAware {
3
4    private static ApplicationContext context;
5
6    @Override
7    public void setApplicationContext(ApplicationContext applicationContext) {
8        context = applicationContext;
9    }
10
11    public static <T> T getBean(Class<T> requiredType) {
12        return context.getBean(requiredType);
13    }
14}

Then retrieve beans without injecting the context:

java
MyRepository repo = StaticContextHolder.getBean(MyRepository.class);

Reach for dependency injection first, since it gives cleaner, more testable code.

4. Check that a bean exists first

java
if (applicationContext.containsBean("myBean")) {
    MyBean myBean = (MyBean) applicationContext.getBean("myBean");
}

This helps when handling multiple configurations or optional dependencies.

Guidelines for bean lookup

  1. Prefer dependency injection (@Autowired) for cleaner, more maintainable code.
  2. Keep getBean() calls rare. Frequent direct calls to ApplicationContext.getBean() can point to a design problem.
  3. In tests, use @SpringBootTest to have the application context managed for you.
  4. Use Spring profiles to manage environment-specific beans and configuration.
AspectDetails
Injection Method@Autowired for fields, setters, or constructors.
Dynamic AccessApplicationContext.getBean() for flexible retrieval.
Static AccessImplement ApplicationContextAware for static contexts.
Condition RetrievalUse containsBean() to check bean existence.
Coding PracticeFavor dependency injection for cleaner code.
Environment ProfilesUtilize profiles to manage configurations.

Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Object-Oriented Design practice on Codemia

Turn requirements into classes, and defend the design, on the problems that come up in OOD rounds.

Practice OOD