@File
2020-01-17T04:39:09.000000Z
字数 4499
阅读 143
java
<dependency>
<groupId>org.aspectj</groupId >
<artifactId>aspectjweaver</artifactId >
<version>1.8.7</version >
</dependency>
...待补全
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
<!-- beans 属性要引入 xmlns:aop -->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd "
xmlns:aop="http://www.springframework.org/schema/aop">
<!-- 开启注解aop -->
<!-- proxy-target-class:是否基于类代理,true依赖cglib -->
<aop:aspectj-autoproxy proxy-target-class="true"/>
</beans>
@Aspect
声明切面类
// 必须注册
@Component
@Aspect
public class Lidaye{}
@order
顺序权重@Pointcut
定义切入点表达式语法 | |
---|---|
execution | 用于匹配符合的方法 |
within | 用于匹配指定的类及其子类中的所有方法 |
this | 匹配可以向上转型为this指定的类型的代理对象中的所有方法 |
target | 匹配可以向上转型为target指定的类型的目标对象中的所有方法 |
args | 用于匹配运行时传入的参数列表的类型为指定的参数列表类型的方法 |
@annotation | 用于匹配持有指定注解的方法 |
@within | 用于匹配持有指定注解的类的所有方法 |
@target | 用于匹配持有指定注解的目标对象的所有方法 |
@args | 用于匹配运行时传入的参数列表的类型持有注解列表对应的注解的方法 |
通配符 | |
* | 匹配任何数量字符 |
… | 匹配任何数量字符的重复,如在类型模式中匹配任何数量子包;而在方法参数模式中匹配任何数量参数 |
+ | 匹配指定类型的子类型;仅能作为后缀放在类型模式后边 |
逻辑运算符 | |
&& | 且 |
|| | 或 |
! | 非 |
/**
* @param value 切入点表达式
* @param argNames 参数
*/
// value = "execution(返回值 包名.类名.方法名(参数))"
@Pointcut("execution(* com.lidaye.*.*(..))")
public void pointcut(){}
@Before
前置通知
/**
* @param value 切入点表达式或切入点方法带()
* @param argNames 参数
*/
@Before("pointcut()")
public void before(){}
@AfterReturning
后置通知
/**
* @param value 切入点表达式或切入点方法带()
* @param pointcut 切入点表达式,会覆盖value
* @param returning 返回值
* @param argNames 参数
*/
@AfterReturning("pointcut()")
public void afterReturn(){}
@Around
环绕通知
/**
* @param value 切入点表达式或切入点方法带()
* @param argNames 参数
*/
@Around("pointcut()")
public Object around(ProceedingJoinPoint pjp){
// 环绕前代码
// 取参数值数组
// Object[] args = pjp.getArgs();
// 取参数名数组
// String[] ageNames = ((CodeSignature) pjp.getSignature()).getParameterNames();
// 执行 前置通知-核心方法-后置通知
Object obj = pjp.proceed();
// 环绕后代码
return obj;
}
@AfterThrowing
异常通知
/**
* @param value 切入点表达式或切入点方法带()
* @param pointcut 切入点表达式,会覆盖value
* @param throwing 异常参数
* @param argNames 参数
*/
// 普通形式
@AfterThrowing("pointcut()")
public void afterThrowing(){}
// 获取异常形式
@AfterThrowing(value = "pointcut()", throwing = "exception")
public void afterThrowing(Exception e){}
@After
最终通知
/**
* @param value 切入点表达式或切入点方法带()
* @param argNames 参数
*/
@After("pointcut()")
public void after(){}
环绕前 -> 前置 -> 核心方法 -> 环绕后/异常 -> 最终 -> 后置
/**
* 结果对象状态码
* @author lidaye
*/
public interface ResultCode {
/**
* 成功
*/
int SUCCESS_CODE = 200;
/**
* 无权限
*/
int UNROOT_CODE = 401;
String UNROOT_MSG = "操作失败,无权限操作";
/**
* 资源请求失败
*/
int UNREAD_CODE = 403;
String UNREAD_MSG = "操作失败,资源请求失败";
/**
* 无请求对象
*/
int LOSS_CODE = 404;
String LOSS_MSG = "操作失败,资源请求丢失";
/**
* 失败
*/
int ERROR_CODE = 500;
String ERROR_MSG = "操作失败,服务器处理异常";
}
/**
* 结果对象
* @author lidaye
*/
@Data
public class Result implements Serializable {
private Integer code;
private Object data;
private String msg;
/**
* 成功返回
* @param res 内容
* @return 结果对象(SUCCESS_CODE)
*/
public static Result success(Object res) {
return newResult(res, ResultCode.SUCCESS_CODE,null);
}
/**
* 错误返回
* @param msg 错误信息
* @return 结果对象(ERROR_CODE)
*/
public static Result error(String msg) {
return newResult(null,ResultCode.ERROR_CODE,msg);
}
public static Result error() {
return error(ResultCode.ERROR_MSG);
}
/**
* 结果丢失
* @param msg 错误信息
* @return 结果对象(LOSS_CODE)
*/
public static Result loss(String msg) {
return newResult(null,ResultCode.LOSS_CODE,msg);
}
public static Result loss(){
return loss(ResultCode.LOSS_MSG);
}
/**
* 无权限
* @param msg 错误信息
* @return 结果对象(UNROOT_CODE)
*/
public static Result root(String msg) {
return newResult(null,ResultCode.UNROOT_CODE,msg);
}
public static Result root() {
return root(ResultCode.UNROOT_MSG);
}
/**
* 获取结果集对象
* @param res 结果内容
* @param code 结果状态码
* @param msg 提示信息
* @return 结果对象
*/
private static Result newResult(Object res, int code, String msg){
Result result = new Result();
result.setCode(code);
result.setData(res);
result.setMsg(msg);
return result;
}
}
@Component
@Aspect
public class ReturnAop {
// 配置影响的方法
@Pointcut("execution(* com.lidaye.pwd.*.controller.*.*(..))")
public void pointcut(){}
// 配置环绕
@Around("pointcut()")
public Result around(ProceedingJoinPoint pjp) {
try {
// 执行 前置通知-核心方法-后置通知
Object obj = pjp.proceed();
// 环绕后代码
return obj != null?
Result.success(obj):
Result.loss();
} catch (Throwable throwable) {
// 异常处理
return Result.error(throwable.getMessage());
}
}
}