Languages
[Edit]
EN

Spring Boot 2 - print all registered bean names

6 points
Created by:
Maggotta
319

In this short article, we would like to show how to print all registered beans in Spring Boot Application.

Quick solution:

public static void main(String[] args) throws Exception {

    ConfigurableApplicationContext context = SpringApplication.run(SpringBootWebApplication.class, args);

    for (String name : context.getBeanDefinitionNames()) {
        System.out.println(name);
    }
}

 

Practical example

package com.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.ConfigurableApplicationContext;

@SpringBootApplication
public class SpringBootWebApplication extends SpringBootServletInitializer {

	@Override
	protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
		return application.sources(SpringBootWebApplication.class);
	}

	public static void main(String[] args) throws Exception {

		ConfigurableApplicationContext context = SpringApplication.run(SpringBootWebApplication.class, args);

		for (String name : context.getBeanDefinitionNames()) {
			System.out.println(name);
        }
	}
}

 

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join