@ javax.annotation.ManagedBean是定义注解的CDI bean吗? [英] Is @javax.annotation.ManagedBean a CDI bean defining annotation?

查看:133
本文介绍了@ javax.annotation.ManagedBean是定义注解的CDI bean吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用

Given that the archive we deploy is an "implicit bean archive" (see below), using @javax.inject.Inject to inject a @javax.annotation.ManagedBean into another managed bean work in WildFly 8.1.0, but it won't work in GlassFish 4.0.1-b08 nor GlassFish 4.1-b13. GlassFish crash with this message:

WELD-001408:对类型的依赖关系...

WELD-001408: Unsatisfied dependencies for type...

我是否误解了以下概述的规格,或者GlassFish有错误?

Do I misunderstand the following outlined specifications or do GlassFish have a bug?

CDI 1.1( JSR-346 )第12.1节"Bean档案" :

CDI 1.1 (JSR-346) section 12.1 "Bean Archives" says:

显式bean归档文件是包含bean.xml文件的归档文件 [..].隐式bean档案文件是包含以下内容的任何其他档案文件: 或更多具有定义注释[..]的bean的bean类.

An explicit bean archive is an archive which contains a beans.xml file [..]. An implicit bean archive is any other archive which contains one or more bean classes with a bean defining annotation [..].

如果那样的话,我的归档文件中没有beans.xml描述符文件,那么我仍然可以使用具有"bean定义注释"的bean.问题是,定义注释的bean是什么?

If then, my archive has no beans.xml descriptor file, I will still be able to use beans that have a "bean defining annotation". Question is, what is a bean defining annotation?

CDI规范第2.5节"Bean定义注释"说:

The CDI specification section 2.5 "Bean defining annotations" says:

任何作用域类型都是定义注释的bean.

Any scope type is a bean defining annotation.

所以这很简单,根据CDI规范的这一部分,这就是全部.如果我部署的存档中没有beans.xml描述符文件,那么只要它们具有明确声明的作用域

So that's pritty clear and that's all there is to it according to this section of the CDI specification. If I deploy an archive with no beans.xml descriptor file in it, then I can @Inject beans as long as they have an explicitly declared scope, @javax.enterprise.context.RequestScoped for example. It works in both WildFly and GlassFish. However..

Java EE技术堆栈内的所有规范都必须遵守的子集规范,即受管Bean( @javax.annotation.ManagedBean 确实定义了一个托管bean.托管bean规范没有说@ManagedBean使bean成为注入点(即字段或参数)的合理注入 target .规范确实说这些bean可以在Java EE应用程序中的任何地方使用"(MB.1.2为什么要管理bean?"一节)在我耳中听起来也应该可以注入.

The subset specification that all specifications within the Java EE technology stack must adhere to, Managed Beans (JSR-316), has a "base model" in which @javax.annotation.ManagedBean do define a managed bean. The managed beans specification doesn't say that @ManagedBean makes the bean a plausible injection target for an injection point (i.e., field or parameter). The specification do say that the beans "can be used anywhere in a Java EE application" (section MB.1.2 "Why Managed Beans?") which in my ears sound like they should be injectable too.

Java EE 7规范( JSR-342 )就是这样说的在EE.5.24节对依赖关系注入的支持"中:

The Java EE 7 specification (JSR-342) has this to say in section EE.5.24 "Support for Dependency Injection":

容器必须支持带有注解的注入点 javax.inject.Inject注释仅在CDI规定的范围内.每 CDI规范,在托管上支持依赖项注入 豆.

Containers must support injection points annotated with the javax.inject.Inject annotation only to the extent dictated by CDI. Per the CDI specification, dependency injection is supported on managed beans.

当前有三种方法使类成为托管bean:

There are currently three ways for a class to become a managed bean:

  1. 成为EJB会话bean组件.
  2. 使用ManagedBean注释进行注释.
  3. 满足CDI规范中描述的条件.
  1. Being an EJB session bean component.
  2. Being annotated with the ManagedBean annotation.
  3. Satisfying the conditions described in the CDI specification.

至少满足这些条件之一的课程将有资格 用于CDI中所述的完全依赖项注入支持 规范.

Classes that satisfy at least one of these conditions will be eligible for full dependency injection support as described in the CDI specification.

您去了:@ManagedBean具有完全依赖项注入支持".没有一半或只有一点点支持.但是,我不确定到底是什么依赖注入支持".但是我认为以下段落对此描述足够好:

There you go: @ManagedBean has "full dependency injection support". Not half or just a little bit of support. Yet, I'm not that sure exactly what "dependency injection support" is. But I think that the paragraph that follows describe it well enough:

