我怎样才能知道作为我的 Spring Boot 应用程序的一部分加载的所有 Bean 的名称?我希望在 main 方法中有一些代码来打印服务器启动后加载的 bean 的详细信息。
如spring-boot的入门指南所示:https://spring.io/guides/gs/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 https://stackoverflow.com/questions/33348937/print-all-the-spring-beans-that-are-loaded-spring-boot/33349229#comment96531704_33349229评论中提到,这不会列出手动注册的bean。
如果您想这样做,您可以使用getSingletonNames() https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/beans/factory/config/SingletonBeanRegistry.html#getSingletonNames--。不过要小心。该方法仅返回已经实例化的bean。如果 bean 尚未实例化,它将not被返回getSingletonNames()
.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)