前言

Spring Boot启动的时候需要加载许多Bean实现最小化配置,本文将尝试找出Spring启动后加载的所有Bean信息;

通过ApplicationContext 去获取所有的Bean

通过CommandLineRunner接口,可以实现在Spring Boot完全启动后执行一些代码逻辑,本文将执行的逻辑是打印所有Bean的信息;

  1. 通过 ApplicationContext.getBeanDefinitionNames() 方法获取所有Bean的名称;
  2. 通过 ApplicationContext.getBean(beanName) 获取Bean的详细信息;

具体代码实现如下:

package com.howtodoinjava.app.controller;

import java.util.Arrays;

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.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.context.ApplicationContext; @SpringBootApplication
public class SpringBootWebApplication extends SpringBootServletInitializer implements CommandLineRunner { @Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(SpringBootWebApplication.class);
} public static void main(String[] args) throws Exception {
SpringApplication.run(SpringBootWebApplication.class, args);
} @Autowired
private ApplicationContext appContext; @Override
public void run(String... args) throws Exception
{
String[] beans = appContext.getBeanDefinitionNames();
Arrays.sort(beans);
for (String bean : beans)
{
System.out.println(bean + " of Type :: " + appContext.getBean(bean).getClass());
}
}
}

运行以上程序,控制台将打印如下信息:

2017-03-06 13:22:50 - Tomcat started on port(s): 8080 (http)

basicErrorController of Type :: class org.springframework.boot.autoconfigure.web.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.filter.OrderedCharacterEncodingFilter
conventionErrorViewResolver of Type :: class org.springframework.boot.autoconfigure.web.DefaultErrorViewResolver
defaultServletHandlerMapping of Type :: class org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport$EmptyHandlerMapping
defaultViewResolver of Type :: class org.springframework.web.servlet.view.InternalResourceViewResolver
dispatcherServlet of Type :: class org.springframework.web.servlet.DispatcherServlet
dispatcherServletRegistration of Type :: class org.springframework.boot.web.servlet.ServletRegistrationBean
duplicateServerPropertiesDetector of Type :: class org.springframework.boot.autoconfigure.web.ServerPropertiesAutoConfiguration$DuplicateServerPropertiesDetector
embeddedServletContainerCustomizerBeanPostProcessor of Type :: class org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizerBeanPostProcessor
error of Type :: class org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration$SpelView
errorAttributes of Type :: class org.springframework.boot.autoconfigure.web.DefaultErrorAttributes
...
...
...

Spring Boot - 获取所有的Bean信息的更多相关文章

  1. Spring Boot动态注入删除bean

    Spring Boot动态注入删除bean 概述 因为如果采用配置文件或者注解,我们要加入对象的话,还要重启服务,如果我们想要避免这一情况就得采用动态处理bean,包括:动态注入,动态删除. 动态注入 ...

  2. Spring Boot获取前端页面参数的几种方式总结

    Spring Boot的一个好处就是通过注解可以轻松获取前端页面的参数,之后可以将参数经过一系列处理传送到后台数据库. 获得的方式有很多种,这里稍微总结一下,大致分为以下几种: 1.指定前端url请求 ...

  3. Spring Boot 获取 java resources 下文件

    Spring Boot 获取 java resources 下文件 Spring Boot 获取 resources 目录下的目录(例:获取 resources 目录下的 template 目录): ...

  4. Spring Spring boot 获取IOC中的bean,ApplicationContext

    https://blog.csdn.net/weixin_38361347/article/details/89304414 https://www.jianshu.com/p/9ea13b00b1d ...

  5. Spring Boot 获取Bean对象实体

    一.实现 ApplicationContextAware 接口 package com.zxguan; import org.springframework.beans.BeansException; ...

  6. Spring Boot 获取yaml配置文件信息

    Spring boot 项目启动过程中: org.springframework.boot.SpringApplication#prepareEnvironment 当程序步入listeners.en ...

  7. Spring Boot普通类调用bean

    1 在Spring Boot可以扫描的包下 假设我们编写的工具类为SpringUtil. 如果我们编写的SpringUtil在Spring Boot可以扫描的包下或者使用@ComponentScan引 ...

  8. 17、Spring Boot普通类调用bean【从零开始学Spring Boot】

    转载:http://blog.csdn.net/linxingliang/article/details/52013017 我们知道如果我们要在一个类使用spring提供的bean对象,我们需要把这个 ...

  9. (17)Spring Boot普通类调用bean【从零开始学Spring Boot】

    我们知道如果我们要在一个类使用spring提供的bean对象,我们需要把这个类注入到spring容器中,交给spring容器进行管理,但是在实际当中,我们往往会碰到在一个普通的Java类中,想直接使用 ...

随机推荐

  1. ext__给grid Panel设置绑定事件

    使用面板来展示详情信息 1.创建一个面板 (双击添加) 2.给该面板设置itemid的值为:detailPanel 3.给面板设置模板 4.添加下面的内容 id:{id}</br> nam ...

  2. 26.HashCode

      在前面三篇博文中讲解了(HashMap.HashSet.HashTable),在其中不断地讲解他们的put和get方法,在这两个方法中计算key的hashCode应该是最重要也是最精华的部分,所以 ...

  3. Linux 搜某个文件里关键字的上下500行到执行文件里

    Linux 搜某个文件里关键字的上下500行到执行文件里grep '300000111110' -C 500 ./saastom7061_APP3/logs/sass.log >/app/saa ...

  4. java基础0615

    1. 1)2)   1)输出:Base  2)编译成功,但没有输出. 2. 编译成功,但没有输出. 3. 只有12行的话,不会新建文件.需要create~~ 4. public static void ...

  5. django2+python3+uwsgi+centos7部署网站

    Centos7中安装Python虚拟环境 2018年08月27日 00:09:36 kaichenkai 阅读数:984 1.为什么要搭建虚拟环境? 问题:如果在一台电脑上, 想开发多个不同的项目, ...

  6. [转] Vmware vs Virtualbox vs KVM vs XEN: virtual machines performance comparison

    http://www.ilsistemista.net/index.php/virtualization/1-virtual-machines-performance-comparison.html? ...

  7. 弹性盒子模型属性之flex-grow

    在学习弹性盒子模型的时候,有几个属性常常让同学们感觉头痛, 不知到最后得到的效果数值到底是怎样计算得来的,那么不要慌,稳住,我们能赢 !!!今天就让我们先来看看flex-grow这个属性 flex-g ...

  8. Android MediaPlayer SeekTo 在 8.0 版本上优化说明

    android使用 mediaPlayer 播放video视频过程中, 当用户退出当前播放,再从后台恢复播放时,需要跳转到之前退出的时间点继续播放.使用的方法基本都是 SeekTo 之前的时间点,但是 ...

  9. FastDFS客户端与自定义文件存储系统

    <1>安装 安装提供给大家的fdfs_client-py-master.zip到虚拟环境中 pip install fdfs_client-py-master.zip pip instal ...

  10. django项目微博第三方登录

    此处咱们用到的是 social_django,所以要把此应用注册到配置文件中, INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.a ...