すべてのSpring管理Beanを取得する方法は?
1. 概要
この記事では、Springが管理するすべてのBeanをコンテナとともに表示するためのさまざまな手法について説明します。
2. IoCコンテナ
Beanは、Springが管理するアプリケーションの基盤です。すべてのBeanは、ライフサイクルの管理を担当するIOCコンテナと共に存在します。
このコンテナ内のすべてのBeanのリストを取得するには2つの方法があります。
-
ListableBeanFactoryインターフェースの使用
-
スプリングブートアクチュエータの使用
3. ListableBeanFactoryインターフェースの使用
The ListableBeanFactory interface provides getBeanDefinitionNames() methodは、このファクトリで定義されているすべてのBeanの名前を返します。 このインターフェイスは、すべてのBeanインスタンスによって列挙されるようにBean定義をプリロードするすべてのBeanファクトリによって実装されます。
既知のすべてのサブインターフェイスとその実装クラスのリストは、the official documentationにあります。
この例では、SpringBootアプリケーションを使用します。
まず、いくつかのSpringBeanを作成します。 簡単なSpringControllerFooControllerを作成しましょう:
@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 BeanFooServiceに依存しています。
@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を作成したことに注意してください。
-
fooController
-
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とともに、it will also log all other beans that are in this containerであることに注意してください。 わかりやすくするために、かなりの数があるため、ここでは省略しました。
4. Spring BootActuatorの使用
Spring Boot Actuator機能は、アプリケーションの統計を監視するために使用されるエンドポイントを提供します。
/beans.を含む多くの組み込みエンドポイントが含まれています。これにより、アプリケーション内のすべてのSpring管理対象Beanの完全なリストが表示されます。 on the official docsの既存のエンドポイントの完全なリストを見つけることができます。
ここで、URLhttp://<address>:<management-port>/beans.にアクセスします。個別の管理ポートを指定していない場合は、デフォルトのサーバーポートを使用できます。 これにより、Spring IoCコンテナ内のすべてのBeanを表示するJSON応答が返されます。
[
{
"context": "application:8080",
"parent": null,
"beans": [
{
"bean": "fooController",
"aliases": [],
"scope": "singleton",
"type": "com.example.displayallbeans.controller.FooController",
"resource": "file [E:/Workspace/tutorials-master/spring-boot/target
/classes/com/example/displayallbeans/controller/FooController.class]",
"dependencies": [
"fooService"
]
},
{
"bean": "fooService",
"aliases": [],
"scope": "singleton",
"type": "com.example.displayallbeans.service.FooService",
"resource": "file [E:/Workspace/tutorials-master/spring-boot/target/
classes/com/example/displayallbeans/service/FooService.class]",
"dependencies": []
},
// ...other beans
]
}
]
もちろん、これも同じ春のコンテナにある他の多くのBeanで構成されていますが、わかりやすくするために、ここでは省略しています。
Spring Boot Actuatorsについて詳しく知りたい場合は、メインのSpring Boot Actuatorガイドに進んでください。
5. 結論
この記事では、ListableBeanFactoryインターフェースとSpring Boot Actuatorsを使用して、すべてのBeanをSpring IoC Containerで表示する方法について学習しました。
このチュートリアルのfull implementationは、over on Github.にあります。