Spring Boot - Get Beans from ApplicationContext

This tutorial shows you how to obtain Spring beans from the ApplicationContext programmatically.

Spring allows objects to be registered as beans that are managed by the Spring IoC container. Getting a bean that's already registered is usually quite easy as it can be injected to the constructor of another bean. However, in some situations, you may need to get a bean programmatically, either by using its name or its class type. You can do it by utilizing the ApplicationContext.

The ApplicationContext provides some methods for getting beans. First of all, you have to get the ApplicationContext first. It's quite easy to do it inside a Spring bean class as it can be automatically injected to the constructor. You can also read our tutorial about how to get ApplicationContext for more details, including how to access it from a non Spring managed class.

Get by Name

There is a method named getBean that has one parameter whose type is String. It can be used to retrieve a bean with the specified name. The return type of the method is an Object. Therefore, you may need to cast the result. If there is no bean with the specified name, it will throw NoSuchBeanDefinitionException.

  Object getBean(String name)

Example:

  MyCounter myCounter = (MyCounter) this.context.getBean("foo");

Get by Class

There is another getBean method whose parameter type is the class type. The return type is the passed class type, so you don't need to cast the returned object. It's used to find a bean with a given type. If not found, it will throw NoSuchBeanDefinitionException. If there are several beans with the given type, it will throw NoUniqueBeanDefinitionException. Therefore, it's suitable if you are sure that there's only one bean with the given type.

  <T> T getBean(Class<T> requiredType)

Example:

  MyCounter myCounter = this.context.getBean(MyCounter.class);

Get by Name and Class

You can also use the method that has both the name and class type. If finds a bean with the matching name and class type. It may throw BeanNotOfRequiredTypeException if there is a bean with the specified name, but the type is different. If the name is not found in the registry, it will throw NoSuchBeanDefinitionException. This method can be useful if there can be more than one bean with the same type but different names. Since the return type is the same as the passed class type, you don't need to cast it.

  <T> T getBean(String name, Class<T> requiredType)

Example:

  MyCounter myCounter = this.context.getBean("foo", MyCounter.class);

Get List of Beans of a Type

It's also possible to get a list of beans with a certain type by using the getBeansOfType method. It requires you to pass the type of the beans to be returned. The return type is a Map whose key is the bean name.

  <T> Map<String, T> getBeansOfType(@Nullable Class<T> type)

Example:

  Map<String, MyCounter> map = this.context.getBeansOfType(MyCounter.class);
  Collection<MyCounter> beans = map.values();

Summary

All registered beans can be accessed from the ApplicationContext. To get a bean, use the ApplicationContext's getBean method by passing the name, class type, or both. If you want to get the list of beans with a particular type, use the getBeansOfType method, which returns a Map.

You can also read about: