In this tutorial, we will show how to find out all the Beans that are loaded by a Spring Boot Application class and their class type information.
Using ApplicationContext to retrieve all loaded Beans
The ApplicationContext is the main interface of a Spring application when it is required to retrieve configuration information to the application. It is read-only at run time, but can be reloaded if necessary and supported by the application. In order to do that, the following methods can be used:
1) ApplicationContext.getBeanDefinitionNames() to find the name of all loaded beans
2) ApplicationContext.getBean(beanName) to get bean including its runtime type information.
Here is an expanded version of our JPA basic application (See Using SpringBoot with JPA tutorial ) which uses the ApplicationContext to print out the Bean information:
package com.example.samplewebapp;
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 org.springframework.context.annotation.Bean;
import java.util.Arrays;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@SpringBootApplication public class DemoApplication {
private static final Logger log = LoggerFactory.getLogger(DemoApplication.class);
@Autowired private ApplicationContext appContext;
public void dumpBeans() {
String[] beans = appContext.getBeanDefinitionNames();
Arrays.sort(beans);
for (String bean: beans) {
System.out.println(bean + " of Type :: " + appContext.getBean(bean).getClass());
}
}
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@Bean public CommandLineRunner demo(PersonRepository repository) {
return (args) -> { // save a couple of persons
repository.save(new Person("Jack", "Smith"));repository.save(new Person("Joe", "Black"));repository.save(new Person("Martin", "Bauer"));
// fetch all persons
log.info("Persons found with findAll():");log.info("-------------------------------");
for (Person person: repository.findAll()) {
log.info(person.toString());
}
log.info("");
// fetch an individual person by ID
repository.findById(1 L).ifPresent(person -> {
log.info("Person found with findById(1L):");log.info("--------------------------------");log.info(person.toString());log.info("");
});
// fetch persons by last name
log.info("Person found with findBySurname('Black'):");log.info("--------------------------------------------");repository.findBySurname("Black").forEach(smith -> {
log.info(smith.toString());
});log.info("");dumpBeans();
};
}
}
Running above application will print bean names and type information in console like below:
|
applicationTaskExecutor of Type :: class org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor
basicErrorController of Type :: class org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController beanNameHandlerMapping of Type :: class org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping beanNameViewResolver of Type :: class org.springframework.web.servlet.view.BeanNameViewResolver characterEncodingFilter of Type :: class org.springframework.boot.web.servlet.filter.OrderedCharacterEncodingFilter conventionErrorViewResolver of Type :: class org.springframework.boot.autoconfigure.web.servlet.error.DefaultErrorViewResolver dataSource of Type :: class com.zaxxer.hikari.HikariDataSource dataSourceInitializedPublisher of Type :: class org.springframework.boot.autoconfigure.orm.jpa.DataSourceInitializedPublisher dataSourceInitializerPostProcessor of Type :: class org.springframework.boot.autoconfigure.jdbc.DataSourceInitializerPostProcessor defaultServletHandlerMapping of Type :: class org.springframework.beans.factory.support.NullBean defaultTemplateResolver of Type :: class org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver defaultValidator of Type :: class org.springframework.validation.beanvalidation.LocalValidatorFactoryBean defaultViewResolver of Type :: class org.springframework.web.servlet.view.InternalResourceViewResolver ... |