With Spring Boot, a lot of beans get loaded into the ApplicationContext depending on your dependencies selection. This post shows a code snippet used to list all the beans loaded in the ApplicationContext.
Your Typical Main Class
Consider the following code snippet. You can use IntelliJ (optional) and Spring Initialzr (https://start.spring.io) to generate the initial codes and pom.xml.
1 2 3 4 5 6 7 8 9 | import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class ComTurretaDemoApplication { public static void main(String[] args) { SpringApplication.run(ComTurretaDemoApplication.class, args); } } |
Get and used ApplicationContext
Modify the original codes to these. Notice the lines 10-16.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.ApplicationContext; import java.util.Arrays; @SpringBootApplication public class ComTurretaDemoApplication { public static void main(String[] args) { ApplicationContext ctx = SpringApplication.run(ComTurretaDemoApplication.class, args); String[] springBootBeans = ctx.getBeanDefinitionNames(); Arrays.sort(springBootBeans); for (String beanName : springBootBeans) { System.out.println(beanName); } } } |