Red Hat

Web Beans Sneak Peek Part III: Declaring components using XML

Posted by Gavin King    |    Sep 23, 2007    |    Tagged as CDI Java EE

/This is the third installment of a series of articles describing the current status of the Web Beans specification. You can find the first installment here and the second installment here./

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

  • when the implementation class comes from some pre-existing library
  • when there should be multiple components with the same implementation class

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

  • write a /resolver method/
  • declare the component using XML

In a future installment we'll talk more about all the crazy kinds of things we can do with resolver methods. First, let's just prove that Web Beans is not entirely annotation-centric.

We can declare a component in web-beans.xml:

<component>
    <class>java.util.Date</class>
</component>

Then an instance of Date could be injected by another component:

@In Date date;

By default, any component declared in XML has the component type @Component. We can use a custom component type:

<component>
    <class>java.util.Date</class>
    <type>org.jboss.test.Mock</type>
</component>

We can override the default component name:

<component>
    <class>java.util.Date</class>
    <name>now</name>
</component>

Or we can specify a scope for the component:

<component>
    <class>java.util.Date</class>
    <scope>javax.webbeans.SessionScoped</scope>
</component>

We can even specify binding annotations using XML, to distinguish between multiple components with the same implementation class:

<component>
    <class>java.util.Date</class>
    <name>now</name>
    <binding>org.jboss.eg.Now</binding>
</component>

<component>
    <class>java.util.Date</class>
    <name>logInTime</name>
    <scope>javax.webbeans.SessionScoped</scope>
    <binding>org.jboss.eg.LogInTime</binding>
</component>

<component>
    <class>java.util.Date</class>
    <name>startupTime</name>
    <scope>javax.webbeans.ApplicationScoped</scope>
    <binding>org.jboss.eg.StartupTime</binding>
</component>

Where @Now, @LogIn and @Startup are binding annotations used at the injection points:

@In @Now Date currentTime;
@In @LogInTime Date loginTime
@In @StartupTime Date startupTime

As usual, a component may support multiple binding annotations:

<component>
    <class>org.jboss.eg.AsynchronousChequePaymentProcessor</class>
    <binding>org.jboss.eg.PayByCheque</binding>
    <binding>org.jboss.eg.Asynchronous</binding>
</component>

Eventually, Web Beans will also support XML-based configuration of component properties, using literal or EL values, something like this:

<component>
    <class>org.jboss.framework.Captcha</class>
    <property name="strategy">math2digit</property>
    <property name="language">#{user.language}</property>
</component>

However, we've not yet worked out all the details of this. In particular, I would love to support a namespaced approach to component configuration, as seen in Seam or Spring 2.0.

back to top