From 0aee12922ac97b4f843c9d4f1d0ef981805fb4d0 Mon Sep 17 00:00:00 2001 From: Gustavo Martin Morcuende Date: Sun, 17 Apr 2016 01:40:48 +0200 Subject: [PATCH] AOP aspectj-maven-plugin --- SpringJava/AOP/SpringMavenWeaver/pom.xml | 23 +++++++++------ .../src/main/java/de/spring/example/MyAdvice.java | 28 ------------------ .../annotation/beforeInitTransactional.java | 15 ++++++++++ .../de/spring/example/aspects/BeforeMyAspect.java | 21 ++++++++++++++ .../java/de/spring/example/aspects/MyAspect.java | 33 ++++++++++++++++++++++ .../de/spring/example/aspects/MyAspectsOrder.java | 11 ++++++++ .../src/main/java/de/spring/example/web/Test.java | 2 ++ .../src/main/resources/spring-config.xml | 20 ++++--------- 8 files changed, 102 insertions(+), 51 deletions(-) delete mode 100644 SpringJava/AOP/SpringMavenWeaver/src/main/java/de/spring/example/MyAdvice.java create mode 100644 SpringJava/AOP/SpringMavenWeaver/src/main/java/de/spring/example/annotation/beforeInitTransactional.java create mode 100644 SpringJava/AOP/SpringMavenWeaver/src/main/java/de/spring/example/aspects/BeforeMyAspect.java create mode 100644 SpringJava/AOP/SpringMavenWeaver/src/main/java/de/spring/example/aspects/MyAspect.java create mode 100644 SpringJava/AOP/SpringMavenWeaver/src/main/java/de/spring/example/aspects/MyAspectsOrder.java diff --git a/SpringJava/AOP/SpringMavenWeaver/pom.xml b/SpringJava/AOP/SpringMavenWeaver/pom.xml index 79f8576..1e3dac6 100644 --- a/SpringJava/AOP/SpringMavenWeaver/pom.xml +++ b/SpringJava/AOP/SpringMavenWeaver/pom.xml @@ -103,11 +103,11 @@ - + org.aspectj aspectjrt - 1.8.9 + 1.8.7 compile @@ -178,9 +178,6 @@ -115") --> default-compile - - ${project.build.directory}/unwoven-classes - @@ -199,16 +196,23 @@ aspectj-maven-plugin 1.8 + + 1.8 + 1.8 + ${project.build.sourceEncoding} + 1.8 + true - ${project.build.directory}/unwoven-classes + ${project.build.directory}/classes true src/main/java - de.spring.example.MyAdvice.java + **/aspects/*.java **/logging/*.aj @@ -219,6 +223,7 @@ + weave-classes process-classes compile @@ -239,8 +244,10 @@ true true - 1.5 + 1.8 + diff --git a/SpringJava/AOP/SpringMavenWeaver/src/main/java/de/spring/example/MyAdvice.java b/SpringJava/AOP/SpringMavenWeaver/src/main/java/de/spring/example/MyAdvice.java deleted file mode 100644 index 6e25974..0000000 --- a/SpringJava/AOP/SpringMavenWeaver/src/main/java/de/spring/example/MyAdvice.java +++ /dev/null @@ -1,28 +0,0 @@ -package de.spring.example; - -import org.aspectj.lang.annotation.After; -import org.aspectj.lang.annotation.Aspect; -import org.aspectj.lang.annotation.Before; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -@Aspect -public class MyAdvice { - private static final Logger LOGGER = LoggerFactory.getLogger(MyAdvice.class); - - // With execution we avoid double weaving (when call and when execution) - @Before("@annotation(de.spring.example.annotation.initTransactional) && execution(* *(..))") - public void initTransactional() - { - LOGGER.info("I am the Advice initTransaction."); - TransactionManager.getInstance().initTransaction(); - } - - - // With execution we avoid double weaving (when call and when execution) - @After("@annotation(de.spring.example.annotation.commitTransactional) && execution(* *(..))") - public void commitTransactional() { - LOGGER.info("I am the Advice commitTransaction."); - TransactionManager.getInstance().commitTransaction(); - } -} diff --git a/SpringJava/AOP/SpringMavenWeaver/src/main/java/de/spring/example/annotation/beforeInitTransactional.java b/SpringJava/AOP/SpringMavenWeaver/src/main/java/de/spring/example/annotation/beforeInitTransactional.java new file mode 100644 index 0000000..724f5a0 --- /dev/null +++ b/SpringJava/AOP/SpringMavenWeaver/src/main/java/de/spring/example/annotation/beforeInitTransactional.java @@ -0,0 +1,15 @@ +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; + +import org.springframework.stereotype.Component; + +@Target({ElementType.METHOD, ElementType.TYPE}) +@Retention(RetentionPolicy.RUNTIME) +@Component +public @interface beforeInitTransactional { + +} diff --git a/SpringJava/AOP/SpringMavenWeaver/src/main/java/de/spring/example/aspects/BeforeMyAspect.java b/SpringJava/AOP/SpringMavenWeaver/src/main/java/de/spring/example/aspects/BeforeMyAspect.java new file mode 100644 index 0000000..2ca1814 --- /dev/null +++ b/SpringJava/AOP/SpringMavenWeaver/src/main/java/de/spring/example/aspects/BeforeMyAspect.java @@ -0,0 +1,21 @@ +package de.spring.example.aspects; + + +import org.aspectj.lang.annotation.Aspect; +import org.aspectj.lang.annotation.Before; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +@Aspect +//Higher values has lower priority +//@Order(1) Just works when using Spring AOP proxies +//When weaving order is given by @DeclarePrecedence annotation, see: MyAspectsOrder +public class BeforeMyAspect { + private static final Logger LOGGER = LoggerFactory.getLogger(BeforeMyAspect.class); + + // With execution we avoid double weaving (when call and when execution) + @Before("@annotation(de.spring.example.annotation.beforeInitTransactional) && execution(* *(..))") + public void beforeInitTransactional() { + LOGGER.info("I am the Advice beforeInitTransaction."); + } +} diff --git a/SpringJava/AOP/SpringMavenWeaver/src/main/java/de/spring/example/aspects/MyAspect.java b/SpringJava/AOP/SpringMavenWeaver/src/main/java/de/spring/example/aspects/MyAspect.java new file mode 100644 index 0000000..742c483 --- /dev/null +++ b/SpringJava/AOP/SpringMavenWeaver/src/main/java/de/spring/example/aspects/MyAspect.java @@ -0,0 +1,33 @@ +package de.spring.example.aspects; + + +import org.aspectj.lang.annotation.After; +import org.aspectj.lang.annotation.Aspect; +import org.aspectj.lang.annotation.Before; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import de.spring.example.TransactionManager; + +@Aspect +// Higher values has lower priority +// @Order(2) Just works when using Spring AOP proxies +//When weaving order is given by @DeclarePrecedence annotation, see: MyAspectsOrder +public class MyAspect { + private static final Logger LOGGER = LoggerFactory.getLogger(MyAspect.class); + + // With execution we avoid double weaving (when call and when execution) + @Before("@annotation(de.spring.example.annotation.initTransactional) && execution(* *(..))") + public void initTransactional() { + LOGGER.info("I am the Advice initTransaction."); + TransactionManager.getInstance().initTransaction(); + } + + + // With execution we avoid double weaving (when call and when execution) + @After("@annotation(de.spring.example.annotation.commitTransactional) && execution(* *(..))") + public void commitTransactional() { + LOGGER.info("I am the Advice commitTransaction."); + TransactionManager.getInstance().commitTransaction(); + } +} diff --git a/SpringJava/AOP/SpringMavenWeaver/src/main/java/de/spring/example/aspects/MyAspectsOrder.java b/SpringJava/AOP/SpringMavenWeaver/src/main/java/de/spring/example/aspects/MyAspectsOrder.java new file mode 100644 index 0000000..6e67dd7 --- /dev/null +++ b/SpringJava/AOP/SpringMavenWeaver/src/main/java/de/spring/example/aspects/MyAspectsOrder.java @@ -0,0 +1,11 @@ +package de.spring.example.aspects; + +import org.aspectj.lang.annotation.Aspect; +import org.aspectj.lang.annotation.DeclarePrecedence; + +@Aspect +// When weaving order is given by @DeclarePreceden annotation +@DeclarePrecedence("BeforeMyAspect, MyAspect") +public class MyAspectsOrder { + +} diff --git a/SpringJava/AOP/SpringMavenWeaver/src/main/java/de/spring/example/web/Test.java b/SpringJava/AOP/SpringMavenWeaver/src/main/java/de/spring/example/web/Test.java index 5cd7d2a..4238a9f 100644 --- a/SpringJava/AOP/SpringMavenWeaver/src/main/java/de/spring/example/web/Test.java +++ b/SpringJava/AOP/SpringMavenWeaver/src/main/java/de/spring/example/web/Test.java @@ -3,6 +3,7 @@ package de.spring.example.web; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import de.spring.example.annotation.beforeInitTransactional; import de.spring.example.annotation.commitTransactional; import de.spring.example.annotation.initTransactional; @@ -33,6 +34,7 @@ public class Test { // IT WORKS WHEN WEAVING!!! @initTransactional + @beforeInitTransactional private void annotatedPrivateMethod() { LOGGER.info("The Advice should be run before even with private methods because I AM WEAVING." + " IT WORKS EVEN CALLING FROM METHOD OF THE SAME CLASS. It doesn't when using proxies AOP."); diff --git a/SpringJava/AOP/SpringMavenWeaver/src/main/resources/spring-config.xml b/SpringJava/AOP/SpringMavenWeaver/src/main/resources/spring-config.xml index ffb978c..03e2418 100644 --- a/SpringJava/AOP/SpringMavenWeaver/src/main/resources/spring-config.xml +++ b/SpringJava/AOP/SpringMavenWeaver/src/main/resources/spring-config.xml @@ -6,23 +6,13 @@ http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> - - - - - - + -- 2.1.4