From 7627fce49a3f12d90305df7df58c8c9b32ef8633 Mon Sep 17 00:00:00 2001 From: gumartinm Date: Wed, 9 Nov 2011 18:37:45 +0100 Subject: [PATCH] A very nice example about custom annotation in Spring. Bonus: really nice maven pom files configuration. --- .../custom-annotations-implementation/pom.xml | 47 +- .../java/de/spring/example/AnnotationsHandler.java | 7 +- .../src/main/java/de/spring/example/Prueba.java | 19 - .../de/spring/example/SpringContextLocator.java | 14 +- .../main/java/de/spring/example/SpringStart.java | 4 +- .../src/main/java/de/spring/example/Test.java | 18 + .../de/spring/example/TransactionManagerN2A.java | 41 -- .../java/de/spring/example/TransactionalN2A.java | 14 - .../example/annotation/CustomTransactional.java | 13 + .../src/main/resources/aop.xml | 11 - .../src/main/resources/spring-config.xml | 82 +-- SpringJava/AnnotationsCustomHandler/pom.xml | 656 ++++++++++++++++++++- 12 files changed, 720 insertions(+), 206 deletions(-) delete mode 100644 SpringJava/AnnotationsCustomHandler/custom-annotations-implementation/src/main/java/de/spring/example/Prueba.java create mode 100644 SpringJava/AnnotationsCustomHandler/custom-annotations-implementation/src/main/java/de/spring/example/Test.java delete mode 100644 SpringJava/AnnotationsCustomHandler/custom-annotations-implementation/src/main/java/de/spring/example/TransactionManagerN2A.java delete mode 100644 SpringJava/AnnotationsCustomHandler/custom-annotations-implementation/src/main/java/de/spring/example/TransactionalN2A.java create mode 100644 SpringJava/AnnotationsCustomHandler/custom-annotations-implementation/src/main/java/de/spring/example/annotation/CustomTransactional.java delete mode 100644 SpringJava/AnnotationsCustomHandler/custom-annotations-implementation/src/main/resources/aop.xml diff --git a/SpringJava/AnnotationsCustomHandler/custom-annotations-implementation/pom.xml b/SpringJava/AnnotationsCustomHandler/custom-annotations-implementation/pom.xml index dab88d4..1ee0664 100644 --- a/SpringJava/AnnotationsCustomHandler/custom-annotations-implementation/pom.xml +++ b/SpringJava/AnnotationsCustomHandler/custom-annotations-implementation/pom.xml @@ -4,75 +4,30 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> + 4.0.0 - custom-annotations de.spring.example 2.0-SNAPSHOT - de.spring.example custom-annotations-implementation - 2.0-SNAPSHOT custom-annotations-implementation http://maven.apache.org - - - - org.apache.maven - maven-model - 2.2.1 - - - - org.springframework spring-context - 2.5.6 - org.springframework spring-jdbc - 2.5.6 - c3p0 c3p0 - 0.9.1.2 - - - org.aspectj - aspectjrt - 1.6.12 - - - - org.aspectj - aspectjweaver - 1.6.12 - - - - - 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 034a7a3..d05ba92 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 @@ -6,6 +6,7 @@ import org.springframework.beans.factory.InitializingBean; import org.springframework.beans.factory.generic.GenericBeanFactoryAccessor; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; +import de.spring.example.annotation.CustomTransactional; public class AnnotationsHandler implements ApplicationContextAware, InitializingBean { @@ -15,12 +16,12 @@ public class AnnotationsHandler implements ApplicationContextAware, Initializing public void afterPropertiesSet() { GenericBeanFactoryAccessor genericBeanFactoryAccessor = new GenericBeanFactoryAccessor(applicationContext); - final Map transactionalClass = genericBeanFactoryAccessor.getBeansWithAnnotation(TransactionalN2A.class); + final Map transactionalClass = genericBeanFactoryAccessor.getBeansWithAnnotation(CustomTransactional.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: "); + final CustomTransactional annotation = fooClass.getAnnotation(CustomTransactional.class); + System.out.println("Found foo class: " + fooClass + ", with tags: "); } } 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 deleted file mode 100644 index 15d3774..0000000 --- a/SpringJava/AnnotationsCustomHandler/custom-annotations-implementation/src/main/java/de/spring/example/Prueba.java +++ /dev/null @@ -1,19 +0,0 @@ -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 index cc452f6..4d77808 100644 --- 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 @@ -5,22 +5,18 @@ 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 + // Spring Context private static final String SPRING_CONFIG_CONTEXT="spring-config.xml"; - //private static final String DATABASE_CONFIG="database-config.xml"; /** @@ -35,12 +31,12 @@ public final class SpringContextLocator { SpringContextLocator.context = new ClassPathXmlApplicationContext(factoryFiles); - System.out.println("The N2A devices context and test " + - "context has been loaded successfully!! "); + System.out.println("The context has been loaded successfully!! "); } /** - * Singleton pattern. GetInstance() + * Singleton pattern not thread safety. To use SingletoHolder pattern as the best approximation + * otherwise to use an Enum class (see Effective Java Second Edition and ) if we need serialization. */ public synchronized static SpringContextLocator getInstance() { if (SpringContextLocator.instance == null) { @@ -50,7 +46,7 @@ public final class SpringContextLocator { } /** - * Return a bean in application context. + * Return bean from 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 index aeb6068..968fbcb 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 @@ -7,9 +7,7 @@ public class SpringStart { * @param args */ public static void main(String[] args) { - System.out.println("HOLA"); + System.out.println("Starting application"); SpringContextLocator.getInstance(); - } - } 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 new file mode 100644 index 0000000..342ec02 --- /dev/null +++ b/SpringJava/AnnotationsCustomHandler/custom-annotations-implementation/src/main/java/de/spring/example/Test.java @@ -0,0 +1,18 @@ +package de.spring.example; + +import de.spring.example.annotation.CustomTransactional; + + +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"); + } + } +} + 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 deleted file mode 100644 index 30e9b66..0000000 --- a/SpringJava/AnnotationsCustomHandler/custom-annotations-implementation/src/main/java/de/spring/example/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/de/spring/example/TransactionalN2A.java b/SpringJava/AnnotationsCustomHandler/custom-annotations-implementation/src/main/java/de/spring/example/TransactionalN2A.java deleted file mode 100644 index 474dc63..0000000 --- a/SpringJava/AnnotationsCustomHandler/custom-annotations-implementation/src/main/java/de/spring/example/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 { -} 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 new file mode 100644 index 0000000..d47064f --- /dev/null +++ b/SpringJava/AnnotationsCustomHandler/custom-annotations-implementation/src/main/java/de/spring/example/annotation/CustomTransactional.java @@ -0,0 +1,13 @@ +/** + * + */ +package de.spring.example.annotation; + +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import org.springframework.stereotype.Component; + + +@Retention(RetentionPolicy.RUNTIME) +public @interface CustomTransactional { +} diff --git a/SpringJava/AnnotationsCustomHandler/custom-annotations-implementation/src/main/resources/aop.xml b/SpringJava/AnnotationsCustomHandler/custom-annotations-implementation/src/main/resources/aop.xml deleted file mode 100644 index 12e9571..0000000 --- a/SpringJava/AnnotationsCustomHandler/custom-annotations-implementation/src/main/resources/aop.xml +++ /dev/null @@ -1,11 +0,0 @@ - - - - - - - - - - \ No newline at end of file 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 6ea229c..e3bc2a5 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 @@ -2,75 +2,47 @@ - - - - + http://www.springframework.org/schema/context/spring-context-2.5.xsd"> + - + + + expression="de.spring.example.annotation.CustomTransactional" /> - - - - - - - - - - - - - - - - + + - - - - - - - - - - - - - - - - + + + diff --git a/SpringJava/AnnotationsCustomHandler/pom.xml b/SpringJava/AnnotationsCustomHandler/pom.xml index fa98657..d017725 100644 --- a/SpringJava/AnnotationsCustomHandler/pom.xml +++ b/SpringJava/AnnotationsCustomHandler/pom.xml @@ -1,17 +1,663 @@ - + + 4.0.0 de.spring.example custom-annotations 2.0-SNAPSHOT custom-annotations - http://maven.apache.org + http://www.myorganization.de + Spring Custom Annotations + + MyOrganization + http://www.myorganization.de + pom - + + trac + http://noserver/ + + + jenkins + http://noserver/jenkins/ + + + scm:svn:http://noserver + http://noserver + + + com.sun.jdmk + jmxtools + 1.2.1 + + + javax.activation + activation + 1.1 + + + log4j + log4j + 1.2.15 + + + com.sun.jdmk + jmxtools + + + com.sun.jmx + jmxri + + + javax.mail + mail + + + javax.jms + jms + + + + + junit + junit + 4.4 + test + + + + + c3p0 + c3p0 + 0.9.1.2 + + + cglib + cglib-nodep + 2.1_3 + + + commons-collections + commons-collections + 3.2.1 + + + commons-configuration + commons-configuration + 1.6 + + + commons-dbcp + commons-dbcp + 1.2.2 + + + commons-io + commons-io + 1.4 + + + commons-lang + commons-lang + 2.4 + + + commons-logging + commons-logging + 1.1.1 + + + commons-net + commons-net + 2.0 + + + commons-pool + commons-pool + 1.3 + + + com.h2database + h2 + 1.2.130 + + + dom4j + dom4j + 1.6.1 + + + xml-apis + xml-apis + + + + + hsqldb + hsqldb + 1.8.0.7 + + + javatar + javatar + 2.5 + + + jpos + jpos + 1.12.2 + provided + + + jpos + jpos-controls + 1.12.2 + provided + + + org.python + jython + 2.5.2b2 + + + urbanophile + java-getopt + 1.0.13 + + + mysql + mysql-connector-java + 5.1.6 + + + org.apache.ibatis + ibatis-sqlmap + 2.3.4.726 + + + org.apache.mina + mina-core + 2.0.0-M6 + + + org.aspectj + aspectjrt + 1.6.5 + + + org.aspectj + aspectjweaver + 1.6.5 + + + org.dbunit + dbunit + 2.4.4 + test + + + org.eclipse.jetty + jetty-continuation + 7.0.0.v20091005 + + + org.eclipse.jetty + jetty-http + 7.0.0.v20091005 + + + org.eclipse.jetty + jetty-io + 7.0.0.v20091005 + + + org.eclipse.jetty + jetty-security + 7.0.0.v20091005 + + + org.eclipse.jetty + jetty-server + 7.0.0.v20091005 + + + org.eclipse.jetty + jetty-servlet + 7.0.0.v20091005 + + + org.eclipse.jetty + jetty-webapp + 7.0.0.v20091005 + + + org.eclipse.jetty + jetty-util + 7.0.0.v20091005 + + + org.eclipse.jetty + jetty-xml + 7.0.0.v20091005 + + + org.slf4j + slf4j-api + 1.5.2 + + + org.slf4j + slf4j-log4j12 + 1.5.2 + + + log4j + log4j + + + + + org.springframework + spring-aop + ${spring.version} + + + org.springframework + spring-beans + ${spring.version} + + + org.springframework + spring-core + ${spring.version} + + + org.springframework + spring-context + ${spring.version} + + + org.springframework + spring-jdbc + ${spring.version} + + + org.springframework + spring-tx + ${spring.version} + + + org.springframework + spring-web + ${spring.version} + + + org.springframework + spring-webmvc + ${spring.version} + + + org.springframework + spring-context-support + ${spring.version} + + + org.springframework + spring-test + ${spring.version} + test + + + org.springframework.batch + spring-batch-test + 2.0.4.RELEASE + + + org.springframework.batch + spring-batch-core + 2.0.4.RELEASE + + + org.springframework.batch + spring-batch-infrastructure + 2.0.4.RELEASE + + + org.springframework + spring-orm + ${spring.version} + + + net.sf.ehcache + ehcache + 1.6.2 + + + org.springmodules + spring-modules-cache + 0.9 + + + p6spy + p6spy + 1.3 + + + javax.transaction + jta + 1.0.1B + + + javax.servlet + servlet-api + 2.5 + + + com.caucho + hessian + 3.1.6 + + + org.codehaus.jettison + jettison + 1.0 + + + com.thoughtworks.xstream + xstream + 1.3 + + + org.ini4j + ini4j + 0.5.1 + + + org.easymock + easymock + 2.4 + test + + + org.easymock + easymockclassextension + 2.4 + test + + + org.apache.commons + commons-compress + 1.0 + + + org.apache.commons + commons-math + 2.0 + + + dtgjpos_forms + dtgjpos_forms + 1.4.12 + provided + + + org.codehaus.castor + castor-xml + 1.3.1 + + + org.apache.xmlbeans + xmlbeans + 2.5.0 + + + org.apache.velocity.tools + velocity-tools-generic + 1.4 + + + org.apache.velocity + velocity + 1.6.2 + + + org.tmatesoft.svnkit + svnkit + 1.3.1 + + + - custom-annotations-implementation + custom-annotations-implementation - + + + + org.apache.maven.plugins + maven-compiler-plugin + 2.0.2 + + 1.6 + 1.6 + ${project.build.sourceEncoding} + + + + + org.apache.maven.plugins + maven-javadoc-plugin + 2.6 + + + org.apache.maven.plugins + maven-surefire-plugin + 2.4.2 + + true + + + + org.codehaus.mojo + jdepend-maven-plugin + 2.0-beta-2 + + + org.apache.maven.plugins + maven-jar-plugin + 2.3.1 + + + + ${project.description} + ${project.version} + ${project.organization.name} + ${project.description} + ${project.version} + ${project.organization.name} + ${BUILD_TAG} + ${BUILD_ID} + ${BUILD_NUMBER} + ${prefix.committedRevision} + ${prefix.repository} + ${prefix.path} + + + + + + com.google.code.maven-svn-revision-number-plugin + maven-svn-revision-number-plugin + 1.6 + + + + revision + + + + + + + prefix + + + + + + + + + + releases + releases + http://noserver/artifactory/custom-annotations-libs-releases-local + + + snapshots-releases + snapshots-releases + http://noserver/artifactory/custom-annotations-libs-snapshots-local + + + noserver + file:///mnt/sdb1/data/downloads/jenkins/ + + + + + + + org.apache.maven.plugins + maven-project-info-reports-plugin + 2.1.2 + + + + index + dependencies + cim + issue-tracking + scm + summary + project-team + + + + + + org.apache.maven.plugins + maven-javadoc-plugin + 2.6.1 + + + html + + MYPROJECT API for ${project.name} ${project.version} + MYPROJECT API for ${project.name} ${project.version} + + + javadoc + aggregate + + + + + + org.codehaus.mojo + taglist-maven-plugin + 2.3 + + + TODO + @todo + FIXME + XXX + + + + + org.apache.maven.plugins + maven-surefire-report-plugin + 2.4.3 + + + integration-tests + + report-only + + + failsafe-report + + target/failsafe-reports + + + + + junit-tests + + report-only + + + surefire-report + + target/surefire-reports + + + + + + + + + UTF-8 + 2.5.6 + + + + + + + + \ No newline at end of file -- 2.1.4