知識庫 / Spring / Spring Boot RSS 訂閱

如何獲取所有 Spring 管理的 Bean?

Spring Boot
HongKong
4
02:32 PM · Dec 06 ,2025

1. 概述

本文將探討如何顯示容器中所有 Spring 管理的 Bean 的不同技術。

2. IoC 容器

Bean 是 Spring 託管應用程序的基礎;所有 Bean 都位於 IoC 容器中,該容器負責管理它們的生命週期。

我們可以通過以下兩種方式獲取容器中所有 Bean 的列表:

  1. 使用 ListableBeanFactory 接口
  2. 使用 Spring Boot Actuator

3. 使用 ListableBeanFactory 接口

ListableBeanFactory 接口提供 getBeanDefinitionNames() 方法,該方法返回該工廠中定義的所有 Bean 的名稱。 此接口由所有預加載 Bean 定義以枚舉其所有 Bean 實例的 Bean 工廠實現。

您可以在 官方文檔 中找到所有已知子接口及其實現類的列表。

對於本示例,我們將使用 Spring Boot 應用程序。

首先,我們將創建一些 Spring Bean。 讓我們創建一個簡單的 Spring 控制器 FooController

@Controller
public class FooController {

    @Autowired
    private FooService fooService;
    
    @RequestMapping(value="/displayallbeans") 
    public String getHeaderAndBody(Map model){
        model.put("header", fooService.getHeader());
        model.put("message", fooService.getBody());
        return "displayallbeans";
    }
}

這個Controller依賴於另一個Spring Bean FooService

@Service
public class FooService {
    
    public String getHeader() {
        return "Display All Beans";
    }
    
    public String getBody() {
        return "This is a sample application that displays all beans "
          + "in Spring IoC container using ListableBeanFactory interface "
          + "and Spring Boot Actuators.";
    }
}

請注意,我們創建了兩個不同的 Bean:

  1. fooController
  2. fooService

在執行此應用程序時,我們將使用applicationContext對象並調用其getBeanDefinitionNames()方法,該方法將返回applicationContext容器中的所有 Bean:

@SpringBootApplication
public class Application {
    private static ApplicationContext applicationContext;

    public static void main(String[] args) {
        applicationContext = SpringApplication.run(Application.class, args);
        displayAllBeans();
    }
    
    public static void displayAllBeans() {
        String[] allBeanNames = applicationContext.getBeanDefinitionNames();
        for(String beanName : allBeanNames) {
            System.out.println(beanName);
        }
    }
}

這將打印出來自 applicationContext 容器的所有 Bean:

fooController
fooService
//other beans

請注意,除了我們定義的 Bean 之外,它還將記錄容器中所有其他 Bean。為了清晰起見,我們此處省略了它們,因為數量相當繁多。

4. 使用 Spring Boot Actuator

Spring Boot Actuator 功能提供端點,用於監控我們應用程序的統計信息。

它包含許多內置端點,包括 /beans。 這將顯示應用程序中所有 Spring 管理的 Bean 的完整列表。 您可以在 官方文檔 中找到現有端點的完整列表。

 

現在,我們只需訪問 URL http://<address>:<management-port>/beans。 如果我們未指定單獨的管理端口,我們可以使用默認服務器端口。 這將返回一個 JSON 響應,顯示 Spring IoC 容器中的所有 Bean:

[
    {
        "context": "application:8080",
        "parent": null,
        "beans": [
            {
                "bean": "fooController",
                "aliases": [],
                "scope": "singleton",
                "type": "com.baeldung.displayallbeans.controller.FooController",
                "resource": "file [E:/Workspace/tutorials-master/spring-boot/target
                  /classes/com/baeldung/displayallbeans/controller/FooController.class]",
                "dependencies": [
                    "fooService"
                ]
            },
            {
                "bean": "fooService",
                "aliases": [],
                "scope": "singleton",
                "type": "com.baeldung.displayallbeans.service.FooService",
                "resource": "file [E:/Workspace/tutorials-master/spring-boot/target/
                  classes/com/baeldung/displayallbeans/service/FooService.class]",
                "dependencies": []
            },
            // ...other beans
        ]
    }
]

當然,這還包括許多其他在同一個 Spring 容器中存在的 Bean,但為了清晰起見,我們在這裏省略了它們。

如果您想更深入地瞭解 Spring Boot Actuator,可以查看主 Spring Boot Actuator 指南。

5. 結論

在本文中,我們學習瞭如何使用 <em >Spring IoC 容器</em><em >ListableBeanFactory</em> 接口,以及 Spring Boot Actuators,來顯示容器中的所有 Bean。

user avatar
0 位用戶收藏了這個故事!
收藏

發佈 評論

Some HTML is okay.