Wednesday, January 18, 2012

JavaEE6: CDI - Bean and Bean Types

In my last post, I explained about CDI. Now let us talk about different CDI concepts.

Beans in CDI

According to Wikipedia "Beans are reusable software components for Java. Practically, they are classes written in the Java programming language conforming to a particular convention." CDI redefines this concept beyond Java beans and EJB.

In CDI,

  • A bean is a source of contextual objects that define application state and/or logic. These objects are called contextual instances of the bean.
  • A Java EE component is a bean if the lifecycle of its instances may be managed by the container according to the lifecycle context model defined in the CDI specification.
  • The container creates and destroys these instances and associates them with the appropriate context.
  • Contextual instances of a bean may be injected into other objects (including other bean instances) that execute in the same context, and may be used in EL expressions that are evaluated in the same context.

Bean Types

A bean type defines a client-visible type of the bean. A bean may have multiple bean types. For example, the following bean has four bean types:

public class CarFactory extends VehicleFactory implements Factory {
    ...
}

The bean types are CarFactory, VehicleFactory, Factory and Object.

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.

No comments: