aop实现插入数据库

@AddPending(attribute = "2",maName="3")
public Response<Integer> queryProcessRouteInfo(@RequestBody VcsMouldReform param ) {
    Integer add = vcsMouldReformService.add(param);
    System.err.println("123=============================================");
    //int i = 2 / 0;

    return PageResponse.success(add);
}

import java.lang.annotation.*;

@Target({ElementType.METHOD,ElementType.LOCAL_VARIABLE,ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface AddPending {
    String attribute() default "";
    String maName() default "";
}

package com.zhuzaocloud.ms.form.aspect;


import com.alibaba.fastjson.JSONObject;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Objects;
import java.util.logging.Logger;

@Component
@Aspect
public class AddPendingAspect {

  final   String   shanghai="Asia/Shanghai";

    @Autowired
    private VcsInterfPendingService vcsInterfPendingService;

    /**
     * 在返回之前进行拦截
     */


    // 配置织入点
    @Pointcut("@annotation(com.zhuzaocloud.ms.form.annotation.AddPending)")
    public void convertUtcMet() {
    }

    //@Before("convertUtcMet()")
    //@After("convertUtcMet()")
    @AfterReturning(value = "convertUtcMet()",returning="resultVal")
    public void doAfterReturning(JoinPoint point ,Object resultVal) throws Throwable {
        handleDataScope(point,resultVal);
    }

    private void handleDataScope(JoinPoint point ,Object resultVal) throws Throwable{
        Object o = enumValue(point,resultVal);
    }


    public Object enumValue(JoinPoint point,Object resultVal)  {
        String declaringTypeName = point.getSignature().getDeclaringTypeName();//获注解所在类的类路径
        Object[] result = point.getArgs();
        // 空返回直接返回
        if (Objects.isNull(result)) {
            return  null;
        }
        Boolean sign=true;
        for (Object obj:result ) {
           if( obj instanceof VcsMouldReform ){
                setEnumValue(obj, obj.getClass(),declaringTypeName);
                sign=false;
            }
        }
        if(sign){
          throw  new RuntimeException("该注解的方法形参中缺少:com.zhuzaocloud.ms.form.entity.VcsMouldReform这个类的实体,导致插入数据失败");

        }
            return result;

    }

    /**
     * 开始根据注解,转换值
     *
     * @param parameterBean
     */
    private void setEnumValue(Object parameterBean, Class clazz,String parament) {
       Class aClass=null;
        try {
            aClass =  Class.forName(parament);
        Method[] methods = aClass.getMethods();
        for (Method method : methods) {
            AddPending annotation1 = method.getAnnotation(AddPending.class);
            if(null!=annotation1){
                String attribute = annotation1.attribute();
                VcsMouldReform vcsMouldReform=new VcsMouldReform();
                vcsMouldReform.setProductId("122");
                vcsMouldReform.setProductCode("122");
                JSONObject jsonObject=new JSONObject();
                jsonObject.put("productId","12");

                Object invoke = method.invoke(aClass.newInstance(),vcsMouldReform);
                System.err.println(invoke);
            }

        }
        } catch (Exception e) {
            Logger.getLogger("",e.getMessage());
            return;
        }

    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值