緣由:專案內需要將Exception獨立抽出來處理,為了不影響原有的商業邏輯,並且讓code reuse 所以第一時間就想到要用AOP實作,順便回去練一下很久沒碰的Spring AOP,並複習一下觀念
問題:Exception處理
解決方式:Spring AOP Around Advice
好處: 1. 統一錯誤訊息,讓每個工程師各寫各的錯誤處理風格可以得到統一。 2. 讓其他人可以專注在商務邏輯 3. 程式看起來比較簡潔乾淨
基本上我不解釋觀念,因為網路上已經有很多很棒的解說了 我也還在學習,很多東西其實第一次看完全都不懂,很抽象, 但隨著時間、經驗的累積,又回去看了第二次第三次,就會越來越有感覺
spring-aop-diagram.jpg
In Spring AOP, 4 type of advices are supported :
Before advice – Run before the method execution
After returning advice – Run after the method returns a result
After throwing advice – Run after the method throws an exception
Around advice – Run around the method execution, combine all three advices above.
============================= 分隔線 =================================
我是使用 Around advice ,其實也就是把程式裡散落在各處的try catch集中至ExceptionAdvice統一處理。
build.gradle
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'maven'
compileJava.options.encoding = 'UTF-8'
def springVersion = "3.2.6.RELEASE"
dependencies {
compile 'org.aspectj:aspectjweaver:1.7.1'
compile 'org.slf4j:jcl-over-slf4j:1.7.1'
compile 'org.slf4j:slf4j-log4j12:1.7.1'
compile "org.springframework:spring-aop:$springVersion"
compile "org.springframework:spring-context:$springVersion"
}
applicationContext.xml
com.test.aop.advice.ExceptionAdvice
public class ExceptionAdvice {
private static Logger log = LoggerFactory.getLogger(ExceptionAdvice.class);
public Object exceptionHandler(ProceedingJoinPoint proceedingJoinPoint) {
Object value = null;
try {
value = proceedingJoinPoint.proceed();
} catch (Throwable e) {
log.info(proceedingJoinPoint.getSignature().getName() + " error");
String errorMsg = "內部錯誤:" + e.getMessage();
String errorCode = "500";
Map resultJsonMap = new HashMap();
resultJsonMap.put("successful", false);
resultJsonMap.put("errorCode", errorCode);
resultJsonMap.put("errorMsg", errorMsg );
value = resultJsonMap;
log.info(value.toString());
}
return value;
}
com.test.service.TestService
public class TestService {
private static ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
public String runTest() {
throw new IllegalArgumentException("這是測試");
}
public static void main(String[] args) {
TestService ts = ac.getBean("testService", TestService.class);
ts.runTest();
}
}
參考資料