Print all the Spring beans that are loaded - Spring Boot

As shown in the getting started guide of spring-boot: https://spring.io/guides/gs/spring-boot/

@SpringBootApplication
public class Application {

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

  @Bean
  public CommandLineRunner commandLineRunner(ApplicationContext ctx) {
    return args -> {

      System.out.println("Let's inspect the beans provided by Spring Boot:");

      String[] beanNames = ctx.getBeanDefinitionNames();
      Arrays.sort(beanNames);
      for (String beanName : beanNames) {
        System.out.println(beanName);
      }
    };
  }    
}

As @Velu mentioned in the comments, this will not list manually registered beans.

In case you want to do so, you can use getSingletonNames(). But be careful. This method only returns already instantiated beans. If a bean isn't already instantiated, it will not be returned by getSingletonNames().


May I suggest using Actuator? it provides several endpoints including /beans which lists all beans in the application. You say "once the server is started" so this is an option for web applications.

To set up actuator

https://spring.io/guides/gs/actuator-service/

List of endpoints in actuator

http://docs.spring.io/spring-boot/docs/current/reference/html/production-ready-endpoints.html


Well, Although, this question is already answered, I still want to provide an answer which is a Java 8 variant :)

Arrays.asList(context.getBeanDefinitionNames()).stream().sorted().forEach(System.out::println);

Lets do Java 8 !!!


Actually I would recommend to create this class aside from modifying your @SpringBootApplication.

@Component
public class ContextTeller implements CommandLineRunner {

@Autowired
ApplicationContext applicationContext;

@Override
public void run(String... args) throws Exception {
    System.out.println("-------------> just checking!");

        System.out.println(Arrays.asList(applicationContext.getBeanDefinitionNames()));

}}

This way Spring Boot will load this class and execute just after loading context. Then you just can remove the file, and everything is clear.