自定义注解的使用

自定义注解

@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("最终通知");
        }
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值