Spring Boot - 使用类类型信息获取所有已加载的bean

2024-05-19 19:38

本文主要是介绍Spring Boot - 使用类类型信息获取所有已加载的bean,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Spring启动会在内部加载大量bean,以最少的配置运行您的应用程序。在这个例子中,我们将学习如何找出所有那些Spring boot加载的bean及其类类型信息。

使用ApplicationContext获取所有已加载的bean

要自动执行方法,当应用程序完全加载时,我正在使用CommandLineRunner接口。CommandLineRunner用于指示bean 在Spring应用程序中包含时应该运行

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 implementsCommandLineRunner {

 

    @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());

        }

    }

}

在应用程序上运行将在控制台中打印bean名称和类型信息,如下所示:

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

 

basicErrorController of Type :: classorg.springframework.boot.autoconfigure.web.BasicErrorController

beanNameHandlerMapping of Type :: classorg.springframework.web.servlet.handler.BeanNameUrlHandlerMapping

beanNameViewResolver of Type :: class org.springframework.web.servlet.view.BeanNameViewResolver

characterEncodingFilter of Type :: classorg.springframework.boot.web.filter.OrderedCharacterEncodingFilter

conventionErrorViewResolver of Type :: classorg.springframework.boot.autoconfigure.web.DefaultErrorViewResolver

defaultServletHandlerMapping of Type :: classorg.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport$EmptyHandlerMapping

defaultViewResolver of Type :: classorg.springframework.web.servlet.view.InternalResourceViewResolver

dispatcherServlet of Type :: class org.springframework.web.servlet.DispatcherServlet

dispatcherServletRegistration of Type :: classorg.springframework.boot.web.servlet.ServletRegistrationBean

duplicateServerPropertiesDetector of Type :: classorg.springframework.boot.autoconfigure.web.ServerPropertiesAutoConfiguration$DuplicateServerPropertiesDetector

embeddedServletContainerCustomizerBeanPostProcessor of Type :: classorg.springframework.boot.context.embedded.EmbeddedServletContainerCustomizerBeanPostProcessor

error of Type :: classorg.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration$SpelView

errorAttributes of Type :: classorg.springframework.boot.autoconfigure.web.DefaultErrorAttributes

...

...

...

我截断了输出。您可以自己验证整个列表。

这篇关于Spring Boot - 使用类类型信息获取所有已加载的bean的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/995873

相关文章

Python使用PIL库将PNG图片转换为ICO图标的示例代码

《Python使用PIL库将PNG图片转换为ICO图标的示例代码》在软件开发和网站设计中,ICO图标是一种常用的图像格式,特别适用于应用程序图标、网页收藏夹图标等场景,本文将介绍如何使用Python的... 目录引言准备工作代码解析实践操作结果展示结语引言在软件开发和网站设计中,ICO图标是一种常用的图像

使用Java发送邮件到QQ邮箱的完整指南

《使用Java发送邮件到QQ邮箱的完整指南》在现代软件开发中,邮件发送功能是一个常见的需求,无论是用户注册验证、密码重置,还是系统通知,邮件都是一种重要的通信方式,本文将详细介绍如何使用Java编写程... 目录引言1. 准备工作1.1 获取QQ邮箱的SMTP授权码1.2 添加JavaMail依赖2. 实现

MyBatis与其使用方法示例详解

《MyBatis与其使用方法示例详解》MyBatis是一个支持自定义SQL的持久层框架,通过XML文件实现SQL配置和数据映射,简化了JDBC代码的编写,本文给大家介绍MyBatis与其使用方法讲解,... 目录ORM缺优分析MyBATisMyBatis的工作流程MyBatis的基本使用环境准备MyBati

Java嵌套for循环优化方案分享

《Java嵌套for循环优化方案分享》介绍了Java中嵌套for循环的优化方法,包括减少循环次数、合并循环、使用更高效的数据结构、并行处理、预处理和缓存、算法优化、尽量减少对象创建以及本地变量优化,通... 目录Java 嵌套 for 循环优化方案1. 减少循环次数2. 合并循环3. 使用更高效的数据结构4

使用Python开发一个图像标注与OCR识别工具

《使用Python开发一个图像标注与OCR识别工具》:本文主要介绍一个使用Python开发的工具,允许用户在图像上进行矩形标注,使用OCR对标注区域进行文本识别,并将结果保存为Excel文件,感兴... 目录项目简介1. 图像加载与显示2. 矩形标注3. OCR识别4. 标注的保存与加载5. 裁剪与重置图像

使用Python实现表格字段智能去重

《使用Python实现表格字段智能去重》在数据分析和处理过程中,数据清洗是一个至关重要的步骤,其中字段去重是一个常见且关键的任务,下面我们看看如何使用Python进行表格字段智能去重吧... 目录一、引言二、数据重复问题的常见场景与影响三、python在数据清洗中的优势四、基于Python的表格字段智能去重

java两个List的交集,并集方式

《java两个List的交集,并集方式》文章主要介绍了Java中两个List的交集和并集的处理方法,推荐使用Apache的CollectionUtils工具类,因为它简单且不会改变原有集合,同时,文章... 目录Java两个List的交集,并集方法一方法二方法三总结java两个List的交集,并集方法一

Spring AI集成DeepSeek三步搞定Java智能应用的详细过程

《SpringAI集成DeepSeek三步搞定Java智能应用的详细过程》本文介绍了如何使用SpringAI集成DeepSeek,一个国内顶尖的多模态大模型,SpringAI提供了一套统一的接口,简... 目录DeepSeek 介绍Spring AI 是什么?Spring AI 的主要功能包括1、环境准备2

Spring AI集成DeepSeek实现流式输出的操作方法

《SpringAI集成DeepSeek实现流式输出的操作方法》本文介绍了如何在SpringBoot中使用Sse(Server-SentEvents)技术实现流式输出,后端使用SpringMVC中的S... 目录一、后端代码二、前端代码三、运行项目小天有话说题外话参考资料前面一篇文章我们实现了《Spring

Spring AI与DeepSeek实战一之快速打造智能对话应用

《SpringAI与DeepSeek实战一之快速打造智能对话应用》本文详细介绍了如何通过SpringAI框架集成DeepSeek大模型,实现普通对话和流式对话功能,步骤包括申请API-KEY、项目搭... 目录一、概述二、申请DeepSeek的API-KEY三、项目搭建3.1. 开发环境要求3.2. mav