【发布时间】:2020-12-30 01:08:23
【问题描述】:
我正在学习spring bean。我在BeanDefinition 中发现使用@Role 我们可以将角色添加到bean。但我不明白什么时候申请这个角色。我的意思是这个注释的效果是什么?人们何时以及如何使用此注释?我已阅读文档但无法正确理解
/**
* Role hint indicating that a {@code BeanDefinition} is a major part
* of the application. Typically corresponds to a user-defined bean.
*/
int ROLE_APPLICATION = 0;
/**
* Role hint indicating that a {@code BeanDefinition} is a supporting
* part of some larger configuration, typically an outer
* {@link org.springframework.beans.factory.parsing.ComponentDefinition}.
* {@code SUPPORT} beans are considered important enough to be aware
* of when looking more closely at a particular
* {@link org.springframework.beans.factory.parsing.ComponentDefinition},
* but not when looking at the overall configuration of an application.
*/
int ROLE_SUPPORT = 1;
/**
* Role hint indicating that a {@code BeanDefinition} is providing an
* entirely background role and has no relevance to the end-user. This hint is
* used when registering beans that are completely part of the internal workings
* of a {@link org.springframework.beans.factory.parsing.ComponentDefinition}.
*/
int ROLE_INFRASTRUCTURE = 2;
【问题讨论】:
-
这用于指示 bean 在应用程序中扮演的“角色”(属于哪个类别)。您很少需要在自己的 bean 上使用
@Role注释,据我所知,Spring 不会将它用于任何必要的事情。也许它是用来调试的。
标签: java spring spring-boot design-patterns