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. windows系统下压缩文件成tar.gz格式的方法

    tar.gz 是linux和unix下面比较常用的格式,几个命令就可以把文件压缩打包成tar.gz格式,然而这种格式在windows并不多见,WinRAR.WinZip等主流压缩工具可以释放解开,却不 ...

  2. js知识点面试题

    网上看到的一个题,在这里存一下 此为题目function Foo() { getName = function () { alert (1); }; return this; } Foo.getNam ...

  3. spring boot 加入mail邮件支持

    一.添加依赖 <!-- 邮件整合 --> <dependency> <groupId>org.springframework.boot</groupId> ...

  4. 如何在Oracle 12C中添加多个分区 (Doc ID 1482456.1)

    How to Add Multiple Partitions in Oracle 12C (Doc ID 1482456.1) APPLIES TO: Oracle Database - Enterp ...

  5. linux系统的特殊符号

    符号 作用 示例 # 注释符号,井号后的内容不会执行 echo #WORD ~ 当前用户的家目录 cd ~ ; 命令顺序执行,前面执行成功与否对后面没影响 COMMAND1 ; COMMAND2 &a ...

  6. Unity3D 截取6面图 做全景图脚本

    using System.Collections;using System.Collections.Generic;using UnityEditor;using UnityEngine; publi ...

  7. leetcode题解:整数反转

    给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转. 示例 1: 输入: 123 输出: 321  示例 2: 输入: -123 输出: -321 示例 3: 输入: 120 输出 ...

  8. js对文中某一处关键字自动检索和全文检索

    部分检索: 代码: <%@ page language="java" contentType="text/html; charset=utf-8" pag ...

  9. Thinkphp 5.1.24 parseKey缺陷导致聚合注入 分析

    测试url: http://127.0.0.1/thinkphp/thinkphp_5.1.24/public/index.php/index/index/sqli2?id=2 控制器是获取id参数作 ...

  10. Scrapy框架-中间件

    一.中间件中主要有3个函数方法 process_request:处理请求,默认返回值是None process_response:处理响应,默认返回值是response对象 process_exce ...