2014-07-17 148 views
2

在我的项目的处理我有这基本上是POJO,并且是坐在领域层顶部的春天控制器/服务层领域层。我也有一个坐在服务和域之间的AOP层。例外通过Spring AOP的+ AspectJ的

我的域层正在抛出现在正在服务层中处理的业务异常。

但是我想改变它,以便从外域层抛出将在AOP层进行处理。 AOP层会发生某种错误响应并将其发送回弹簧控制器/ Web服务层。

我或许可以创造一个IBizResponse,使两个子类/它的接口SuccessResponse和错误响应,使我的领域层方法返回IBizResponse。但是我无法弄清楚如何让AOP将ErrorResponse对象返回给服务层。

回答

5

参见投掷https://docs.spring.io/spring/docs/4.1.0.RELEASE/spring-framework-reference/htmlsingle/#aop-introduction-defn

建议部异常通知后经过运行时通过抛出异常匹配方法执行退出。它使用@AfterThrowing注解来声明:

例子

import org.aspectj.lang.annotation.Aspect; 
import org.aspectj.lang.annotation.AfterThrowing; 

@Aspect 
public class AfterThrowingExample { 

    @AfterThrowing("com.xyz.myapp.SystemArchitecture.dataAccessOperation()") 
    public void doRecoveryActions() { 
    // ... 
    } 

} 



import org.aspectj.lang.annotation.Aspect; 
import org.aspectj.lang.annotation.AfterThrowing; 

@Aspect 
public class AfterThrowingExample { 

    @AfterThrowing(
    pointcut="com.xyz.myapp.SystemArchitecture.dataAccessOperation()", 
    throwing="ex") 
    public void doRecoveryActions(DataAccessException ex) { 
     // ... 
    } 

} 
0

考虑com.sc.bs.impl *是业务/领域层封装,使用@Around注解拦截它AOP层。代码片段:

@Around("execution(* com.sc.bs.impl..*.*(..))") 
public Object exceptionHandlerWithReturnType(ProceedingJoinPoint joinPoint) throws Throwable{ 
    try { 
     obj = joinPoint.proceed(); 
    } catch(Exception ex) { 
     throw ex; 
    } 
} 
+0

你的尝试抓住是有点无用的不是吗? – rekire

+0

是的,但我用于审计和事务提交和回滚等其他目的。 –

0

我遇到了同样的情况,在任何异常处理的情况下,我必须返回错误响应DTO。里面@Aspect类,

@Aspect 
    @Component 
    public class MyAspect{ 

private static final Logger LOGGER = LoggerFactory.getLogger(MyAspect.class); 


@Pointcut("execution(* com.linda.dao.strategy.*.*(..))") 
public void strategyMethods() { } 


@Pointcut("execution(* com.linda.controller.*.*(..)) || execution(* com.linda.Manager.*(..))") 
public void controllerMethods(){ } 

@Around("strategyMethods()") 
public Object profileStrategyMethods(ProceedingJoinPoint pjp) throws Throwable { 

    long start = System.currentTimeMillis(); 
    Object output = null; 
    LOGGER.info("Class:"+pjp.getTarget().getClass()+" entry -> method ->"+pjp.getSignature().getName()); 
    try{ 
     output = pjp.proceed(); 
     long elapsedTime = System.currentTimeMillis() - start; 
     LOGGER.info("Method execution time: " + elapsedTime + " milliseconds."); 
     LOGGER.info("Class:"+pjp.getTarget().getClass()+" exit -> method ->"+pjp.getSignature().getName()); 
    }catch(Throwable t){ 
     throw new InternalServerException(t.getMessage()); 
    } 

    return output; 
} 


@AfterThrowing(pointcut="execution(* com.linda.dao.strategy.*.*(..)) || execution(* com.linda.controller.*.*(..)) || execution(* com.linda.Manager.*(..))",throwing = "ex") 
public void doRecoveryActions(JoinPoint joinPoint, Throwable ex) { 


     Signature signature = joinPoint.getSignature(); 
     String methodName = signature.getName(); 
     String stuff = signature.toString(); 
     String arguments = Arrays.toString(joinPoint.getArgs()); 
     LOGGER.error("Write something in the log... We have caught exception in method: " 
       + methodName + " with arguments " 
       + arguments + "\nand the full toString: " + stuff + "\nthe exception is: " 
       + ex.getMessage()); 
    } 

定义的另一个类的异常处理象下面这样:

@ControllerAdvice 

公共类ExceptionLogAdvice {

@ExceptionHandler(InternalServerException.class) 
@ResponseStatus(HttpStatus.BAD_GATEWAY) 
@ResponseBody 
public ResponseEntity<Object> handleValidationException(final InternalServerException internalServerException){ 

    ErrorResponseDTO dto = constructErrorResponse(internalServerException); 
    return ResponseEntity.status(HttpStatus.BAD_GATEWAY).body(dto); 
} 

}

啾啾的代码位的我无法分享实际的代码。希望我明确了这个概念。