Using J2SE 5.0 Annotations

As you saw in Chapter 12 when looking at transactions, Spring provides the ability to define configuration options using source level metadata. As of Spring 1.1, this functionality was provided solely through the Commons Attributes metadata library, because at the time, there was no native Java support for source level metadata. With the advent of J2SE 5.0, Java now has native support for source level metadata using annotations.

Using Spring's annotation support, which will most likely be released in Spring 1.2, you can now access Java annotations using the Spring Attributes interface. In addition to this, many of the Spring features that use Commons Attributes for configuration will also begin to support Java annotations; indeed, the appropriate annotations for the transaction classes are almost complete.


We have assumed a certain familiarity with the new language features of Java 5.0 in this section. If you are unfamiliar with the new language features, read Java 5.0 Tiger: A Developer's Notebook by David Flanagan and Brett McLaughlin (O'Reilly, 2004).

Java annotations introduce a whole new set of syntax features into the language. To create an annotation, you use the @interface keyword; you can assign default values for annotation properties using the default keyword. Listing D-6 shows a simple example of this.

Listing D-6: The Name Annotation

image from book
package com.apress.prospring.apd.jsr175;      import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target;      @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) public @interface Name {          String first() default "John";     String last() default "Smith"; }  
image from book

Here you can see that we create an annotation called Name with two attributes, first and last. We give each of the annotation attributes a default value using the default keyword. Also notice that this annotation definition is itself annotated with two of the standard annotations. We use the Retention annotation to define how the annotation metadata should be retained during the compile/run process. We use RetentionPolicy.RUNTIME to indicate that this annotation should be accessible at runtime using reflection. In addition, the Target annotation defines those parts of your code, such as methods and classes, to which the annotation can be applied. The use of ElementType.TYPE means that you can apply this annotation to classes only.

Using this annotation to annotate another class is simple, as shown in Listing D-7.

Listing D-7: Using the Name Annotation

image from book
package com.apress.prospring.apd.jsr175;      @Name(first="Rob", last="Harrop") public class AttributeDemo {      }
image from book

Here you can see that we annotate the AttributeDemo class with the Name annotation, providing values for both the first and last attributes. Accessing the annotation information at runtime is easy using Spring's AnnotationsAttributes class, as shown in Listing D-8.

Listing D-8: Accessing Annotations at Runtime

image from book
package com.apress.prospring.apd.jsr175;      import java.util.Collection; import java.util.Iterator;      import org.springframework.metadata.Attributes; import org.springframework.metadata.annotations.AnnotationsAttributes;      public class Example {          public static void main(String[] args) {         Attributes attributes = new AnnotationsAttributes();              Collection attrs = attributes.getAttributes(AttributeDemo.class,                 Name.class);              for (Iterator i = attrs.iterator(); i.hasNext();) {             Name nameAttr = (Name);             System.out.println("Found name attribute: " + nameAttr.first()                     + " " + nameAttr.last());         }     } }
image from book

Pro Spring
Pro Spring
ISBN: 1590594614
EAN: 2147483647
Year: 2006
Pages: 189

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: