Spring Boot - 获取所有的Bean信息
阅读目录
前言
通过ApplicationContext 去获取所有的Bean
前言
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
...
...
... @Author 风一样的码农
@HomePageUrl http://www.cnblogs.com/chenpi/
@Copyright 转载请注明出处,谢谢~

转载-通过ApplicationContext 去获取所有的Bean的更多相关文章

  1. Spring Boot - 获取所有的Bean信息

    前言 Spring Boot启动的时候需要加载许多Bean实现最小化配置,本文将尝试找出Spring启动后加载的所有Bean信息: 通过ApplicationContext 去获取所有的Bean 通过 ...

  2. Spring通过ApplicationContext主动获取bean

    有些场景无法通过AutoWired和compoment注解传递进来,于是希望通过Spring context主动去获取beandemo: package com.qhong.Util; import ...

  3. Spring IOC 容器源码分析 - 获取单例 bean

    1. 简介 为了写 Spring IOC 容器源码分析系列的文章,我特地写了一篇 Spring IOC 容器的导读文章.在导读一文中,我介绍了 Spring 的一些特性以及阅读 Spring 源码的一 ...

  4. Spring容器、BeanFactory和ApplicationContext,及3种装配Bean的方式

    目录 一. spring容器理解 二. BeanFactory和ApplicationContext之间的关系 三. BeanFactory详情介绍 四.ApplicationContext介绍 五. ...

  5. 5.2:缓存中获取单例bean

    5.2  缓存中获取单例bean 介绍过FactoryBean的用法后,我们就可以了解bean加载的过程了.前面已经提到过,单例在Spring的同一个容器内只会被创建一次,后续再获取bean直接从单例 ...

  6. java 从spring容器中获取注入的bean对象

      java 从spring容器中获取注入的bean对象 CreateTime--2018年6月1日10点22分 Author:Marydon 1.使用场景 控制层调用业务层时,控制层需要拿到业务层在 ...

  7. 普通Java类获取Spring的Bean的方法

    普通Java类获取Spring的Bean的方法 在SSH集成的前提下.某些情况我们需要在Action以外的类中来获得Spring所管理的Service对象. 之前我在网上找了好几好久都没有找到合适的方 ...

  8. web项目中获取spring的bean对象

    Spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架,如何在程序中不通过注解的形式(@Resource.@Autowired)获取Spring配置的bean呢? Bean工厂(c ...

  9. 普通java类获取spring容器bean的方法

    很多时候,我们在普通的java类中需要获取spring的bean来做操作,比如,在线程中,我们需要操作数据库,直接通过spring的bean中构建的service就可以完成.无需自己写链接..有时候有 ...

随机推荐

  1. [20191127]表 full Hash Value的计算.txt

    [20191127]表 full Hash Value的计算.txt --//曾经做过表full Hash Value的计算,当时我是通过建立简单的schema以及表名的形式,使用hashcat破解o ...

  2. bayaim——达梦数据库安装DM7-单实例-Centos7.txt

    bay——安装_达梦DM7-单实例-Centos7.txt IP:10.20.100.22 ---------------------------------------------1.下载 注意:要 ...

  3. 阿里云ECS服务器部署HADOOP集群(四):Hive本地模式的安装

    本篇将在阿里云ECS服务器部署HADOOP集群(一):Hadoop完全分布式集群环境搭建的基础上搭建. 本地模式需要采用MySQL数据库存储数据. 1 环境介绍 一台阿里云ECS服务器:master ...

  4. docker容器虚拟化技术

    简单来说,在Windows系统下安装各种运行环境的坑简直不要太多了(● ̄(エ) ̄●),并不仅限于docker.Nginx.PHP.Python等等,我会尽详细写出实际过程中遇到的各种各样的奇葩问题 1 ...

  5. Java之System类

    System类概述 java.lang.System 类中提供了大量的静态方法,可以获取与系统相关的信息或系统级操作,在System类的API文档中,常用的方法有: public static lon ...

  6. Redis令牌桶限流

    一 .场景描述 在开发接口服务器的过程中,为了防止客户端对于接口的滥用,保护服务器的资源, 通常来说我们会对于服务器上的各种接口进行调用次数的限制.比如对于某个 用户,他在一个时间段(interval ...

  7. jQuery 源码解析(二十七) 样式操作模块 坐标详解

    样式操作模块可用于管理DOM元素的样式.坐标和尺寸,本节讲解一下坐标这一块. 对于坐标来说,jQuery提供了一个offset方法用于获取第一个匹配元素的坐标或者设置所有匹配元素的坐标,还有offse ...

  8. java遍历request.getParameterMap()中的值

    在开发过程中发现request对象有提供一个request.getParameterMap()方法可以获取到从前端请求发送的参数Map. 但是在使用get()方法通过key(键)去获取这个参数Map中 ...

  9. Gson 格式化JSON日期数据

    Google的Gson功能非常强大! 格式化日期我们只需要这样创建就好了 Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd hh ...

  10. mysql定时任务(event事件)

    1.事件简介 事件(event)是MySQL在相应的时刻调用的过程式数据库对象.一个事件可调用一次,也可周期性的启动,它由一个特定的线程来管理的,也就是所谓的“事件调度器”. 事件和触发器类似,都是在 ...