Adding code to execute other custom annotations.
authorgumartinm <gu.martinm@gmail.com>
Wed, 9 Nov 2011 18:44:00 +0000 (19:44 +0100)
committergumartinm <gu.martinm@gmail.com>
Wed, 9 Nov 2011 18:44:00 +0000 (19:44 +0100)
You can see here this powerful feature called Java Annotations.

SpringJava/AnnotationsCustomHandler/custom-annotations-implementation/src/main/java/de/spring/example/AnnotationsHandler.java
SpringJava/AnnotationsCustomHandler/custom-annotations-implementation/src/main/java/de/spring/example/SpringStart.java
SpringJava/AnnotationsCustomHandler/custom-annotations-implementation/src/main/java/de/spring/example/Test.java
SpringJava/AnnotationsCustomHandler/custom-annotations-implementation/src/main/java/de/spring/example/annotation/CustomTransactional.java
SpringJava/AnnotationsCustomHandler/custom-annotations-implementation/src/main/java/de/spring/example/annotation/TestAnnotation.java [new file with mode: 0644]
SpringJava/AnnotationsCustomHandler/custom-annotations-implementation/src/main/resources/spring-config.xml

index d05ba92..a38540b 100644 (file)
@@ -1,5 +1,7 @@
 package de.spring.example;
 
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
 import java.util.Map;
 import org.springframework.beans.BeansException;
 import org.springframework.beans.factory.InitializingBean;
@@ -7,6 +9,7 @@ import org.springframework.beans.factory.generic.GenericBeanFactoryAccessor;
 import org.springframework.context.ApplicationContext;
 import org.springframework.context.ApplicationContextAware;
 import de.spring.example.annotation.CustomTransactional;
+import de.spring.example.annotation.TestAnnotation;
 
 
 public class AnnotationsHandler implements ApplicationContextAware, InitializingBean {
@@ -16,12 +19,38 @@ public class AnnotationsHandler implements ApplicationContextAware, Initializing
          public void afterPropertiesSet() {
                  GenericBeanFactoryAccessor genericBeanFactoryAccessor = new GenericBeanFactoryAccessor(applicationContext);
                  
+                 //Spring searched the annotations configured in spring-config.xml file. 
+                 //With this code we can retrieve the Spring beans with those annotations.
                  final Map<String, Object> transactionalClass = genericBeanFactoryAccessor.getBeansWithAnnotation(CustomTransactional.class);
 
-                 for (final Object myFoo : transactionalClass.values()) {
-                         final Class<? extends Object> fooClass = myFoo.getClass();
-                         final CustomTransactional annotation = fooClass.getAnnotation(CustomTransactional.class);
-                         System.out.println("Found foo class: " + fooClass + ", with tags: ");
+                 for (final Object annotationObject : transactionalClass.values()) 
+                 {
+                         final Class<? extends Object> annotationClass = annotationObject.getClass();
+                         final CustomTransactional annotation = annotationClass.getAnnotation(CustomTransactional.class);
+                         System.out.println("Found foo class: " + annotationClass + ", with annotation: " + annotation);
+                         for (Method m : annotationClass.getDeclaredMethods())
+                         {
+                                 //Another annotation just to show the nice thing about using annotations in Java.
+                                 //It requires Java reflection.
+                                 if (m.isAnnotationPresent(TestAnnotation.class))
+                                 {
+                                       try {
+                                               m.invoke(null);
+                                       //First of all we catch exception related to Java reflection.
+                                       } catch (IllegalArgumentException e) {
+                                               System.out.println(m + " failed: " + e.getCause());
+                                       } catch (IllegalAccessException e) {
+                                               System.out.println(m + " failed: " + e.getCause());
+                                       } catch (InvocationTargetException e) {
+                                               System.out.println(m + " failed: " + e.getCause());
+                                       } catch (Exception e)
+                                       {
+                                               //If the invoke method throws an exception we can catch it here 
+                                               //and write a nice message.
+                                               System.out.println("INVALID method: " + m + ", something went wrong: " + e);
+                                       }
+                                 }
+                         }
                  }
          }
 
@@ -30,5 +59,4 @@ public class AnnotationsHandler implements ApplicationContextAware, Initializing
              throws BeansException {
            this.applicationContext = applicationContext;
          }
-       }
-
+       }
\ No newline at end of file
index 342ec02..dfad354 100644 (file)
@@ -1,18 +1,19 @@
 package de.spring.example;
 
 import de.spring.example.annotation.CustomTransactional;
+import de.spring.example.annotation.TestAnnotation;
 
-
-public class Test {
-  public void bar(){
-    System.out.println("This is the containing class");
-  }
-  
-  @CustomTransactional
-  public class InnerService {
-      public void innerMethod() {
-          System.out.println("I am the inner class");
-      }
-  }
+public class Test {    
+       public void bar(){
+               System.out.println("This is the containing class");
+       }
+       
+       @CustomTransactional
+       public class InnerService {
+               @TestAnnotation
+               public void innerMethod() {
+                       //System.out.println("I am the inner class");
+               }
+       }
 }
 
index d47064f..15b41f4 100644 (file)
@@ -3,11 +3,18 @@
  */
 package de.spring.example.annotation;
 
+import java.lang.annotation.ElementType;
 import java.lang.annotation.Retention;
 import java.lang.annotation.RetentionPolicy;
-import org.springframework.stereotype.Component;
+import java.lang.annotation.Target;
 
+/**
+ * 
+ * Spring will search this annotation.
+ *
+ */
 
+@Target({ElementType.METHOD, ElementType.TYPE})
 @Retention(RetentionPolicy.RUNTIME)
 public @interface CustomTransactional {
 }
diff --git a/SpringJava/AnnotationsCustomHandler/custom-annotations-implementation/src/main/java/de/spring/example/annotation/TestAnnotation.java b/SpringJava/AnnotationsCustomHandler/custom-annotations-implementation/src/main/java/de/spring/example/annotation/TestAnnotation.java
new file mode 100644 (file)
index 0000000..0855672
--- /dev/null
@@ -0,0 +1,23 @@
+/**
+ * 
+ */
+package de.spring.example.annotation;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * 
+ * We us this annotation to show how to invoke methods with annotations
+ * using Java reflection.
+ * This annotation will not be searched by Spring.
+ *
+ */
+
+@Target({ElementType.METHOD})
+@Retention(RetentionPolicy.RUNTIME)
+public @interface TestAnnotation {
+
+}
index e3bc2a5..0167510 100644 (file)
@@ -10,7 +10,7 @@
        <!--    This would be useful in case of not using a xml file for the beans declaration
                (we can use @Bean with @Configuration annotation instead of xml files) Not sure
                if this is useful in case of searching custom annotations. Apparently this code does not
-               need it, so I will no use it.
+               need it, so I will not use it.
        <context:annotation-config/>
        -->