1. 概要

この記事では、Springで管理されているすべてのBeanをコンテナー内に表示するためのさまざまな手法について説明します。

2. IoCコンテナ

Beanは、Springが管理するアプリケーションの基盤です。 すべてのBeanは、ライフサイクルの管理を担当するIOCコンテナー内に存在します。

このコンテナ内のすべてのBeanのリストは、次の2つの方法で取得できます。

  1. ListableBeanFactoryインターフェースの使用
  2. スプリングブートアクチュエータの使用

3. ListableBeanFactoryインターフェースの使用

ListableBeanFactoryインターフェースは、このファクトリで定義されたすべてのBeanの名前を返すgetBeanDefinitionNames()メソッドを提供します。 このインターフェースは、すべてのBeanインスタンスを列挙するために、Bean定義をプリロードするすべてのBeanファクトリによって実装されます。

既知のすべてのサブインターフェイスとその実装クラスのリストは、公式ドキュメントにあります。

この例では、Spring Bootアプリケーションを使用します。

まず、いくつかのSpringBeanを作成します。 簡単な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";
    }
}

このコントローラーは、別の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.";
    }
}

ここでは、2つの異なる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);
        }
    }
}

これにより、 applicationContextcontainerからすべてのBeanが出力されます。

fooController
fooService
//other beans

は、私たちが定義したBeanとともに、このコンテナにある他のすべてのBeanもログに記録することに注意してください。 わかりやすくするために、かなりの数があるため、ここでは省略しました。

4. SpringBootActuatorの使用

Spring Bootアクチュエータ機能は、アプリケーションの統計を監視するために使用されるエンドポイントを提供します。

/を含む多くの組み込みエンドポイントが含まれています豆。 これにより、アプリケーション内のすべてのSpring管理対象Beanの完全なリストが表示されます。 上の既存のエンドポイントの完全なリストは、公式ドキュメントにあります。

 

今、私たちはちょうどURLを打つでしょう http://

/豆。

個別の管理ポートを指定していない場合は、デフォルトのサーバーポートを使用できます。 これにより、SpringIoCコンテナ内のすべてのBeanを表示するJSON応答が返されます。

[
    {
        "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 Actuatorsについて詳しく知りたい場合は、メインの Spring BootActuatorガイドに進んでください。

5. 結論

この記事では、ListableBeanFactoryインターフェースとSpringBootActuatorsを使用して、SpringIoCコンテナーにすべてのBeanを表示する方法について学習しました。

このチュートリアルの完全な実装は、Githubのにあります。