如何解决春豆起什么作用?
我正在学习有关春豆的知识。我在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:
public static void getBeanRoleInfo(GenericApplicationContext ctx) {
int[] beanCntByRole = new int[3];
for (String name : ctx.getBeanDefinitionNames()) {
int roleNo = ctx.getBeanDefinition(name).getRole();
beanCntByRole[roleNo]++;
}
System.out.println("ROLE_APPLICATION : " + beanCntByRole[0] +
"\n" + "ROLE_SUPPORT : " + beanCntByRole[1] +
"\n" + "ROLE_INFRASTRUCTURE : " + beanCntByRole[2]
);
}
顺便说一下,Spring 团队没有使用 enum class 的原因是 Spring 的早期版本没有 enum class,而 jdk 1.5 之后就可以使用了。
如果你想设置它,你可以使用 @Role
注释。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。