作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我对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/
我是一名优秀的程序员,十分优秀!