勞爾貝爾託內幾乎是正確的,但並不完全。邏輯必須顛倒,ExceptionConversionAspect
必須是第一個優先。對Java SE
完全工作示例(我只是模仿了Java EE除外):
Helper類:
package javax.validation;
public class ConstraintViolationException extends RuntimeException {
private static final long serialVersionUID = -8041265519275356912L;
public ConstraintViolationException(String arg0) {
super(arg0);
}
}
package com.myexample.validator.annotation;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@Retention(RetentionPolicy.RUNTIME)
public @interface Validate {}
package com.myexample.service;
public class InvalidServiceInputException extends RuntimeException {
public InvalidServiceInputException(String arg0, Throwable arg1) {
super(arg0, arg1);
}
}
樣品驅動器應用:
驅動程序應用程序由@Validate
進行註釋並模擬服務 - 請參閱軟件包名稱。它通過10個方法調用進行循環,捕獲異常並將它們打印到標準輸出以顯示它們確實按需要進行了轉換。
package com.myexample.service;
import com.myexample.validator.annotation.Validate;
@Validate
public class Application {
public void doSomething(int i) {
System.out.printf("Doing something #%d%n", i);
}
public static void main(String[] args) {
Application application = new Application();
for (int i = 0; i < 10; i++) {
try {
application.doSomething(i + 1);
}
catch (Exception e) {
System.out.println(e);
System.out.println(" cause: " + e.getCause());
}
}
}
}
方面:
驗證方面隨機引發一個ConstraintViolationException
用於演示目的。
package com.myexample.aspect;
import java.util.Random;
import javax.validation.ConstraintViolationException;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
@Aspect
public class ValidateAspect {
private static final Random RANDOM = new Random();
@Around("execution(public !static * *(..)) && @within(com.myexample.validator.annotation.Validate)")
public Object validateMethodInvocation(ProceedingJoinPoint thisJoinPoint) throws Throwable {
Object result = thisJoinPoint.proceed();
if (RANDOM.nextBoolean())
throw new ConstraintViolationException("uh-oh");
return result;
}
}
現在異常轉換方面還有一個額外的@DeclarePrecedence
註釋。
package com.myexample.aspect;
import javax.validation.ConstraintViolationException;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.DeclarePrecedence;
import com.myexample.service.InvalidServiceInputException;
@Aspect
@DeclarePrecedence("ExceptionConversionAspect, *")
public class ExceptionConversionAspect {
@AfterThrowing(pointcut = "execution(* com.myexample.service..*(..))", throwing = "e")
public void convertServiceException(Exception e) {
if (e instanceof ConstraintViolationException) {
throw new InvalidServiceInputException("The service inputs failed validation", e);
}
}
}
控制檯輸出:
Doing something #1
Doing something #2
com.myexample.service.InvalidServiceInputException: The service inputs failed validation
cause: javax.validation.ConstraintViolationException: uh-oh
Doing something #3
com.myexample.service.InvalidServiceInputException: The service inputs failed validation
cause: javax.validation.ConstraintViolationException: uh-oh
Doing something #4
Doing something #5
Doing something #6
com.myexample.service.InvalidServiceInputException: The service inputs failed validation
cause: javax.validation.ConstraintViolationException: uh-oh
Doing something #7
Doing something #8
Doing something #9
Doing something #10
也許這將有助於爲validateMethodInvocation方法打造方面 - 它將在例外通過該方法拋出觸發或覆蓋ValidateAspect所以它拋出適當的異常? – 2012-04-20 13:49:53