SeamFramework.orgCommunity Documentation

Chapter 12. Defining Web Beans using XML

12.1. Declaring Web Bean classes
12.2. Declaring Web Bean metadata
12.3. Declaring Web Bean members
12.4. Declaring inline Web Beans
12.5. Using a schema

So far, we've seen plenty of examples of Web Beans declared using annotations. However, there are a couple of occasions when we can't use annotations to define the Web Bean:

In either of these cases, Web Beans gives us two options:

Many frameworks use XML to provide metadata relating to Java classes. However, Web Beans uses a very different approach to specifying the names of Java classes, fields or methods to most other frameworks. Instead of writing class and member names as the string values of XML elements and attributes, Web Beans lets you use the class or member name as the name of the XML element.

The advantage of this approach is that you can write an XML schema that prevents spelling errors in your XML document. It's even possible for a tool to generate the XML schema automatically from the compiled Java code. Or, an integrated development environment could perform the same validation without the need for the explicit intermediate generation step.

For each Java package, Web Beans defines a corresponding XML namespace. The namespace is formed by prepending urn:java: to the Java package name. For the package com.mydomain.myapp, the XML namespace is urn:java:com.mydomain.myapp.

Java types belonging to a package are referred to using an XML element in the namespace corresponding to the package. The name of the element is the name of the Java type. Fields and methods of the type are specified by child elements in the same namespace. If the type is an annotation, members are specified by attributes of the element.

For example, the element <util:Date/> in the following XML fragment refers to the class java.util.Date:


<WebBeans xmlns="urn:java:javax.webbeans"
          xmlns:util="urn:java:java.util">

    <util:Date/>

</WebBeans>

And this is all the code we need to declare that Date is a simple Web Bean! An instance of Date may now be injected by any other Web Bean:

@Current Date date

We can declare the scope, deployment type and interceptor binding types using direct child elements of the Web Bean declaration:


<myapp:ShoppingCart>
    <SessionScoped/>
    <myfwk:Transactional requiresNew="true"/>
    <myfwk:Secure/>
</myapp:ShoppingCart>

We use exactly the same approach to specify names and binding type:


<util:Date>
    <Named>currentTime</Named>
</util:Date>

<util:Date>
    <SessionScoped/>
    <myapp:Login/>
    <Named>loginTime</Named>
</util:Date>

<util:Date>
    <ApplicationScoped/>
    <myapp:SystemStart/>
    <Named>systemStartTime</Named>
</util:Date>

Where @Login and @SystemStart are binding annotations types.

@Current Date currentTime;

@Login Date loginTime;
@SystemStart Date systemStartTime;

As usual, a Web Bean may support multiple binding types:


<myapp:AsynchronousChequePaymentProcessor>
    <myapp:PayByCheque/>
    <myapp:Asynchronous/>
</myapp:AsynchronousChequePaymentProcessor>

Interceptors and decorators are just simple Web Beans, so they may be declared just like any other simple Web Bean:


<myfwk:TransactionInterceptor>
    <Interceptor/>
    <myfwk:Transactional/>
</myfwk:TransactionInterceptor>

TODO!

Web Beans lets us define a Web Bean at an injection point. For example:


<myapp:System>
    <ApplicationScoped/>
    <myapp:admin>
        <myapp:Name>
            <myapp:firstname>Gavin</myapp:firstname>
            <myapp:lastname>King</myapp:lastname>
            <myapp:email>gavin@hibernate.org</myapp:email>
        </myapp:Name>
    </myapp:admin>
</myapp:System>

The <Name> element declares a simple Web Bean of scope @Dependent and class Name, with a set of initial field values. This Web Bean has a special, container-generated binding and is therefore injectable only to the specific injection point at which it is declared.

This simple but powerful feature allows the Web Beans XML format to be used to specify whole graphs of Java objects. It's not quite a full databinding solution, but it's close!

If we want our XML document format to be authored by people who aren't Java developers, or who don't have access to our code, we need to provide a schema. There's nothing specific to Web Beans about writing or using the schema.


<WebBeans xmlns="urn:java:javax.webbeans"
          xmlns:myapp="urn:java:com.mydomain.myapp"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="urn:java:javax.webbeans http://java.sun.com/jee/web-beans-1.0.xsd
                              urn:java:com.mydomain.myapp http://mydomain.com/xsd/myapp-1.2.xsd">

    <myapp:System>
        ...
    </myapp:System>

</WebBeans>

Writing an XML schema is quite tedious. Therefore, the Web Beans RI project will provide a tool which automatically generates the XML schema from compiled Java code.