自定义注解
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface TestAnnotation {
String op() default "eq";
String opValue() default "";
String column() default "";
}
Aop的实现
package com.ywhk.example.config;
import com.alibaba.fastjson.JSON;
import com.ywhk.base.commons.model.page.ICommonPage;
import com.ywhk.base.commons.model.query.condition.FullCondition;
import com.ywhk.base.commons.model.query.wrapper.PageQueryWrapper;
import com.ywhk.base.commons.model.update.UpdateItemParam;
import com.ywhk.example.annotation.TestAnnotation;
import com.ywhk.example.annotation.TestAnnotationBefore;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.List;
@Aspect
@Component
@Slf4j
public class TestAnnotationAop {
private final String ExpGetResultDataPonit = "@annotation(com.ywhk.example.annotation.TestAnnotation)";
private final String TestAnnotationBeforePoint = "@annotation(com.ywhk.example.annotation.TestAnnotationBefore)";
@Pointcut(value = ExpGetResultDataPonit)
void testAnnotationPointCut() {
}
@Before(value = TestAnnotationBeforePoint)
public void doBefore(JoinPoint joinPoint)throws Exception{
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
Method method = signature.getMethod();
Annotation[] annotations = method.getAnnotations();
TestAnnotationBefore testAnnotationBefore=null;
for (Annotation annotation : annotations) {
if(annotation instanceof TestAnnotationBefore){
testAnnotationBefore=method.getAnnotation(TestAnnotationBefore.class);
}
}
String op = testAnnotationBefore.op();
String opValue = testAnnotationBefore.opValue();
String queryColumn = testAnnotationBefore.queryColumn();
String updateColumn = testAnnotationBefore.updateColumn();
String updateValue = testAnnotationBefore.updateValue();
Object[] args = joinPoint.getArgs();
for (Object arg : args) {
if(arg instanceof UpdateItemParam){
UpdateItemParam updateItemParam= (UpdateItemParam)arg;
updateItemParam.setCompareField(queryColumn);
updateItemParam.setCompareValue(opValue);
updateItemParam.setFieldName(updateColumn);
updateItemParam.setValue(updateValue);
log.info("传递更新参数{}",JSON.toJSONString(updateItemParam));
}
}
}
private void getparameters(ProceedingJoinPoint pjp) throws Exception {
MethodSignature signature = (MethodSignature) pjp.getSignature();
Method method = signature.getMethod();
Annotation[] annotations = method.getAnnotations();
TestAnnotation testAnnotation=null;
for (Annotation annotation : annotations) {
if(annotation instanceof TestAnnotation){
testAnnotation=method.getAnnotation(TestAnnotation.class);
}
}
Object[] arguments = pjp.getArgs();
log.info("入参为{}", JSON.toJSONString(arguments));
for (Object argument : arguments) {
if(argument instanceof PageQueryWrapper){
FullCondition condition =new FullCondition(testAnnotation.column(),testAnnotation.opValue(),testAnnotation.op());
((PageQueryWrapper) argument).addConditionItem(condition);
}
}
log.info("入参为{}", JSON.toJSONString(arguments));
}
private void getReturn(ProceedingJoinPoint pjp ,Object keys) throws Exception{
MethodSignature signature = (MethodSignature) pjp.getSignature();
List keyList=(List)((ICommonPage)keys).records();
Method method = signature.getMethod();
keyList.forEach(e->{
Class<?> aClass = e.getClass();
try {
Field idNo = aClass.getDeclaredField("idNo");
idNo.setAccessible(true);
Object o = idNo.get(e);
String finalIdNo = o.toString().replaceAll("(\\w{6})\\w*(\\w{4})", "$1****$2");
idNo.set(e,finalIdNo);
log.info("idNo--{}",finalIdNo);
log.info("member--{}",e);
} catch (Exception ex) {
ex.printStackTrace();
}
});
log.info("结果为{}",JSON.toJSONString(keyList));
}
@Around(value = ExpGetResultDataPonit)
public void doThrowReturningAdvice1(ProceedingJoinPoint pjp)throws Exception {
log.info("前置通知");
getparameters(pjp);
long end = 0;
long start = System.currentTimeMillis();
try {
Object proceed = pjp.proceed();
end = System.currentTimeMillis();
getReturn(pjp,proceed);
log.info("后置通知");
} catch (Throwable throwable) {
log.info("异常通知");
} finally {
log.info("分页所需要的时间为{}毫秒", end - start);
log.info("最终通知");
}
}
}