ZetCode

Spring Boot 列出 Bean

最后修改于 2023 年 7 月 16 日

Spring Boot 列出 Bean 展示了如何列出存储在 Spring 容器中的所有 Bean,包括内置 Bean 和自定义 Bean。

Spring 是一个流行的 Java 应用框架,而 Spring Boot 是 Spring 的一个演进,它有助于以最小的努力创建独立的、生产级的基于 Spring 的应用程序。

核心 Spring 容器创建和管理 Bean。 可以通过 ApplicationContext 获取 Bean。 在以下应用程序中,我们列出所有已存储的 Bean。 该应用程序是一个命令行 Spring Boot 应用程序。

build.gradle
...
src
├───main
│   ├───java
│   │   └───com
│   │       └───zetcode
│   │           │   Application.java
│   │           │   MyRunner.java
│   │           └───bean
│   │                   MyBean.java
│   └───resources
└───test
    ├───java
    └───resources

这是 Spring Boot 应用程序的项目结构。

build.gradle
plugins {
    id 'org.springframework.boot' version '3.1.1'
    id 'io.spring.dependency-management' version '1.1.0'
    id 'java'
}

group = 'com.zetcode'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '17'

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter'
}

这是 Gradle 构建文件。 spring-boot-starter 是核心启动器,包括自动配置支持、日志记录和 YAML。

com/zetcode/bean/MyBean.java
package com.zetcode.bean;

import org.springframework.stereotype.Component;

@Component
public class MyBean {
    
    private final String message = "This is MyBean";
    
    public String getMessage() {
        
        return message;
    }
}

MyBean 是一个自定义 Bean,由 Spring 创建和管理。 由 @Component 注解修饰的类会被 Spring 自动检测并存储在 Spring 容器中。

com/zetcode/MyRunner.java
package com.zetcode;

import com.zetcode.bean.MyBean;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

@Component
public class MyRunner implements CommandLineRunner {

    private final ApplicationContext appContext;
    private final MyBean myBean;

    public MyRunner(ApplicationContext appContext, MyBean myBean) {
        this.appContext = appContext;
        this.myBean = myBean;
    }

    @Override
    public void run(String... args) throws Exception {

        System.out.println(myBean.getMessage());

        System.out.println("List of beans:");

        String[] beans = appContext.getBeanDefinitionNames();

        for (String bean : beans) {
            System.out.println(bean);
        }
    }
}

CommandLineRunner 接口指示当 Bean 包含在 SpringApplication 中时应该运行。 它可以用于创建 Spring Boot 命令行应用程序。

@Component
public class MyRunner implements CommandLineRunner {

MyRunner 也是一个 Spring Bean,并且被列在 Bean 之中。

public MyRunner(ApplicationContext appContext, MyBean myBean) {
    this.appContext = appContext;
    this.myBean = myBean;
}

ApplicationContext 和自定义的 MyBean 通过构造函数注入到字段中。

System.out.println(myBean.getMessage());

在这里,我们打印存储在自定义 Bean 中的消息。

String[] beans = appContext.getBeanDefinitionNames();

从应用程序上下文,我们使用 getBeanDefinitionNames 获取 Bean 名称的数组。

for (String bean : beans) {
    System.out.println(bean);
}

Bean 的名称被打印到控制台。

com/zetcode/Application.java
package com.zetcode;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

Application 中,设置 Spring Boot 应用程序。 设置 Spring Boot 应用程序。 @SpringBootApplication 启用自动配置和组件扫描。 Spring 将自动扫描 Bean,并将同时选取 MyBeanMyRunner

$ ./gradlew -q bootRun
...
This is MyBean
List of beans:
org.springframework.context.annotation.internalConfigurationAnnotationProcessor
org.springframework.context.annotation.internalAutowiredAnnotationProcessor
org.springframework.context.annotation.internalCommonAnnotationProcessor
org.springframework.context.event.internalEventListenerProcessor
org.springframework.context.event.internalEventListenerFactory
application
org.springframework.boot.autoconfigure.internalCachingMetadataReaderFactory
myRunner
myBean
...

我们运行应用程序。 -q 选项关闭 Gradle 消息。

在本文中,我们列出了存储在 Spring 容器中的所有 Bean。

作者

我的名字是 Jan Bodnar,我是一个充满激情的程序员,拥有丰富的编程经验。 我自 2007 年以来一直在撰写编程文章。 迄今为止,我撰写了 1,400 多篇文章和 8 本电子书。 我拥有超过十年的编程教学经验。

列出 Spring Boot 教程