Ideas:
-
Get ApplicationContext by getBeanDefinitionNames() so the question becomes, how do I get ApplicationContext
- Inject ApplicationContext with a construct
@RequestMapping("/spring") @RestController public class GetAllBeansInSpringController { private ApplicationContext applicationContext; @Autowired public void setApplicationContext(ApplicationContext applicationContext) { this.applicationContext = applicationContext; } @GetMapping("/beans") public String[] getAllBeansInSpring() { returnapplicationContext.getBeanDefinitionNames(); }}Copy the code
- Gets the return value of the startup class
@SpringBootApplication public class MykafkaApplication { private static ApplicationContext context; public static void main(String[] args) { context = SpringApplication.run(MykafkaApplication.class, args); display(); } public static void display(a) { // System.err.println(context.getBeanDefinitionCount());Arrays.stream(context.getBeanDefinitionNames()).forEach(System.err::println); }}Copy the code
- Some other ways (to be added)
- Inject ApplicationContext with a construct
-
Some external tools – Using Actuator(to be added)