From f147d7ea6f026d66e3852c14bef86c2881360750 Mon Sep 17 00:00:00 2001 From: gumartinm Date: Wed, 9 Nov 2011 19:44:00 +0100 Subject: [PATCH] Adding code to execute other custom annotations. You can see here this powerful feature called Java Annotations. --- .../java/de/spring/example/AnnotationsHandler.java | 40 ++++++++++++++++++---- .../main/java/de/spring/example/SpringStart.java | 2 +- .../src/main/java/de/spring/example/Test.java | 25 +++++++------- .../example/annotation/CustomTransactional.java | 9 ++++- .../spring/example/annotation/TestAnnotation.java | 23 +++++++++++++ .../src/main/resources/spring-config.xml | 2 +- 6 files changed, 80 insertions(+), 21 deletions(-) create mode 100644 SpringJava/AnnotationsCustomHandler/custom-annotations-implementation/src/main/java/de/spring/example/annotation/TestAnnotation.java diff --git a/SpringJava/AnnotationsCustomHandler/custom-annotations-implementation/src/main/java/de/spring/example/AnnotationsHandler.java b/SpringJava/AnnotationsCustomHandler/custom-annotations-implementation/src/main/java/de/spring/example/AnnotationsHandler.java index d05ba92..a38540b 100644 --- a/SpringJava/AnnotationsCustomHandler/custom-annotations-implementation/src/main/java/de/spring/example/AnnotationsHandler.java +++ b/SpringJava/AnnotationsCustomHandler/custom-annotations-implementation/src/main/java/de/spring/example/AnnotationsHandler.java @@ -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 transactionalClass = genericBeanFactoryAccessor.getBeansWithAnnotation(CustomTransactional.class); - for (final Object myFoo : transactionalClass.values()) { - final Class 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 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 diff --git a/SpringJava/AnnotationsCustomHandler/custom-annotations-implementation/src/main/java/de/spring/example/SpringStart.java b/SpringJava/AnnotationsCustomHandler/custom-annotations-implementation/src/main/java/de/spring/example/SpringStart.java index 968fbcb..a88b6d2 100644 --- a/SpringJava/AnnotationsCustomHandler/custom-annotations-implementation/src/main/java/de/spring/example/SpringStart.java +++ b/SpringJava/AnnotationsCustomHandler/custom-annotations-implementation/src/main/java/de/spring/example/SpringStart.java @@ -10,4 +10,4 @@ public class SpringStart { System.out.println("Starting application"); SpringContextLocator.getInstance(); } -} +} \ No newline at end of file diff --git a/SpringJava/AnnotationsCustomHandler/custom-annotations-implementation/src/main/java/de/spring/example/Test.java b/SpringJava/AnnotationsCustomHandler/custom-annotations-implementation/src/main/java/de/spring/example/Test.java index 342ec02..dfad354 100644 --- a/SpringJava/AnnotationsCustomHandler/custom-annotations-implementation/src/main/java/de/spring/example/Test.java +++ b/SpringJava/AnnotationsCustomHandler/custom-annotations-implementation/src/main/java/de/spring/example/Test.java @@ -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"); + } + } } diff --git a/SpringJava/AnnotationsCustomHandler/custom-annotations-implementation/src/main/java/de/spring/example/annotation/CustomTransactional.java b/SpringJava/AnnotationsCustomHandler/custom-annotations-implementation/src/main/java/de/spring/example/annotation/CustomTransactional.java index d47064f..15b41f4 100644 --- a/SpringJava/AnnotationsCustomHandler/custom-annotations-implementation/src/main/java/de/spring/example/annotation/CustomTransactional.java +++ b/SpringJava/AnnotationsCustomHandler/custom-annotations-implementation/src/main/java/de/spring/example/annotation/CustomTransactional.java @@ -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 index 0000000..0855672 --- /dev/null +++ b/SpringJava/AnnotationsCustomHandler/custom-annotations-implementation/src/main/java/de/spring/example/annotation/TestAnnotation.java @@ -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 { + +} diff --git a/SpringJava/AnnotationsCustomHandler/custom-annotations-implementation/src/main/resources/spring-config.xml b/SpringJava/AnnotationsCustomHandler/custom-annotations-implementation/src/main/resources/spring-config.xml index e3bc2a5..0167510 100644 --- a/SpringJava/AnnotationsCustomHandler/custom-annotations-implementation/src/main/resources/spring-config.xml +++ b/SpringJava/AnnotationsCustomHandler/custom-annotations-implementation/src/main/resources/spring-config.xml @@ -10,7 +10,7 @@ -- 2.1.4