From: gumartinm Date: Wed, 9 Nov 2011 15:49:02 +0000 (+0100) Subject: First steps with my own Spring annotations handler. X-Git-Url: https://git.gumartinm.name/?a=commitdiff_plain;h=7ec4deff33a2440368963adbb6118e9bf6fb7a02;p=JavaForFun First steps with my own Spring annotations handler. --- 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 new file mode 100644 index 0000000..034a7a3 --- /dev/null +++ b/SpringJava/AnnotationsCustomHandler/custom-annotations-implementation/src/main/java/de/spring/example/AnnotationsHandler.java @@ -0,0 +1,33 @@ +package de.spring.example; + +import java.util.Map; +import org.springframework.beans.BeansException; +import org.springframework.beans.factory.InitializingBean; +import org.springframework.beans.factory.generic.GenericBeanFactoryAccessor; +import org.springframework.context.ApplicationContext; +import org.springframework.context.ApplicationContextAware; + + +public class AnnotationsHandler implements ApplicationContextAware, InitializingBean { + private ApplicationContext applicationContext; + + @Override + public void afterPropertiesSet() { + GenericBeanFactoryAccessor genericBeanFactoryAccessor = new GenericBeanFactoryAccessor(applicationContext); + + final Map transactionalClass = genericBeanFactoryAccessor.getBeansWithAnnotation(TransactionalN2A.class); + + for (final Object myFoo : transactionalClass.values()) { + final Class fooClass = myFoo.getClass(); + final TransactionalN2A annotation = fooClass.getAnnotation(TransactionalN2A.class); + System.out.println("Found 1 foo class: " + fooClass + ", with tags: "); + } + } + + @Override + public void setApplicationContext(final ApplicationContext applicationContext) + throws BeansException { + this.applicationContext = applicationContext; + } + } + diff --git a/SpringJava/AnnotationsCustomHandler/custom-annotations-implementation/src/main/java/de/spring/example/Prueba.java b/SpringJava/AnnotationsCustomHandler/custom-annotations-implementation/src/main/java/de/spring/example/Prueba.java new file mode 100644 index 0000000..15d3774 --- /dev/null +++ b/SpringJava/AnnotationsCustomHandler/custom-annotations-implementation/src/main/java/de/spring/example/Prueba.java @@ -0,0 +1,19 @@ +package de.spring.example; + +import es.dia.pos.n2a.aspectj.annotations.TransactionalN2A; + + +public class Prueba { + public void bar(){ + System.out.println("I am not a number, I am a free man!"); + } + + @TransactionalN2A + public class InnerService { + public void innerMethod() { + System.out.println("xxx: AopService$InnerClass.innerMethod()"); + } + } + +} + diff --git a/SpringJava/AnnotationsCustomHandler/custom-annotations-implementation/src/main/java/de/spring/example/SpringContextLocator.java b/SpringJava/AnnotationsCustomHandler/custom-annotations-implementation/src/main/java/de/spring/example/SpringContextLocator.java new file mode 100644 index 0000000..cc452f6 --- /dev/null +++ b/SpringJava/AnnotationsCustomHandler/custom-annotations-implementation/src/main/java/de/spring/example/SpringContextLocator.java @@ -0,0 +1,58 @@ +package de.spring.example; + +import org.springframework.context.ApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; + + +/** + * Localizador de beans para de los dispositivos + * + * @author rvp001es + */ +public final class SpringContextLocator { + + + // Singleton Pattern + private static SpringContextLocator instance; + + // Spring ApplicationContext + private static ApplicationContext context; + + // Dispositivos logicos + private static final String SPRING_CONFIG_CONTEXT="spring-config.xml"; + //private static final String DATABASE_CONFIG="database-config.xml"; + + + /** + * Private constructor. Singleton pattern. + */ + private SpringContextLocator() { + String[] factoryFiles = null; + System.out.println("Loading files context " + + SpringContextLocator.SPRING_CONFIG_CONTEXT); + + factoryFiles = new String[] { SPRING_CONFIG_CONTEXT }; + + SpringContextLocator.context = new ClassPathXmlApplicationContext(factoryFiles); + + System.out.println("The N2A devices context and test " + + "context has been loaded successfully!! "); + } + + /** + * Singleton pattern. GetInstance() + */ + public synchronized static SpringContextLocator getInstance() { + if (SpringContextLocator.instance == null) { + SpringContextLocator.instance = new SpringContextLocator(); + } + return SpringContextLocator.instance; + } + + /** + * Return a bean in application context. + */ + public Object getBean(final String name) { + return SpringContextLocator.context.getBean(name); + } +} 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 new file mode 100644 index 0000000..aeb6068 --- /dev/null +++ b/SpringJava/AnnotationsCustomHandler/custom-annotations-implementation/src/main/java/de/spring/example/SpringStart.java @@ -0,0 +1,15 @@ +package de.spring.example; + + +public class SpringStart { + + /** + * @param args + */ + public static void main(String[] args) { + System.out.println("HOLA"); + SpringContextLocator.getInstance(); + + } + +} diff --git a/SpringJava/AnnotationsCustomHandler/custom-annotations-implementation/src/main/java/de/spring/example/TransactionManagerN2A.java b/SpringJava/AnnotationsCustomHandler/custom-annotations-implementation/src/main/java/de/spring/example/TransactionManagerN2A.java new file mode 100644 index 0000000..30e9b66 --- /dev/null +++ b/SpringJava/AnnotationsCustomHandler/custom-annotations-implementation/src/main/java/de/spring/example/TransactionManagerN2A.java @@ -0,0 +1,41 @@ +package de.spring.example; + +import org.springframework.jdbc.datasource.DataSourceTransactionManager; +import org.springframework.transaction.TransactionStatus; + + +public class TransactionManagerN2A { + private static TransactionManagerN2A instance = new TransactionManagerN2A(); + private DataSourceTransactionManager transactionManager; + private TransactionStatus transactionStatus; + + //Why could you want to extend this class? + private TransactionManagerN2A() {} + + public static TransactionManagerN2A getInstance() { + return instance; + } + + public void initTransaction() + { + transactionStatus = this.transactionManager.getTransaction(null); + } + + public void rollbackTransaction() + { + this.transactionManager.rollback(this.transactionStatus); + } + + public void commitTransaction() + { + this.transactionManager.commit(this.transactionStatus); + } + + + /************************* Setters and getters *******************************************/ + public void setTransactionManager(final DataSourceTransactionManager transactionManager) + { + this.transactionManager = transactionManager; + } +} + diff --git a/SpringJava/AnnotationsCustomHandler/custom-annotations-implementation/src/main/java/de/spring/example/TransactionalN2A.java b/SpringJava/AnnotationsCustomHandler/custom-annotations-implementation/src/main/java/de/spring/example/TransactionalN2A.java new file mode 100644 index 0000000..474dc63 --- /dev/null +++ b/SpringJava/AnnotationsCustomHandler/custom-annotations-implementation/src/main/java/de/spring/example/TransactionalN2A.java @@ -0,0 +1,14 @@ +/** + * + */ +package de.spring.example; + +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import org.springframework.stereotype.Component; + + +@Retention(RetentionPolicy.RUNTIME) +@Component +public @interface TransactionalN2A { +} diff --git a/SpringJava/AnnotationsCustomHandler/custom-annotations-implementation/src/main/java/es/dia/pos/n2a/aspectj/annotations/AnnotationsHandler.java b/SpringJava/AnnotationsCustomHandler/custom-annotations-implementation/src/main/java/es/dia/pos/n2a/aspectj/annotations/AnnotationsHandler.java deleted file mode 100644 index 034a7a3..0000000 --- a/SpringJava/AnnotationsCustomHandler/custom-annotations-implementation/src/main/java/es/dia/pos/n2a/aspectj/annotations/AnnotationsHandler.java +++ /dev/null @@ -1,33 +0,0 @@ -package de.spring.example; - -import java.util.Map; -import org.springframework.beans.BeansException; -import org.springframework.beans.factory.InitializingBean; -import org.springframework.beans.factory.generic.GenericBeanFactoryAccessor; -import org.springframework.context.ApplicationContext; -import org.springframework.context.ApplicationContextAware; - - -public class AnnotationsHandler implements ApplicationContextAware, InitializingBean { - private ApplicationContext applicationContext; - - @Override - public void afterPropertiesSet() { - GenericBeanFactoryAccessor genericBeanFactoryAccessor = new GenericBeanFactoryAccessor(applicationContext); - - final Map transactionalClass = genericBeanFactoryAccessor.getBeansWithAnnotation(TransactionalN2A.class); - - for (final Object myFoo : transactionalClass.values()) { - final Class fooClass = myFoo.getClass(); - final TransactionalN2A annotation = fooClass.getAnnotation(TransactionalN2A.class); - System.out.println("Found 1 foo class: " + fooClass + ", with tags: "); - } - } - - @Override - public void setApplicationContext(final ApplicationContext applicationContext) - throws BeansException { - this.applicationContext = applicationContext; - } - } - diff --git a/SpringJava/AnnotationsCustomHandler/custom-annotations-implementation/src/main/java/es/dia/pos/n2a/aspectj/annotations/AspectHandler.java b/SpringJava/AnnotationsCustomHandler/custom-annotations-implementation/src/main/java/es/dia/pos/n2a/aspectj/annotations/AspectHandler.java deleted file mode 100644 index 0f0da3f..0000000 --- a/SpringJava/AnnotationsCustomHandler/custom-annotations-implementation/src/main/java/es/dia/pos/n2a/aspectj/annotations/AspectHandler.java +++ /dev/null @@ -1,19 +0,0 @@ -package de.spring.example; - -import java.util.Map; - -import org.aspectj.lang.annotation.Aspect; -import org.springframework.beans.BeansException; -import org.springframework.beans.factory.InitializingBean; -import org.springframework.beans.factory.generic.GenericBeanFactoryAccessor; -import org.springframework.context.ApplicationContext; -import org.springframework.context.ApplicationContextAware; - -@Aspect -public class AspectHandler { - - - - - } - diff --git a/SpringJava/AnnotationsCustomHandler/custom-annotations-implementation/src/main/java/es/dia/pos/n2a/aspectj/annotations/Prueba.java b/SpringJava/AnnotationsCustomHandler/custom-annotations-implementation/src/main/java/es/dia/pos/n2a/aspectj/annotations/Prueba.java deleted file mode 100644 index 548c5e9..0000000 --- a/SpringJava/AnnotationsCustomHandler/custom-annotations-implementation/src/main/java/es/dia/pos/n2a/aspectj/annotations/Prueba.java +++ /dev/null @@ -1,17 +0,0 @@ -package de.spring.example; - - -public class Prueba { - public void bar(){ - System.out.println("I am not a number, I am a free man!"); - } - - @TransactionalN2A - public class InnerService { - public void innerMethod() { - System.out.println("xxx: AopService$InnerClass.innerMethod()"); - } - } - -} - diff --git a/SpringJava/AnnotationsCustomHandler/custom-annotations-implementation/src/main/java/es/dia/pos/n2a/aspectj/annotations/SpringContextLocator.java b/SpringJava/AnnotationsCustomHandler/custom-annotations-implementation/src/main/java/es/dia/pos/n2a/aspectj/annotations/SpringContextLocator.java deleted file mode 100644 index cc452f6..0000000 --- a/SpringJava/AnnotationsCustomHandler/custom-annotations-implementation/src/main/java/es/dia/pos/n2a/aspectj/annotations/SpringContextLocator.java +++ /dev/null @@ -1,58 +0,0 @@ -package de.spring.example; - -import org.springframework.context.ApplicationContext; -import org.springframework.context.support.ClassPathXmlApplicationContext; - - -/** - * Localizador de beans para de los dispositivos - * - * @author rvp001es - */ -public final class SpringContextLocator { - - - // Singleton Pattern - private static SpringContextLocator instance; - - // Spring ApplicationContext - private static ApplicationContext context; - - // Dispositivos logicos - private static final String SPRING_CONFIG_CONTEXT="spring-config.xml"; - //private static final String DATABASE_CONFIG="database-config.xml"; - - - /** - * Private constructor. Singleton pattern. - */ - private SpringContextLocator() { - String[] factoryFiles = null; - System.out.println("Loading files context " + - SpringContextLocator.SPRING_CONFIG_CONTEXT); - - factoryFiles = new String[] { SPRING_CONFIG_CONTEXT }; - - SpringContextLocator.context = new ClassPathXmlApplicationContext(factoryFiles); - - System.out.println("The N2A devices context and test " + - "context has been loaded successfully!! "); - } - - /** - * Singleton pattern. GetInstance() - */ - public synchronized static SpringContextLocator getInstance() { - if (SpringContextLocator.instance == null) { - SpringContextLocator.instance = new SpringContextLocator(); - } - return SpringContextLocator.instance; - } - - /** - * Return a bean in application context. - */ - public Object getBean(final String name) { - return SpringContextLocator.context.getBean(name); - } -} diff --git a/SpringJava/AnnotationsCustomHandler/custom-annotations-implementation/src/main/java/es/dia/pos/n2a/aspectj/annotations/SpringStart.java b/SpringJava/AnnotationsCustomHandler/custom-annotations-implementation/src/main/java/es/dia/pos/n2a/aspectj/annotations/SpringStart.java deleted file mode 100644 index b566806..0000000 --- a/SpringJava/AnnotationsCustomHandler/custom-annotations-implementation/src/main/java/es/dia/pos/n2a/aspectj/annotations/SpringStart.java +++ /dev/null @@ -1,14 +0,0 @@ -package de.spring.example; - -public class SpringStart { - - /** - * @param args - */ - public static void main(String[] args) { - System.out.println("HOLA"); - SpringContextLocator.getInstance(); - - } - -} diff --git a/SpringJava/AnnotationsCustomHandler/custom-annotations-implementation/src/main/java/es/dia/pos/n2a/aspectj/annotations/TransactionManagerN2A.java b/SpringJava/AnnotationsCustomHandler/custom-annotations-implementation/src/main/java/es/dia/pos/n2a/aspectj/annotations/TransactionManagerN2A.java deleted file mode 100644 index 30e9b66..0000000 --- a/SpringJava/AnnotationsCustomHandler/custom-annotations-implementation/src/main/java/es/dia/pos/n2a/aspectj/annotations/TransactionManagerN2A.java +++ /dev/null @@ -1,41 +0,0 @@ -package de.spring.example; - -import org.springframework.jdbc.datasource.DataSourceTransactionManager; -import org.springframework.transaction.TransactionStatus; - - -public class TransactionManagerN2A { - private static TransactionManagerN2A instance = new TransactionManagerN2A(); - private DataSourceTransactionManager transactionManager; - private TransactionStatus transactionStatus; - - //Why could you want to extend this class? - private TransactionManagerN2A() {} - - public static TransactionManagerN2A getInstance() { - return instance; - } - - public void initTransaction() - { - transactionStatus = this.transactionManager.getTransaction(null); - } - - public void rollbackTransaction() - { - this.transactionManager.rollback(this.transactionStatus); - } - - public void commitTransaction() - { - this.transactionManager.commit(this.transactionStatus); - } - - - /************************* Setters and getters *******************************************/ - public void setTransactionManager(final DataSourceTransactionManager transactionManager) - { - this.transactionManager = transactionManager; - } -} - diff --git a/SpringJava/AnnotationsCustomHandler/custom-annotations-implementation/src/main/java/es/dia/pos/n2a/aspectj/annotations/TransactionalN2A.java b/SpringJava/AnnotationsCustomHandler/custom-annotations-implementation/src/main/java/es/dia/pos/n2a/aspectj/annotations/TransactionalN2A.java deleted file mode 100644 index 474dc63..0000000 --- a/SpringJava/AnnotationsCustomHandler/custom-annotations-implementation/src/main/java/es/dia/pos/n2a/aspectj/annotations/TransactionalN2A.java +++ /dev/null @@ -1,14 +0,0 @@ -/** - * - */ -package de.spring.example; - -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import org.springframework.stereotype.Component; - - -@Retention(RetentionPolicy.RUNTIME) -@Component -public @interface TransactionalN2A { -}