</exclusions>
</dependency>
- <!-- Using aspectj-maven-plugin with AJDT -->
+ <!-- Using aspectj-maven-plugin with AJDT, requires 1.8.7 version -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
- <version>1.8.9</version>
+ <version>1.8.7</version>
<scope>compile</scope>
</dependency>
-115") -->
<id>default-compile</id>
<configuration>
- <compilerArguments>
- <d>${project.build.directory}/unwoven-classes</d>
- </compilerArguments>
</configuration>
</execution>
</executions>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.8</version>
<configuration>
+ <!--
<ajdtBuildDefFile>build.ajproperties</ajdtBuildDefFile>
+ -->
+ <source>1.8</source>
+ <target>1.8</target>
+ <encoding>${project.build.sourceEncoding}</encoding>
+ <complianceLevel>1.8</complianceLevel>
+ <verbose>true</verbose>
<weaveDirectories>
- <weaveDirectory>${project.build.directory}/unwoven-classes</weaveDirectory>
+ <weaveDirectory>${project.build.directory}/classes</weaveDirectory>
</weaveDirectories>
<showWeaveInfo>true</showWeaveInfo>
<sources>
<source>
<basedir>src/main/java</basedir>
<includes>
- <include>de.spring.example.MyAdvice.java</include>
+ <include>**/aspects/*.java</include>
</includes>
<excludes>
<exclude>**/logging/*.aj</exclude>
<executions>
<execution>
<!-- Compile and weave aspects after all classes compiled by javac -->
+ <id>weave-classes</id>
<phase>process-classes</phase>
<goals>
<goal>compile</goal>
<configuration>
<verbose>true</verbose>
<privateScope>true</privateScope>
- <complianceLevel>1.5</complianceLevel>
+ <complianceLevel>1.8</complianceLevel>
+ <!--
<ajdtBuildDefFile>build.ajproperties</ajdtBuildDefFile>
+ -->
</configuration>
<reportSets>
<reportSet>
+++ /dev/null
-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();
- }
-}
--- /dev/null
+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 {
+
+}
--- /dev/null
+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.");
+ }
+}
--- /dev/null
+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();
+ }
+}
--- /dev/null
+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 {
+
+}
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;
// 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.");
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
-
- <!--
- We have to use SPRING ASPECTJ (no SPRING AOP 2.0/1.2) because we want to use
- annotations with inner classes.
- AspectJ under this configuration requires at least one 'META-INF/aop.xml' file
- with the configuration about the Advices.
-
- This switches on the load-time weaving
- See: http://static.springsource.org/spring/docs/3.1.0.M2/spring-framework-reference/html/aop.html#aop-aj-ltw-spring
- -->
- <context:load-time-weaver weaver-class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver"
- aspectj-weaving="autodetect"/>
- <bean id="myAdvice" class="de.spring.example.MyAdvice">
-
- </bean>
+ <!--
+ There is no need of declaring aspects in Spring xml files because I am using aspectj-maven-plugin
+ <bean id="myAdvice" class="de.spring.example.aspects.MyAspect" />
+ <bean id="beforeMyAdvice" class="de.spring.example.aspects.BeforeMyAspect" />
+ -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="user" value="root"/>