概述
@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; } } }
最后
以上就是悦耳冬瓜为你收集整理的aop实现插入数据库的全部内容,希望文章能够帮你解决aop实现插入数据库所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复