How to display all beans loaded by Spring Boot

In Spring Boot, you can use appContext.getBeanDefinitionNames() to get all the beans loaded by the Spring container.

1. CommandLineRunner as Interface

Application.java

package com.mkyong;

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;

import java.util.Arrays;

@SpringBootApplication
public class Application implements CommandLineRunner {

    @Autowired
    private ApplicationContext appContext;

    public static void main(String[] args) throws Exception {
        SpringApplication.run(Application.class, args);
    }

    @Override
    public void run(String... args) throws Exception {

        String[] beans = appContext.getBeanDefinitionNames();
        Arrays.sort(beans);
        for (String bean : beans) {
            System.out.println(bean);
        }

    }

}

Output

Console.java

application
customerRepository
customerRepositoryImpl
dataSource
dataSourceInitializedPublisher
dataSourceInitializer
dataSourceInitializerPostProcessor
emBeanDefinitionRegistrarPostProcessor
entityManagerFactory
entityManagerFactoryBuilder
hikariPoolDataSourceMetadataProvider
jdbcTemplate
jpaContext
//...

2. CommandLineRunner as Bean

Just different ways to print the loaded beans.

Application.java

package com.mkyong;

import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;

import java.util.Arrays;

@SpringBootApplication
public class Application {

    public static void main(String[] args) throws Exception {
        SpringApplication.run(Application.class, args);
    }

    @Bean
    public CommandLineRunner run(ApplicationContext appContext) {
        return args -> {

            String[] beans = appContext.getBeanDefinitionNames();
            Arrays.stream(beans).sorted().forEach(System.out::println);

        };
    }

}

References

  1. CommandLineRunner JavaDoc
  2. Spring Boot non-web application example

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

avatar
3 Comment threads
3 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
4 Comment authors
Divye KardeRajasekharMohammad IrfanBahadar Ali Recent comment authors
newest oldest most voted
Bahadar Ali
Guest
Bahadar Ali

How to display only user beans (beans that are defined by developer in the code)?

Mohammad Irfan
Guest
Mohammad Irfan

Hi Ali,

You can use Actuator with HAL BROWSER to see all the beans(beans that are defined by developer in the code) and all other AutoConfigure beans also.

Bahadar Ali
Guest
Bahadar Ali

Thanks Irfan,
Yes Actuator list all the beans. But I get very last list of beans which is mix-up of both user-defined and auto-configured beans. I want to display a list of only user defined beans.

Rajasekhar
Guest
Rajasekhar

thanks for the help

Divye Karde
Guest
Divye Karde

Hi, I’m new to spring and Spring boot. I’ve been looking to find the explanation on how the commandLineRunner bean is getting called. Could you please point me to any useful resource? Thanks in advance.

Divye Karde
Guest
Divye Karde

My understanding is that as long as there’s a commandLineRunner in the spring context, it’ll get executed. But I don’t know if that’s correct. I’m also interested to know the running or initialisation order of the things in spring.