From: gumartinm Date: Thu, 10 Nov 2011 22:45:44 +0000 (+0100) Subject: Weaving with ASPECTJ libraries. X-Git-Url: https://git.gumartinm.name/?a=commitdiff_plain;h=9a388eb46545802ac3933441e589b72e7eed504d;p=JavaForFun Weaving with ASPECTJ libraries. With this configuration we are not relying on the SPRING AOP libraries. So, I guess, this is the SPRING ASPECTJ configuration, the rest of them are SPRING AOP 2.0/1.2 with Aspects. --- diff --git a/SpringJava/SpringLTW/pom.xml b/SpringJava/SpringLTW/pom.xml new file mode 100644 index 0000000..75bab27 --- /dev/null +++ b/SpringJava/SpringLTW/pom.xml @@ -0,0 +1,663 @@ + + + + 4.0.0 + de.spring.example + spring-ltw + 2.0-SNAPSHOT + spring-ltw + http://www.myorganization.de + Spring AspectJ with Spring Libraries + + 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 + + + + + spring-ltw-example + + + + + 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 + + + + + + + + diff --git a/SpringJava/SpringLTW/spring-ltw-example/pom.xml b/SpringJava/SpringLTW/spring-ltw-example/pom.xml new file mode 100644 index 0000000..03f917f --- /dev/null +++ b/SpringJava/SpringLTW/spring-ltw-example/pom.xml @@ -0,0 +1,50 @@ + + + + + 4.0.0 + + spring-ltw + de.spring.example + 2.0-SNAPSHOT + + + spring-ltw-example + spring-ltw-example + http://maven.apache.org + + + + org.springframework + spring-aspects + 2.5.6 + + + org.springframework + spring-context + + + cglib + cglib-nodep + + + org.springframework + spring-aop + + + org.springframework + spring-jdbc + + + c3p0 + c3p0 + + + mysql + mysql-connector-java + + + diff --git a/SpringJava/SpringLTW/spring-ltw-example/src/main/java/de/spring/example/MyAdvice.java b/SpringJava/SpringLTW/spring-ltw-example/src/main/java/de/spring/example/MyAdvice.java new file mode 100644 index 0000000..5a5e2a3 --- /dev/null +++ b/SpringJava/SpringLTW/spring-ltw-example/src/main/java/de/spring/example/MyAdvice.java @@ -0,0 +1,25 @@ +package de.spring.example; + +import org.aspectj.lang.annotation.After; +import org.aspectj.lang.annotation.Aspect; +import org.aspectj.lang.annotation.Before; +import de.spring.example.TransactionManagerN2A; + +@Aspect +public class MyAdvice { + + + @Before("@annotation(es.dia.pos.n2a.spring.example.annotation.initTransactionalN2A)") + public void initTransactionalN2A() + { + System.out.println("I am the Advice initTransaction."); + TransactionManagerN2A.getInstance().initTransaction(); + } + + + @After("@annotation(es.dia.pos.n2a.spring.example.annotation.commitTransactionalN2A)") + public void commitTransactionalN2A() { + System.out.println("I am the Advice commitTransaction."); + TransactionManagerN2A.getInstance().commitTransaction(); + } +} diff --git a/SpringJava/SpringLTW/spring-ltw-example/src/main/java/de/spring/example/SpringContextLocator.java b/SpringJava/SpringLTW/spring-ltw-example/src/main/java/de/spring/example/SpringContextLocator.java new file mode 100644 index 0000000..2ece260 --- /dev/null +++ b/SpringJava/SpringLTW/spring-ltw-example/src/main/java/de/spring/example/SpringContextLocator.java @@ -0,0 +1,53 @@ +package de.spring.example; + +import org.springframework.context.ApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; + + +/** + * + */ +public final class SpringContextLocator { + + // Singleton Pattern + private static SpringContextLocator instance; + + // Spring ApplicationContext + private static ApplicationContext context; + + // Spring Context + private static final String SPRING_CONFIG_CONTEXT="/spring-config.xml"; + + + /** + * Private constructor. Singleton pattern. + */ + private SpringContextLocator() { + String[] factoryFiles = null; + System.out.println("Loading context files: " + SpringContextLocator.SPRING_CONFIG_CONTEXT); + + factoryFiles = new String[] { SPRING_CONFIG_CONTEXT }; + + SpringContextLocator.context = new ClassPathXmlApplicationContext(factoryFiles); + + System.out.println("The context has been loaded successfully!! "); + } + + /** + * 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 static SpringContextLocator getInstance() { + if (SpringContextLocator.instance == null) { + SpringContextLocator.instance = new SpringContextLocator(); + } + return SpringContextLocator.instance; + } + + /** + * Return bean from application context. + */ + public Object getBean(final String name) { + return SpringContextLocator.context.getBean(name); + } +} \ No newline at end of file diff --git a/SpringJava/SpringLTW/spring-ltw-example/src/main/java/de/spring/example/SpringStart.java b/SpringJava/SpringLTW/spring-ltw-example/src/main/java/de/spring/example/SpringStart.java new file mode 100644 index 0000000..a9da7cb --- /dev/null +++ b/SpringJava/SpringLTW/spring-ltw-example/src/main/java/de/spring/example/SpringStart.java @@ -0,0 +1,20 @@ +package de.spring.example; + +import de.spring.example.web.Test; + +public class SpringStart { + + /** + * @param args + */ + public static void main(String[] args) { + System.out.println("Starting application"); + SpringContextLocator.getInstance(); + + Test test = (Test) SpringContextLocator.getInstance().getBean("testOuter"); + test.myMethod(); + + Test.InnerTest testInner = (Test.InnerTest) SpringContextLocator.getInstance().getBean("testInner"); + testInner.innerMethod(); + } +} diff --git a/SpringJava/SpringLTW/spring-ltw-example/src/main/java/de/spring/example/TransactionManagerN2A.java b/SpringJava/SpringLTW/spring-ltw-example/src/main/java/de/spring/example/TransactionManagerN2A.java new file mode 100644 index 0000000..867b4bb --- /dev/null +++ b/SpringJava/SpringLTW/spring-ltw-example/src/main/java/de/spring/example/TransactionManagerN2A.java @@ -0,0 +1,44 @@ +package de.spring.example; + +import org.aspectj.lang.annotation.Aspect; +import org.springframework.jdbc.datasource.DataSourceTransactionManager; +import org.springframework.transaction.TransactionStatus; + +@Aspect +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() + { + System.out.println("initTRANSACTION"); + transactionStatus = this.transactionManager.getTransaction(null); + } + + public void rollbackTransaction() + { + this.transactionManager.rollback(this.transactionStatus); + } + + + public void commitTransaction() + { + System.out.println("commitTRANSACTION"); + this.transactionManager.commit(this.transactionStatus); + } + + + /************************* Setters and getters *******************************************/ + public void setTransactionManager(final DataSourceTransactionManager transactionManager) + { + this.transactionManager = transactionManager; + } +} diff --git a/SpringJava/SpringLTW/spring-ltw-example/src/main/java/de/spring/example/annotation/commitTransactionalN2A.java b/SpringJava/SpringLTW/spring-ltw-example/src/main/java/de/spring/example/annotation/commitTransactionalN2A.java new file mode 100644 index 0000000..950c319 --- /dev/null +++ b/SpringJava/SpringLTW/spring-ltw-example/src/main/java/de/spring/example/annotation/commitTransactionalN2A.java @@ -0,0 +1,13 @@ +package de.spring.example.annotation; + +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; +import java.lang.annotation.ElementType; +import org.springframework.stereotype.Component; + +@Target({ElementType.METHOD, ElementType.TYPE}) +@Retention(RetentionPolicy.RUNTIME) +@Component +public @interface commitTransactionalN2A { +} diff --git a/SpringJava/SpringLTW/spring-ltw-example/src/main/java/de/spring/example/annotation/initTransactionalN2A.java b/SpringJava/SpringLTW/spring-ltw-example/src/main/java/de/spring/example/annotation/initTransactionalN2A.java new file mode 100644 index 0000000..7996478 --- /dev/null +++ b/SpringJava/SpringLTW/spring-ltw-example/src/main/java/de/spring/example/annotation/initTransactionalN2A.java @@ -0,0 +1,13 @@ +package de.spring.example.annotation; + +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; +import java.lang.annotation.ElementType; +import org.springframework.stereotype.Component; + +@Target({ElementType.METHOD, ElementType.TYPE}) +@Retention(RetentionPolicy.RUNTIME) +@Component +public @interface initTransactionalN2A { +} diff --git a/SpringJava/SpringLTW/spring-ltw-example/src/main/java/de/spring/example/web/Test.java b/SpringJava/SpringLTW/spring-ltw-example/src/main/java/de/spring/example/web/Test.java new file mode 100644 index 0000000..d17d3ef --- /dev/null +++ b/SpringJava/SpringLTW/spring-ltw-example/src/main/java/de/spring/example/web/Test.java @@ -0,0 +1,23 @@ +package de.spring.example.web; + +import de.spring.example.annotation.commitTransactionalN2A; +import de.spring.example.annotation.initTransactionalN2A; + + +public class Test { + @initTransactionalN2A + public int myMethod() + { + System.out.println("The Advice should be run before."); + + //This value will be caught by the Advice with the @AfterReturning annotation. + return 666; + } + + public class InnerTest { + @commitTransactionalN2A + public void innerMethod() { + System.out.println("I am the inner class. The Advice should be run after. "); + } + } +} diff --git a/SpringJava/SpringLTW/spring-ltw-example/src/main/resources/META-INF/aop.xml b/SpringJava/SpringLTW/spring-ltw-example/src/main/resources/META-INF/aop.xml new file mode 100644 index 0000000..9f6ee4b --- /dev/null +++ b/SpringJava/SpringLTW/spring-ltw-example/src/main/resources/META-INF/aop.xml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + diff --git a/SpringJava/SpringLTW/spring-ltw-example/src/main/resources/spring-config.xml b/SpringJava/SpringLTW/spring-ltw-example/src/main/resources/spring-config.xml new file mode 100644 index 0000000..c6b5893 --- /dev/null +++ b/SpringJava/SpringLTW/spring-ltw-example/src/main/resources/spring-config.xml @@ -0,0 +1,64 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +