</scm>
<dependencies>
<dependency>
- <groupId>com.sun.jdmk</groupId>
- <artifactId>jmxtools</artifactId>
- <version>1.2.1</version>
- </dependency>
- <dependency>
<groupId>javax.activation</groupId>
<artifactId>activation</artifactId>
<version>1.1</version>
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(de.spring.example.annotation.initTransactionalN2A)")
- public void initTransactionalN2A()
- {
- System.out.println("I am the Advice initTransaction.");
- TransactionManagerN2A.getInstance().initTransaction();
- }
-
- @After("@annotation(de.spring.example.annotation.commitTransactionalN2A)")
- public void commitTransactionalN2A() {
- System.out.println("I am the Advice commitTransaction.");
- TransactionManagerN2A.getInstance().commitTransaction();
- }
+
+ @Before("execution(* de.spring.example.web.Gus.prueba())")
+ public void initTransactional()
+ {
+ System.out.println("I am the Advice initTransaction.");
+ TransactionManager.getInstance().initTransaction();
+ }
+
+
+ @After("@annotation(de.spring.example.annotation.commitTransactional)")
+ public void commitTransactional() {
+ System.out.println("I am the Advice commitTransaction.");
+ TransactionManager.getInstance().commitTransaction();
+ }
}
--- /dev/null
+package de.spring.example;
+
+import org.aspectj.lang.annotation.Aspect;
+import org.springframework.jdbc.datasource.DataSourceTransactionManager;
+import org.springframework.transaction.TransactionStatus;
+
+@Aspect
+public class TransactionManager {
+ private static TransactionManager instance = new TransactionManager();
+ private DataSourceTransactionManager transactionManager;
+ private TransactionStatus transactionStatus;
+
+ //Why could you want to extend this class?
+ private TransactionManager() {
+ }
+
+ public static TransactionManager 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;
+ }
+}
+++ /dev/null
-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;
- }
-}
--- /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 commitTransactional {
+}
+++ /dev/null
-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 {
-}
--- /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 initTransactional {
+}
+++ /dev/null
-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 {
-}
--- /dev/null
+package de.spring.example.web;
+
+import de.spring.example.annotation.initTransactional;
+
+public class Gus {
+
+ @initTransactional
+ public void prueba() {
+ System.out.println("GUSOOK");
+ }
+}
package de.spring.example.web;
-import de.spring.example.annotation.commitTransactionalN2A;
-import de.spring.example.annotation.initTransactionalN2A;
+import de.spring.example.annotation.commitTransactional;
+import de.spring.example.annotation.initTransactional;
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. ");
- }
- }
+ @initTransactional
+ 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 {
+ @commitTransactional
+ public void innerMethod() {
+ System.out.println("I am the inner class. The Advice should be run after. ");
+ }
+ }
}
<property name="dataSource" ref="dataSource"/>
</bean>
- <bean id="transactionManagerN2A"
- class="de.spring.example.TransactionManagerN2A"
+ <bean id="myTransactionManager"
+ class="de.spring.example.TransactionManager"
factory-method="getInstance">
<property name="transactionManager">
<ref bean="transactionManager" />