1. Overview

In spring application, generally, Beans have been created when we used @Bean, @Service, @Controller, @Configuration, and any other specific annotations and based on its scope it will be available in spring life-cycle. In this article, we will learn that we get all the loaded beans from the spring application context. We will also learn about getting bean using bean name, get all the Beans by Class type or Annotation type. ApplicationContext is one of the Bean which contains all the basic information about Spring content like application name, all the beans information, application display name ect.

2. Ways to get loaded beans in Spring / Spring boot

2.1 Get all beans

ApplicationContext.getBeanDefinitionNames() will return names of beans which is correctly loaded. getBean(String name) method using that we can get particular bean using bean name.

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
/**
 * Created by JavaDeveloperZone on 07-13-2019.
 */
@SpringBootApplication
public class SpringBootConfig implements CommandLineRunner {
    @Autowired
    private ApplicationContext appContext;
    @Override
    public void run(String... strings) {
        String[] beans = appContext.getBeanDefinitionNames();
        for(String bean:beans){
             System.out.println("Bean name: " + bean);
             Object object = appContext.getBean(bean);
             System.out.println( "Bean object:" + object);
        }
    }
    public static void main(String[] args) throws Exception {
        SpringApplication springApplication = new SpringApplication(SpringBootConfig.class);
        springApplication.run(args);
    }
}

Output

Bean name: mvcUriComponentsContributor
Bean object:org.springframework.web.method.support.CompositeUriComponentsContributor@3cb8c8ce
Bean name: httpRequestHandlerAdapter
Bean object:org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter@1fde0371
Bean name: simpleControllerHandlerAdapter
Bean object:org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter@70c0a3d5
...
...

2.2 Spring get Bean By bean name

Here is an example of getting the bean using bean name in spring boot application.

@SpringBootApplication
public class SpringBootConfig implements CommandLineRunner {
    @Autowired
    private ApplicationContext appContext;
    @Override
    public void run(String... strings) {
        Object object = appContext.getBean("HRService");
        System.out.println( "Bean object:" + object);
    }
    public static void main(String[] args) throws Exception {
        SpringApplication springApplication = new SpringApplication(SpringBootConfig.class);
        springApplication.run(args);
    }
}
@Service
class HRService{
    // code
}

2.2 Get the specific type of beans

ApplicationContext.getBeansOfType() is a method using that we can load all the bean of a specific type of beans. It will return Map<String, Object> key is bean name and Object is bean actual object:

@SpringBootApplication
public class SpringBootConfig implements CommandLineRunner {
    @Autowired
    private ApplicationContext appContext;
    @Override
    public void run(String... strings) {
        java.util.Map<String,EmployeeService> beans = appContext.getBeansOfType(EmployeeService.class);
        beans.forEach((k,v)->{
            System.out.println(k + " - "+v);
        });
    }
    public static void main(String[] args) throws Exception {
        SpringApplication springApplication = new SpringApplication(SpringBootConfig.class);
        springApplication.run(args);
    }
}
@Service
class EmployeeService{
    
}

Output:

employeeService - com.javadeveloperzone.EmployeeService@55e7a35c

2.3 Get the specific type of beans by annotation type

ApplicationContext.getBeansWithAnnotation() is a method using that we can Map of all the bean of specific type i.e Service, Controller or any other annotation using that we can create the bean.

@SpringBootApplication
public class SpringBootConfig implements CommandLineRunner {
    @Autowired
    private ApplicationContext appContext;
    @Override
    public void run(String... strings) {
        java.util.Map<String,Object> beans = appContext.getBeansWithAnnotation(Service.class);
        beans.forEach((k,v)->{
            System.out.println(k + " - "+v);
        });
    }
    public static void main(String[] args) throws Exception {
        SpringApplication springApplication = new SpringApplication(SpringBootConfig.class);
        springApplication.run(args);
    }
}
@Service
class EmployeeService{
    // code
}
@Service
class HRService{
    // code
}

Output

employeeService - com.javadeveloperzone.EmployeeService@602ae7b6
HRService - com.javadeveloperzone.HRService@10cd6753

3. Concussion

In this article, We learn about how to get a list of all the beans which are available in the spring/spring boot context at the run time. Sometimes we need to check that beans that we are expecting are loading in spring context or not or we need to check the only specific type of our custom beans in spring content.

4. References

 

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *