JBoss.org Community Documentation

9.1. Using annotations

With the notable exception of defining classloaders nearly all of the microcontainer features can be used by adding annotations to your POJO classes.

Note

We are not able to define classloaders using annotations for the simple reason that in order to read any annotations we must first load the class. Once the class is loaded we cannot change its classloader so a classloader annotation would be useless.

The microcontainer annotations live in the org.jboss.beans.metadata.api.annotations package and to use them you need Java 1.5 or above.

If you are using Java 1.4 or below then JBoss Microcontainer allows you to define a subset of the annotations in the XML deployment descriptor. The caveat is that these apply to bean instances instead of bean classes. As with normal annotations you are free to add as many as necessary at a single point. However you must always specify the fully-qualified class name since there is no way to 'import' packages in XML. Currently you can place annotations inside the following elements:

  • <deployment> -all beans in the deployment inherit this annotation

    
    <deployment name="SimpleDeployment" xmlns="urn:jboss:bean-deployer:2.0">
        <annotation>@org.jboss.test.TestAnnotation</annotation>
    </deployment>
  • <bean>

    
    <bean name="example" class="org.jboss.test.Example">
        <annotation>@org.jboss.test.TestAnnotation1</annotation>
        <annotation>@org.jboss.test.TestAnnotation2</annotation>
    </bean>
  • <constructor>

    
    <bean name="example" class="org.jboss.test.Example">
        <constructor>
            <annotation>@org.jboss.test.TestAnnotation</annotation>
        </constructor>
    </bean> 
  • <create>, <start>, <stop>, <destroy>

    
    <bean name="example" class="org.jboss.test.Example">
        <create>
            <annotation>@org.jboss.test.TestAnnotation</annotation>
        </create>
    </bean>
  • <install>, <uninstall>

    
    <bean name="example" class="org.jboss.test.Example">
        <install method="someMethod">
            <annotation>@org.jboss.test.TestAnnotation</annotation>
        </install>
    </bean> 
  • <incallback>, <uncallback>

    
    
  • <value>

    
    <bean name="example" class="org.jboss.test.Example">
        <property name="PropertyName">
            <annotation>@org.jboss.test.TestAnnotation</annotation>
            <value>123</value>
        </property>
    </bean>

Important

Annotations declared in XML apply to bean instances instead of bean classes.

Examples of how to use both normal and XML annotations will be given in the following sections.