gpt4 book ai didi

java - 如何将基于注释的语法转换为原生 AspectJ 语法

转载 作者:行者123 更新时间:2023-12-02 13:27:09 24 4
gpt4 key购买 nike

我对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;
}
}

有人可以帮我解决这个问题吗?谢谢!

最佳答案

我编了一个小例子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

顺便说一句,如果您同意匿名切入点,则无需单独声明它们。你可以这样做:

具有匿名切入点的方面变体:

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);
}
}
}

关于java - 如何将基于注释的语法转换为原生 AspectJ 语法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43348989/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com