Spring Boot - 获取所有的Bean信息

本文介绍如何在SpringBoot启动时获取所有加载的Bean信息。通过实现CommandLineRunner接口,在应用启动后执行代码,利用ApplicationContext获取Bean定义名称,并打印每个Bean的类型。此方法有助于理解SpringBoot的初始化过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前言

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
...
...
...

转载于:https://www.cnblogs.com/chenpi/p/9785938.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值