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

相关文章

Spring Security常见问题及解决方案

《SpringSecurity常见问题及解决方案》SpringSecurity是Spring生态的安全框架,提供认证、授权及攻击防护,支持JWT、OAuth2集成,适用于保护Spring应用,需配置... 目录Spring Security 简介Spring Security 核心概念1. ​Securit

postgresql使用UUID函数的方法

《postgresql使用UUID函数的方法》本文给大家介绍postgresql使用UUID函数的方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录PostgreSQL有两种生成uuid的方法。可以先通过sql查看是否已安装扩展函数,和可以安装的扩展函数

SpringBoot+EasyPOI轻松实现Excel和Word导出PDF

《SpringBoot+EasyPOI轻松实现Excel和Word导出PDF》在企业级开发中,将Excel和Word文档导出为PDF是常见需求,本文将结合​​EasyPOI和​​Aspose系列工具实... 目录一、环境准备与依赖配置1.1 方案选型1.2 依赖配置(商业库方案)二、Excel 导出 PDF

SpringBoot改造MCP服务器的详细说明(StreamableHTTP 类型)

《SpringBoot改造MCP服务器的详细说明(StreamableHTTP类型)》本文介绍了SpringBoot如何实现MCPStreamableHTTP服务器,并且使用CherryStudio... 目录SpringBoot改造MCP服务器(StreamableHTTP)1 项目说明2 使用说明2.1

spring中的@MapperScan注解属性解析

《spring中的@MapperScan注解属性解析》@MapperScan是Spring集成MyBatis时自动扫描Mapper接口的注解,简化配置并支持多数据源,通过属性控制扫描路径和过滤条件,利... 目录一、核心功能与作用二、注解属性解析三、底层实现原理四、使用场景与最佳实践五、注意事项与常见问题六

Spring的RedisTemplate的json反序列泛型丢失问题解决

《Spring的RedisTemplate的json反序列泛型丢失问题解决》本文主要介绍了SpringRedisTemplate中使用JSON序列化时泛型信息丢失的问题及其提出三种解决方案,可以根据性... 目录背景解决方案方案一方案二方案三总结背景在使用RedisTemplate操作redis时我们针对

Java中Arrays类和Collections类常用方法示例详解

《Java中Arrays类和Collections类常用方法示例详解》本文总结了Java中Arrays和Collections类的常用方法,涵盖数组填充、排序、搜索、复制、列表转换等操作,帮助开发者高... 目录Arrays.fill()相关用法Arrays.toString()Arrays.sort()A

Spring Boot Maven 插件如何构建可执行 JAR 的核心配置

《SpringBootMaven插件如何构建可执行JAR的核心配置》SpringBoot核心Maven插件,用于生成可执行JAR/WAR,内置服务器简化部署,支持热部署、多环境配置及依赖管理... 目录前言一、插件的核心功能与目标1.1 插件的定位1.2 插件的 Goals(目标)1.3 插件定位1.4 核

如何使用Lombok进行spring 注入

《如何使用Lombok进行spring注入》本文介绍如何用Lombok简化Spring注入,推荐优先使用setter注入,通过注解自动生成getter/setter及构造器,减少冗余代码,提升开发效... Lombok为了开发环境简化代码,好处不用多说。spring 注入方式为2种,构造器注入和setter

MySQL中比较运算符的具体使用

《MySQL中比较运算符的具体使用》本文介绍了SQL中常用的符号类型和非符号类型运算符,符号类型运算符包括等于(=)、安全等于(=)、不等于(/!=)、大小比较(,=,,=)等,感兴趣的可以了解一下... 目录符号类型运算符1. 等于运算符=2. 安全等于运算符<=>3. 不等于运算符<>或!=4. 小于运