如何将基于注释的转换为本机AspectJ语法

时间:2017-04-11 14:34:57

标签: java annotations aspectj

我对aspectj及其语法有些麻烦。我有一些写在java文件上的东西,我想将它翻译成.aj文件,因为我认为这更容易,但我找不到可以遵循的教程。

这是我的代码:

@Aspect
public class Aspect{

    @Pointcut("@annotation(annotationVariableName)")
    public void annotationPointCutDefinition(Annotation annotationVariableName){
    }

    @Pointcut("execution(* *(..))")
    public void atExecution(){}

    @Around("annotationPointCutDefinition(withTransactionVariableName) && atExecution()")
    public Object aroundAdvice(ProceedingJoinPoint joinPoint, Annotation annotationVariableName) throws Throwable {
        boolean parameter= annotationVariableName.parameter();
            Object returnObject = null;

            try {
                returnObject = joinPoint.proceed();
            } catch (Throwable throwable) {
                throw throwable;
            }
            return returnObject;
    }
}

任何人都可以帮我吗?谢谢!

2 个答案:

答案 0 :(得分:1)

我编写了一个与您对事务管理的评论相关的示例MCVE,以便使代码及其日志输出更加清晰:

<强>注释:

package de.scrum_master.app;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Transaction {
  boolean myFlag();
}

驱动程序应用程序:

请注意,两种方法都带有注释,一种没有。

package de.scrum_master.app;

public class Application {
  public static void main(String[] args) {
    Application application = new Application();
    application.doSomething();
    application.doSomethingElse();
    application.doSomethingWeird();
  }

  @Transaction(myFlag = true)
  public void doSomething() {
    System.out.println("Doing something");
  }

  public void doSomethingElse() {
    System.out.println("Doing something else\n");
  }

  @Transaction(myFlag = false)
  public void doSomethingWeird() {
    System.out.println("Doing something weird");
    throw new RuntimeException("oops");
  }
}

<强>方面:

package de.scrum_master.aspect;

import org.aspectj.lang.SoftException;

import de.scrum_master.app.Transaction;

public aspect TransactionAspect {
  pointcut hasAnnotation(Transaction myAnnotation) : @annotation(myAnnotation);

  pointcut methodExecution() : execution(* *(..));

  Object around(Transaction myAnnotation) : methodExecution() && hasAnnotation(myAnnotation) {
    System.out.println(thisJoinPoint + " -> " + myAnnotation);
    boolean parameter = myAnnotation.myFlag();
    System.out.println("Transaction start");
    try {
      Object result = proceed(myAnnotation);
      System.out.println("Transaction commit\n");
      return result;
    } catch (Exception e) {
      System.out.println("Transaction roll-back\n");
      // Native AspectJ advices must not return checked exceptions, only runtime exceptions.
      // So we soften the caught exception, just in case.
      throw new SoftException(e);
    }
  }
}

控制台日志:

execution(void de.scrum_master.app.Application.doSomething()) -> @de.scrum_master.app.Transaction(myFlag=true)
Transaction start
Doing something
Transaction commit

Doing something else

execution(void de.scrum_master.app.Application.doSomethingWeird()) -> @de.scrum_master.app.Transaction(myFlag=false)
Transaction start
Doing something weird
Transaction roll-back

Exception in thread "main" org.aspectj.lang.SoftException
    at de.scrum_master.app.Application.doSomethingWeird_aroundBody3$advice(Application.java:22)
    at de.scrum_master.app.Application.doSomethingWeird(Application.java:1)
    at de.scrum_master.app.Application.main(Application.java:8)
Caused by: java.lang.RuntimeException: oops
    at de.scrum_master.app.Application.doSomethingWeird_aroundBody2(Application.java:23)
    at de.scrum_master.app.Application.doSomethingWeird_aroundBody3$advice(Application.java:17)
    ... 2 more

顺便说一下,如果您对匿名切入点没问题,则无需单独声明它们。你可以这样做:

具有匿名切入点的Aspect变体:

package de.scrum_master.aspect;

import org.aspectj.lang.SoftException;

import de.scrum_master.app.Transaction;

public aspect TransactionAspect {
  Object around(Transaction myAnnotation) : execution(* *(..)) && @annotation(myAnnotation) {
    System.out.println(thisJoinPoint + " -> " + myAnnotation);
    boolean parameter = myAnnotation.myFlag();
    System.out.println("Transaction start");
    try {
      Object result = proceed(myAnnotation);
      System.out.println("Transaction commit\n");
      return result;
    } catch (Exception e) {
      System.out.println("Transaction roll-back\n");
      // Native AspectJ advices must not return checked exceptions, only runtime exceptions.
      // So we soften the caught exception, just in case.
      throw new SoftException(e);
    }
  }
}

答案 1 :(得分:0)

就个人而言,我发现以下程序员指南对我自己很有用,虽然它不是真正的教程:https://eclipse.org/aspectj/doc/next/progguide/index.html。点击切入点以获得转换您的切入点的基础知识,该页面也提供了建议,尽管它没有详细说明“周围”建议,但在生产方面有一个例子

快速搜索教程会引发以下内容(我没有使用过这个): http://o7planning.org/en/10257/java-aspect-oriented-programming-tutorial-with-aspectj