编程语言
首页 > 编程语言> > Java EE中的CDI-无法注入哪些Java类?

Java EE中的CDI-无法注入哪些Java类?

作者:互联网

我正在查看Oracle Java EE 6 Tutorial,并在“作为可注入对象的豆”部分下显示

The following kinds of objects can be injected:

(Almost) any Java class

Session beans
……

不能注入的Java类的例子是什么?他们只是提到了一些理论上的技术限制,还是可以注入的类的已知限制?

解决方法:

spec,ch. 2.2.1:

Almost any Java type may be a bean type of a bean:

  • A bean type may be an interface, a concrete class or an abstract class, and may be declared final or have final methods.
  • A bean type may be a parameterized type with actual type parameters and type variables.
  • A bean type may be an array type. Two array types are considered identical only if the element type is identical.
  • A bean type may be a primitive type. Primitive types are considered to be identical to their corresponding wrapper
    types in java.lang.
  • A bean type may be a raw type.

A type variable is not a legal bean type. A parameterized type that contains a wildcard type parameter is not a legal bean
type.

Note that certain additional restrictions are specified in Section 5.4.1, “Unproxyable bean types” for beans with a normal scope, as defined in Section 6.3, “Normal scopes and pseudo-scopes”.

然后是引用的部分:

Certain legal bean types cannot be proxied by the container:

  • classes which don’t have a non-private constructor with no parameters,
  • classes which are declared final or have final methods,
  • primitive types,
  • and array types.

综上所述:任何Java类型(包括接口,抽象类)都可以是CDI bean,除非它是“普通作用域”,并且至少满足以下条件之一:

>没有没有参数的非私有构造函数
>是最终方法/具有最终方法
>是原始(int,double等)或数组

普通作用域(例如,@ Application-,@ Session-,@ RequestScoped)意味着它需要由容器代理,因此上述限制可以用“不可行”代替. @Dependent和@ javax.inject.Singleton不是普通作用域,它们是伪作用域.

标签:cdi,java,java-ee
来源: https://codeday.me/bug/20191030/1964102.html