Saturday, September 27, 2008

Web Beans Component

In the last post, I gave brief introduction to the Web Beans Specification, JSR 299. In this blog, I am trying to give you a real java codes :) Showing with code instead of writing is great . Ok, lets start with the definition of the web bean component and create some codes for them.
public class Payment{

For Web Beans Container understands that this class is a web beans component, it looks for some annotations defined on the class. In Web Beans every component is annotated with the Component Type annotation. Every component type annnotation is annotated with meta annotation @ComponentType. For example, in the above code, Payment class is annotated with the @Component component type. If we look at the definiton of the @Component annotation, it looks like

@Target(value = { ElementType.TYPE, ElementType.METHOD })
@Retention(value = RetentionPolicy.RUNTIME)
public @interface Component {

@Component annotation is the built-in annotation. There are two built-in annotations in the web beans container, these are @Standart and @Component. You are free to define more components with creating new component types.

In web beans container, every web beans defines its scoped using the scope type annotation. In the above code, class is annotated with the @RequestScoped built-in scope types. Scope type annotation is defined using the @ScopeType meta annotation. For example, below is the definition of the @RequestScoped annotation. Scope type defines the context of the web beans component instances. For example, Payment component instances are only available in the current request context. Each request context is available between the Servlet Request Start and End.

@Target( { ElementType.TYPE, ElementType.METHOD })
public @interface RequestScoped {

There are other built-in scope types in the web beans container
  • Application Scoped : Start when Servlet Context is initialized and is destroyed when Servlet Context is destroyed.
  • Session Scoped : Start when Http Session is started and is destroyed when Http Session is destroyed at the end of the session.
  • Conversation Scoped : This is the more complicated scoped definiton. We will look at this scope witihin the next blogs.
Every web beans components has named. Naming is differs with respect to the type of the component. There are several different type of the component definition in the web beans specifications, these are standart type components like Payment class annotated with @Component , producer methods components (these are defined at the method level) and EJB related component definitions. We will look at the producer method and EJB components within the coming next blogs. In the standart type components, component is named with @Named annotation. If there is no annotation, the name is the simple name of the class with changing the first character to the lowe case, in the Payment example, component name is payment.

So far we defined the followings about the web beans components
  • Component Type
  • Scope Type
  • Name
We have to explicitly specify that Web Bean Component and the Web Bean Component Instance is different from each other. Instances are created and initialized by the web beans container using the web beans components. We can say that web beans component actually defines the semantic of the classes that instantiates the actual components. Actually, web beans container uses the Container and Context API s to create components instances. We will explains these API in the next coming blogs.

There are two more definitons that must be explained that enable web beans container to fully differentiate the web beans components from each other. These are the API type of the components and the binding type annotation. API type is the class and interface type of the web beans component that it implements. binding type is the special annotation, that differentiates the web beans components that implements the same interface API contract. Binding type annotations is defined using the @BindingType meta annotation.

Lets create some interface that our Payment class implements, call IPayment

public interface IPayment{
public void doPay();

And create some sub-classes,

public class CreditPayment implements IPayment{

public class CheckPayment implements IPayment{


@Target({ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER, ElementType.TYPE})
public @interface Check {

@Target({ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER, ElementType.TYPE})
public @interface Credit {
Ok, in the above code we defined two binding type annotation, two class and one interface. This two web beans components implements the same API type contract, namely IPayment. But it differs from each other with binding annotations. When the web beans container injects the IPayment run time instances into the other web beans components, it differentiates each other using the "API type and Binding Type."For example in the following code, web beans container selects the CreditPayment web beans component in the runtime,
public class
PaymentService {

@In @Credit IPayment payment;

public void doPayment
In the above code, web beans container injects the CreditPayment instance in the runtime into the PaymentService component. Altough, the API type is the same (IPayment), binding type is the Credit.

Ohhh, we have defined lots of new things in this blog :). In the next blog, we will look at the other type web beans components, componens event and interceptor mechnaisms and so much...

Monday, September 15, 2008

Introduction to WebBeans

Hi to all;

Nowadays I am playing with the Web Beans Specification. It is published on the with the code JSR-299.

In these small transcripts, I will try to explain and give an example of what the Web Bean tries to solve in the enterprise development. We can summarize its main objectives as follow
  • Defines contextual components.
  • Handles dependency injections and auto destruction of the components.
  • Defines loosely coupled components wiring using its powerful events mechanism.
  • Adds new scope types to the already defined request, session and application scopes defined in the servlet specification.
  • Allows deployment time changing of the component implementation
  • Allows runtime changing of the component implementation with type safe resolutions
  • Allow components integration with Unified EL
  • Integrates JSF (Java Server Faces) and EJB3 technologies seemlesly. It allows EJB3 session beans as JSF managed beans.
  • It integrates with the JPA extended persistence context.
Actually, Web Beans specification defines the Unified Component Model for Java Enterprise Application based on the existing J2EE specifications. It does not add extra functionality such as transaction management, security etc., it defines and manages the stateful contextual components within well defined scopes.

We will try to create some code examples in the next post to further explain above concepts.