提交 19a14a55 编写于 作者: S studyingpanda

chen

上级 f5ccbe31
package com.imooc.springbootaop;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
/**
* 返回值切面
*/
@Component
@Aspect
public class ResultAspect {
// 切入点表达式,表示切入点为返回类型ResultBo的所有方法
@Pointcut("execution(public com.imooc.springbootaop.ResultBo *(..))")
public void ResultAspect() {
}
// 环绕通知
@Around("ResultAspect()")
public Object deAround(ProceedingJoinPoint joinPoint) throws Throwable {
try {
return joinPoint.proceed();// 返回正常结果
} catch (Exception ex) {
return new ResultBo<>(ex);// 被切入的方法执行异常时,返回ResultBo
}
}
}
package com.imooc.springbootaop;
public class ResultBo<T> {
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
/**
* 错误码 0表示没有错误(异常) 其他数字代表具体错误码
*/
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册