java class 可注射的正确注解是什么?
What is the correct annotation for a java class to be injectable?
我有一个 POJO,我想将其注入到 CDI Bean 中。现在我知道我可以将 beans.xml 中的发现模式从 'annotated' 更改为 'all'。但我也可以只给我的 POJO 一个 bean 定义注释。我想任何注解都可以,但我想知道,仅用于使我的 POJO 可注入的单一目的的正确注解是什么?
@Dependent 将在每个注入点为该 bean 创建一个新实例。通常是干扰最少的。
http://docs.oracle.com/javaee/7/api/javax/enterprise/context/Dependent.html
您可能根本没有注释,不需要注释即可使您的 bean 可注入(即使其成为托管 bean 或 cdi bean)。
为了注入 class 它应该是:
- 具体class(即不是抽象或接口)或者它应该被注释为
@Decorator
- 应该有无参数构造函数或用
@Inject
注释的构造函数
- 不应使用 EJB 组件定义注释进行注释或在
ejb-jar.xml
中声明为 EJB bean class。
所以你几乎可以注入所有 'normal' classes。此外,class 应该位于 bean 存档中。从 CDI 1.1 开始,有两种类型的 bean 存档:隐式和显式。来自 Oracle 文档:
An explicit bean archive is an archive that contains a beans.xml
deployment descriptor, which can be an empty file, contain no version
number, or contain the version number 1.1 with the bean-discovery-mode
attribute set to all. For example:
http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
version="1.1" bean-discovery-mode="all">
... CDI can manage and inject any bean in an explicit archive, except those annotated with @Vetoed.
An implicit bean archive is an archive that contains some beans
annotated with a scope type, contains no beans.xml deployment
descriptor, or contains a beans.xml deployment descriptor with the
bean-discovery-mode attribute set to annotated.
In an implicit archive, CDI can only manage and inject beans annotated
with a scope type.
For a web application, the beans.xml deployment descriptor, if
present, must be in the WEB-INF directory. For EJB modules or JAR
files, the beans.xml deployment descriptor, if present, must be in the
META-INF directory.
假设您想继续使用 bean 发现模式 annotated
,您需要在 POJO 上有一个 bean 定义注释,这将使它成为所谓的 implicit bean
。这是来自 CDI spec 的相关引述:
The set of bean defining annotations contains:
@ApplicationScoped, @SessionScoped, @ConversationScoped and @RequestScoped annotations,
all other normal scope types,
@Interceptor and @Decorator annotations,
all stereotype annotations (i.e. annotations annotated with @Stereotype),
and the @Dependent scope annotation.
至于哪个注释是正确的 - 这取决于您的用例。通常,您需要考虑 bean 的生命周期(请求、对话、与应用程序一样存在的应用程序范围)。但如果你真的不在乎,我会说直接去 @Dependent
。
事实上,如果您有 bean 发现模式 all
并且在该 POJO class 上没有注释,它无论如何都会被 CDI 选择为 @Dependent
。
希望能回答问题。
我有一个 POJO,我想将其注入到 CDI Bean 中。现在我知道我可以将 beans.xml 中的发现模式从 'annotated' 更改为 'all'。但我也可以只给我的 POJO 一个 bean 定义注释。我想任何注解都可以,但我想知道,仅用于使我的 POJO 可注入的单一目的的正确注解是什么?
@Dependent 将在每个注入点为该 bean 创建一个新实例。通常是干扰最少的。
http://docs.oracle.com/javaee/7/api/javax/enterprise/context/Dependent.html
您可能根本没有注释,不需要注释即可使您的 bean 可注入(即使其成为托管 bean 或 cdi bean)。
为了注入 class 它应该是:
- 具体class(即不是抽象或接口)或者它应该被注释为
@Decorator
- 应该有无参数构造函数或用
@Inject
注释的构造函数
- 不应使用 EJB 组件定义注释进行注释或在
ejb-jar.xml
中声明为 EJB bean class。
所以你几乎可以注入所有 'normal' classes。此外,class 应该位于 bean 存档中。从 CDI 1.1 开始,有两种类型的 bean 存档:隐式和显式。来自 Oracle 文档:
An explicit bean archive is an archive that contains a beans.xml deployment descriptor, which can be an empty file, contain no version number, or contain the version number 1.1 with the bean-discovery-mode attribute set to all. For example:
http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd" version="1.1" bean-discovery-mode="all"> ... CDI can manage and inject any bean in an explicit archive, except those annotated with @Vetoed.
An implicit bean archive is an archive that contains some beans annotated with a scope type, contains no beans.xml deployment descriptor, or contains a beans.xml deployment descriptor with the bean-discovery-mode attribute set to annotated.
In an implicit archive, CDI can only manage and inject beans annotated with a scope type.
For a web application, the beans.xml deployment descriptor, if present, must be in the WEB-INF directory. For EJB modules or JAR files, the beans.xml deployment descriptor, if present, must be in the META-INF directory.
假设您想继续使用 bean 发现模式 annotated
,您需要在 POJO 上有一个 bean 定义注释,这将使它成为所谓的 implicit bean
。这是来自 CDI spec 的相关引述:
The set of bean defining annotations contains:
@ApplicationScoped, @SessionScoped, @ConversationScoped and @RequestScoped annotations,
all other normal scope types,
@Interceptor and @Decorator annotations,
all stereotype annotations (i.e. annotations annotated with @Stereotype),
and the @Dependent scope annotation.
至于哪个注释是正确的 - 这取决于您的用例。通常,您需要考虑 bean 的生命周期(请求、对话、与应用程序一样存在的应用程序范围)。但如果你真的不在乎,我会说直接去 @Dependent
。
事实上,如果您有 bean 发现模式 all
并且在该 POJO class 上没有注释,它无论如何都会被 CDI 选择为 @Dependent
。
希望能回答问题。