@Before("handler(*) && args(e)")
public void logCaughtException(
JoinPoint thisJoinPoint,
JoinPoint.EnclosingStaticPart thisEnclosingJoinPointStaticPart,
Exception e
) {
// Exception handler
System.out.println(thisJoinPoint.getSignature() + " -> " + e);
// Method signature + parameter types/names
MethodSignature methodSignature = (MethodSignature) thisEnclosingJoinPointStaticPart.getSignature();
System.out.println(" " + methodSignature);
Class<?>[] paramTypes = methodSignature.getParameterTypes();
String[] paramNames = methodSignature.getParameterNames();
for (int i = 0; i < paramNames.length; i++)
System.out.println(" " + paramTypes[i].getName() + " " + paramNames[i]);
// Method annotations - attention, reflection!
Method method = methodSignature.getMethod();
for (Annotation annotation: method.getAnnotations())
System.out.println(" " + annotation);
}
:
package de.scrum_master.app;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
int id();
String name();
String remark();
}
package de.scrum_master.app;
public class Example {
@MyAnnotation(id = 11, name = "John", remark = "my best friend")
public void divideByZeroWithCatch(int dividend, String someText) {
try {
int a = 5/0;
} catch (ArithmeticException e) {
System.out.println("Can not divide by zero");
}
}
public void divideByZeroWithNoCatch() {
int b = 5/0;
}
public static void main(String[] args) {
Example e = new Example();
System.out.println("***** Calling method with catch block *****");
e.divideByZeroWithCatch(123, "Hello world!");
System.out.println("***** Calling method without catch block *****");
e.divideByZeroWithNoCatch();
}
}
を次にコンソールログは言う:
***** Calling method with catch block *****
catch(ArithmeticException) -> java.lang.ArithmeticException:/by zero
void de.scrum_master.app.Example.divideByZeroWithCatch(int, String)
int dividend
java.lang.String someText
@de.scrum_master.app.MyAnnotation(id=11, name=John, remark=my best friend)
Can not divide by zero
***** Calling method without catch block *****
execution(void de.scrum_master.app.Example.divideByZeroWithNoCatch()) -> java.lang.ArithmeticException:/by zero
execution(void de.scrum_master.app.Example.main(String[])) -> java.lang.ArithmeticException:/by zero
Exception in thread "main" java.lang.ArithmeticException:/by zero
at de.scrum_master.app.Example.divideByZeroWithNoCatch(Example.java:14)
at de.scrum_master.app.Example.main(Example.java:22)
それはあなたのために十分であるならば、あなたは大丈夫です。しかし、静的部分は完全な結合点ではないので、そこからパラメータ値にアクセスすることはできません。これを行うには、手帳を手作業で行う必要があります。そして、これはおそらく高価であり、アプリケーションを遅くする可能性があります。しかし、それは価値がある何のために、私はそれを行う方法をお見せ:
package de.scrum_master.aspect;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.reflect.MethodSignature;
@Aspect
public class LogAspect {
private ThreadLocal<JoinPoint> enclosingJoinPoint;
@AfterThrowing(value = "execution(* *(..))", throwing = "e")
public void log(JoinPoint thisJoinPoint, Throwable e) {
System.out.println(thisJoinPoint + " -> " + e);
}
@Before("execution(* *(..)) && within(de.scrum_master.app..*)")
public void recordJoinPoint(JoinPoint thisJoinPoint) {
if (enclosingJoinPoint == null)
enclosingJoinPoint = ThreadLocal.withInitial(() -> thisJoinPoint);
else
enclosingJoinPoint.set(thisJoinPoint);
}
@Before("handler(*) && args(e)")
public void logCaughtException(JoinPoint thisJoinPoint, Exception e) {
// Exception handler
System.out.println(thisJoinPoint + " -> " + e);
// Method signature + parameter types/names
JoinPoint enclosingJP = enclosingJoinPoint.get();
MethodSignature methodSignature = (MethodSignature) enclosingJP.getSignature();
System.out.println(" " + methodSignature);
Class<?>[] paramTypes = methodSignature.getParameterTypes();
String[] paramNames = methodSignature.getParameterNames();
Object[] paramValues = enclosingJP.getArgs();
for (int i = 0; i < paramNames.length; i++)
System.out.println(" " + paramTypes[i].getName() + " " + paramNames[i] + " = " + paramValues[i]);
// Target object upon which method is executed
System.out.println(" " + enclosingJP.getTarget());
// Method annotations - attention, reflection!
Method method = methodSignature.getMethod();
for (Annotation annotation: method.getAnnotations())
System.out.println(" " + annotation);
}
}
は、なぜ我々はジョインポイント簿記のためThreadLocal
メンバーが必要なのですか?明らかに、そうでなければマルチスレッドアプリケーションでは問題に陥るはずです。
今コンソールログは言う:
***** Calling method with catch block *****
handler(catch(ArithmeticException)) -> java.lang.ArithmeticException:/by zero
void de.scrum_master.app.Example.divideByZeroWithCatch(int, String)
int dividend = 123
java.lang.String someText = Hello world!
[email protected]
@de.scrum_master.app.MyAnnotation(id=11, name=John, remark=my best friend)
Can not divide by zero
***** Calling method without catch block *****
execution(void de.scrum_master.app.Example.divideByZeroWithNoCatch()) -> java.lang.ArithmeticException:/by zero
execution(void de.scrum_master.app.Example.main(String[])) -> java.lang.ArithmeticException:/by zero
Exception in thread "main" java.lang.ArithmeticException:/by zero
at de.scrum_master.app.Example.divideByZeroWithNoCatch(Example.java:14)
at de.scrum_master.app.Example.main(Example.java:22)
ああ、私はその余分なポイントカットについて知らなかった。それはAspectJのすべての実装で利用できますか?たとえば、私が知っている限り、SpringAspectJの実装では、[there(Spring-pointcut-designators)](https://docs.spring.io/spring/docs/current/)のように、ハンドラの使用を許可していません。 –
私は知っていますが、この質問はAspectJに関するものであり、Spring AOPに関するものではありません。後者はちょうど "AOP lite"であり、リンク先のマニュアルセクションには、AspectJが 'handler()'をサポートし、Spring AOPがサポートしていない灰色のボックス "その他のポイントカットタイプ"が記述されています。しかし、良いニュースは次のとおりです。また、Springがロードタイム織りで完全なAspectJを使用するように設定することもできます(https://docs.spring.io/spring/docs/current/spring-framework-reference/html/aop.html #aop-using-aspectj)を使用します。次に 'handler()'を使うこともできます。 – kriegaex
はい、私はすでに「完全なAspectJ」ではないことを明示的に指摘したいと思います。以前は春のアオの味が私にそこに噛み付いていただけのようです。 –