Fork me on GitHub

Programming Design Notes

使用 AspectJ 記錄 Spring Bean 丟出的 Exception

| Comments

在上一篇也提過使用 AspectJ 可輕鬆地將大量 Method 前後插入程式碼,網址: AspectJ 記錄執行時間

今次這篇是使用 AspectJ 去記錄 Spring Bean 丟出的 Exception,一樣是非常簡單就可達成目的。

首先要先在設定 bean 的 xml 檔案加上以下內容:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

<aop:aspectj-autoproxy />
<context:component-scan base-package="com.ctlok.pro.bean.aspect" />

</beans>

製作一個 CommonLogger 的 class:
package com.ctlok.pro.bean.aspect;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import java.util.logging.Logger;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class CommonLogger {

private static final Logger logger = Logger.getLogger(CommonLogger.class.getName());

@Pointcut("execution(* com.ctlok.pro.bean.service.*.*(..))")
public void service() {
}

}

首先定義那些 bean method 要被記錄,在這裡是些有 com.ctlok.pro.bean.service 內的 class 和所有 method 都記錄起來。

再來是加入記錄程式碼:
@AfterThrowing(pointcut = "service()", throwing = "throwable")
public void exceptionLogger(JoinPoint joinPoint, Throwable throwable) {
String clazz = joinPoint.getTarget().getClass().getSimpleName();
String method = joinPoint.getSignature().getName();
String log = "Class: " + clazz + "run Method: " + method + " throw Exception: " + throwable.toString();
logger.warning(log);
}

@AfterThrowing 注解用來設定被記錄的 method 丟出 exception 後便執行。
throwing 用來設定 Throwable class 的名稱。

完整的 CommonLogger:
package com.ctlok.pro.bean.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import java.util.logging.Logger;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class CommonLogger {

private static final Logger logger = Logger.getLogger(CommonLogger.class
.getName());

@Pointcut("execution(* com.ctlok.pro.bean.service.*.*(..))")
public void service() {
}

@AfterThrowing(pointcut = "service()", throwing = "throwable")
public void exceptionLogger(JoinPoint joinPoint, Throwable throwable) {
String clazz = joinPoint.getTarget().getClass().getSimpleName();
String method = joinPoint.getSignature().getName();
String log = "Class: " + clazz + "run Method: " + method
+ " throw Exception: " + throwable.toString();
logger.warning(log);
}

}

相關書籍: Aspectj in Action: Enterprise AOP with Spring ApplicationsAspectJ in Action: Practical Aspect-Oriented ProgrammingEclipse AspectJ: Aspect-Oriented Programming with AspectJ and the Eclipse AspectJ Development Tools