表EE.5-1中列出的满足第三个条件的组件类 以上条件,但第一个条件和第二个条件都不能 如果使用CDI进行注释,也可以用作CDI托管bean 定义bean的注释或包含在其CDI的bean归档文件中 已启用.但是,如果将它们用作CDI管理的Bean(例如, 注入到其他托管类中),则托管的实例 CDI可能不是Java EE管理的实例 容器.

Component classes listed in Table EE.5-1 that satisfy the third condition above, but neither the first nor the second condition, can also be used as CDI managed beans if they are annotated with a CDI bean-defining annotation or contained in a bean archive for which CDI is enabled. However, if they are used as CDI managed beans (e.g., injected into other managed classes), the instances that are managed by CDI may not be the instances that are managed by the Java EE container.

基本上,本段所说的第二个条件是 ,它可能是注入到其他托管类中的CDI托管bean(因为异常bean也可以").

Basically, what this paragraph says is that the second condition is CDI managed beans that may be injected into other managed classes (because the exception beans "can also").

伞式规范和托管Bean规范都清楚地表明CDI规范是硬道理.

The umbrella specification and the managed beans specification has both made it somewhat clear that the CDI specification has the last word.

仅在CDI规范中两次提到了@ManagedBean注释,这两个注释均在第11章中提到,它讲述了CDI扩展可以观察到的生命周期CDI事件. 11.5.7节是命中之一,它定义了一个ProcessInjectionPoint事件.托管bean可能会使用依赖项注入-在那里不足为奇.但是,第11.5.8节定义了ProcessInjectionTarget事件.规范对ProcessInjectionTarget事件的描述如下:

The @ManagedBean annotation is only found mentioned in the CDI specification two times, both of which occur in chapter 11 which speaks of life cycle CDI events that a CDI extension can observe. Section 11.5.7 is one of the hits and define a ProcessInjectionPoint event. A managed bean may use dependency injection - no surprise there. However, section 11.5.8 define a ProcessInjectionTarget event. Here's what the specification has to say about the ProcessInjectionTarget event:

容器必须为每个Java EE组件类触发一个事件 支持注入,该注入可以由容器在以下位置实例化 运行时,包括使用@ ManagedBean,EJB声明的每个托管bean 会话或消息驱动的bean,bean,拦截器或装饰器.

The container must fire an event for every Java EE component class supporting injection that may be instantiated by the container at runtime, including every managed bean declared using @ManagedBean, EJB session or message-driven bean, bean, interceptor or decorator.

此短语无疑表示@ManagedBean可用作注入点的目标,而无需添加范围类型的概念(

This phrase says undoubtedly that a @ManagedBean may be used as target for an injection point without adding the notion of scope types (@Dependent is always default).

如前所述,从WildFly的隐式bean归档中注入@ManagedBean,据我所知,这是所有引用的Java EE规范所必需的.因此,我认为是GlassFish带有错误.但是CDI规范从未在第2.5节"Bean定义批注"中对@ManagedBean讲过一句话,而且一如既往,在阅读重叠的Java EE规范时,我很不高兴,所以我想我应该问一下,然后提交一个对GlassFish团队来说是严重"的错误.

As stated earlier, injecting a @ManagedBean from an implicit bean archive work in WildFly and as far as I can understand, this is required by all Java EE specifications just quoted. So I think it is GlassFish that has a bug. But the CDI spec never said a word about @ManagedBean in section 2.5 "Bean defining annotations", and as always, I'm a nervewreck when reading through overlapping Java EE specifications, so I thought I should ask before I go and file a "critical" bug to the GlassFish team.

提交了GlassFish错误: https://java.net/jira/browse/GLASSFISH-21169 .

Filed a GlassFish bug: https://java.net/jira/browse/GLASSFISH-21169.

推荐答案

这不是一个完整的答案,因为当我们尝试将所有规范放在一起并使其具有合理性时,不可避免地会引起混淆.我只是想说 CDI 1.2 已经澄清了定义注解的bean到底是什么. (请参阅"2.5.1.Bean定义注释"一节). CDI 1.2给出了一个列表:

This is not a complete answer as confusion will inevitably arise when we try to put together and make sense out of all specifications. I just wanted to say that CDI 1.2 has made a clarification about what exactly a bean defining annotation is (see section "2.5.1. Bean defining annotations"). CDI 1.2 give a list:

一组定义注释的bean包含:

The set of bean defining annotations contains:

  • @ ApplicationScoped,@ SessionScoped,@ ConversationScoped和@RequestScoped注释,
  • 所有其他常规作用域类型,
  • @Interceptor和@Decorator批注,
  • 所有构造型注释(即以@Stereotype注释的注释)
  • 和@Dependent范围注释.
  • @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.

应该补充的是,定义普通作用域类型"(第二个要点)的是自定义作用域,已注释

It should be added that what defines a "normal scope type" (second bullet point) is a custom scope annotated @NormalScope.

这篇关于@ javax.annotation.ManagedBean是定义注解的CDI bean吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