通过CommandLineRunner
接口,可以实现在Spring Boot完全启动后执行一些代码逻辑,本文将执行的逻辑是打印所有Bean的信息;
1) 通过 ApplicationContext.getBeanDefinitionNames()
方法获取所有Bean的名称;
2) 通过 ApplicationContext.getBean(beanName)
获取Bean的详细信息;
public class BootdemoApplication implements CommandLineRunner { public static void main(String[] args) { SpringApplication.run(BootdemoApplication.class, args); /*SpringApplication application = new SpringApplication(BootdemoApplication.class); //关闭banner条显示 application.setBannerMode(Banner.Mode.OFF); application.run(args);*/ } @Autowired private ApplicationContext applicationContext; @Override public void run(String... args) throws Exception { String[] beans = applicationContext.getBeanDefinitionNames(); Arrays.sort(beans); for (String bean : beans) { System.out.println("bean id:"+bean+",bean类型:" + applicationContext.getBean(bean).getClass()); } } }
0条评论
点击登录参与评论