springboot注解Aspect的实现方案是什么

springboot注解Aspect的实现方案是什么

这篇文章跟大家分析一下“springboot注解Aspect的实现方案是什么”。内容详细易懂,对“springboot注解Aspect的实现方案是什么”感兴趣的朋友可以跟着小编的思路慢慢深入来阅读一下,希望阅读后能够对大家有所帮助。下面跟着小编一起深入学习“springboot注解Aspect的实现方案是什么”的知识吧。

    目标

    下面提供一种自定义注解,来实现业务审批操作的DEMO,不包含审批流程的配置功能。
    具体方案是
    自定义一个Aspect注解,拦截sevice方法,将拦截的信息持久化,待审批;审批时获取持久化数据,执行目标方法。

    springboot注解Aspect的实现方案是什么

    实现

    POM

    <?xmlversion="1.0"encoding="UTF-8"?><projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.5.8</version><relativePath/><!--lookupparentfromrepository--></parent><groupId>com.proc</groupId><artifactId>process-test</artifactId><version>1.0.0-SNAPSHOT</version><name>process-test</name><description>DemoprojectforSpringBoot</description><properties><java.version>1.8</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-aop</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-configuration-processor</artifactId><optional>true</optional></dependency><!--用于主线程给子线程传递数据,支持线程池--><dependency><groupId>com.alibaba</groupId><artifactId>transmittable-thread-local</artifactId><version>2.12.2</version></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>

    一些实体类

    CheckedParam

    用于包装页面传进来的参数

    packagecom.proc.model;importjava.util.List;publicclassCheckedParam{//业务标记,由页面传入,用于审批时页面根据tagPageJs解析data,渲染到页面,审批管理员可看到审批的内容privateStringtagPageJs;//页面传入的原始数据privateList<String>data;publicStringgetTagPageJs(){returntagPageJs;}publicvoidsetTagPageJs(StringtagPageJs){this.tagPageJs=tagPageJs;}publicList<String>getData(){returndata;}publicvoidsetData(List<String>data){this.data=data;}}

    ProcessDbModel

    拦截的信息包装类,用于持久化数据

    packagecom.proc.model;publicclassProcessDbModel{//bean的目标类全限定名privateStringtargetClassName;//拦截到的service方法名privateStringmethodName;//页面传入的tagPageJs或Checked注解的tagprivateStringtag;privateStringdescription;//拦截到的service入参类型,包含泛型信息privateStringparamTypes;//拦截到的service入参值privateStringparamArgs;//拦截到的service入参值或页面传入的原始数据privateStringdata;publicStringgetTargetClassName(){returntargetClassName;}publicvoidsetTargetClassName(StringtargetClassName){this.targetClassName=targetClassName;}publicStringgetMethodName(){returnmethodName;}publicvoidsetMethodName(StringmethodName){this.methodName=methodName;}publicStringgetTag(){returntag;}publicStringgetDescription(){returndescription;}publicvoidsetDescription(Stringdescription){this.description=description;}publicvoidsetTag(Stringtag){this.tag=tag;}publicStringgetParamTypes(){returnparamTypes;}publicvoidsetParamTypes(StringparamTypes){this.paramTypes=paramTypes;}publicStringgetParamArgs(){returnparamArgs;}publicvoidsetParamArgs(StringparamArgs){this.paramArgs=paramArgs;}publicStringgetData(){returndata;}publicvoidsetData(Stringdata){this.data=data;}@OverridepublicStringtoString(){return"ProcessDbModel[targetClassName="+targetClassName+",methodName="+methodName+",tag="+tag+",description="+description+",paramTypes="+paramTypes+",paramArgs="+paramArgs+",data="+data+"]";}}

    测试用的入参对象

    packagecom.proc.model;importjava.math.BigDecimal;publicclassScore{privateBigDecimallangue;privateBigDecimalmath;privateBigDecimalenglish;publicBigDecimalgetLangue(){returnlangue;}publicvoidsetLangue(BigDecimallangue){this.langue=langue;}publicBigDecimalgetMath(){returnmath;}publicvoidsetMath(BigDecimalmath){this.math=math;}publicBigDecimalgetEnglish(){returnenglish;}publicvoidsetEnglish(BigDecimalenglish){this.english=english;}@OverridepublicStringtoString(){return"Score[langue="+langue+",math="+math+",english="+english+"]";}}

    packagecom.proc.model;importjava.util.List;publicclassPerson<T>{privateStringname;privateStringage;privateStringsex;privateStringtestName;privateStringsalary;privateStringwork;privateList<T>grades;publicStringgetName(){returnname;}publicvoidsetName(Stringname){this.name=name;}publicStringgetAge(){returnage;}publicvoidsetAge(Stringage){this.age=age;}publicStringgetSex(){returnsex;}publicvoidsetSex(Stringsex){this.sex=sex;}publicStringgetSalary(){returnsalary;}publicvoidsetSalary(Stringsalary){this.salary=salary;}publicStringgetTestName(){returntestName;}publicvoidsetTestName(StringtestName){this.testName=testName;}publicStringgetWork(){returnwork;}publicvoidsetWork(Stringwork){this.work=work;}publicList<T>getGrades(){returngrades;}publicvoidsetGrades(List<T>grades){this.grades=grades;}@OverridepublicStringtoString(){return"Person[name="+name+",age="+age+",sex="+sex+",testName="+testName+",salary="+salary+",work="+work+",grades="+grades+"]";}}

    一些工具类

    JacksonCanonicalUtil

    packagecom.proc.util;importcom.fasterxml.jackson.core.type.TypeReference;importcom.fasterxml.jackson.databind.JavaType;importcom.fasterxml.jackson.databind.json.JsonMapper;publicclassJacksonCanonicalUtil{privatestaticfinalJsonMapperMAPPER=newJsonMapper();privateJacksonCanonicalUtil(){}publicstatic<T>StringtoCanonical(Class<T>clazz){returnMAPPER.getTypeFactory().constructType(clazz).toCanonical();}publicstatic<T>StringtoCanonical(TypeReference<T>tr){returnMAPPER.getTypeFactory().constructType(tr).toCanonical();}//反序列化时从持久数据中获取JavaTypepublicstaticJavaTypeconstructFromCanonical(Stringcanonical){returnMAPPER.getTypeFactory().constructFromCanonical(canonical);}}

    StringZipUtil

    用于压缩和解压字符串,减少持久数据占用空间

    packagecom.proc.util;importjava.io.ByteArrayOutputStream;importjava.io.OutputStream;importjava.nio.charset.StandardCharsets;importjava.util.Base64;importjava.util.zip.DeflaterOutputStream;importjava.util.zip.InflaterOutputStream;publicclassStringZipUtil{privateStringZipUtil(){}publicstaticStringzipBase64(Stringtext){try(ByteArrayOutputStreamout=newByteArrayOutputStream()){try(OutputStreamos=newDeflaterOutputStream(out)){os.write(text.getBytes(StandardCharsets.UTF_8));}returnBase64.getEncoder().encodeToString(out.toByteArray());}catch(Exceptione){thrownewRuntimeException("压缩字符串出错",e);}}publicstaticStringunzipBase64(Stringtext){try(ByteArrayOutputStreamout=newByteArrayOutputStream()){try(OutputStreamos=newInflaterOutputStream(out)){os.write(Base64.getDecoder().decode(text));}returnnewString(out.toByteArray(),StandardCharsets.UTF_8);}catch(Exceptione){thrownewRuntimeException("解压字符串出错",e);}}}

    Base64Util

    一些参数值转为Base64后持久化

    packagecom.proc.util;importjava.nio.charset.StandardCharsets;importjava.util.ArrayList;importjava.util.Base64;importjava.util.List;importjava.util.stream.Collectors;importcom.fasterxml.jackson.databind.json.JsonMapper;publicclassBase64Util{privateBase64Util(){}privatestaticfinalJsonMapperMAPPER=newJsonMapper();publicstaticString[]toStrings(Object[]objs){List<String>list=newArrayList<>();try{for(Objectobj:objs){list.add(MAPPER.writeValueAsString(obj));}}catch(Exceptione){thrownewRuntimeException("序列化对象出错",e);}returnlist.toArray(newString[0]);}publicstaticStringencode(String[]strs){List<String>list=newArrayList<>();for(Stringstr:strs){list.add(Base64.getEncoder().encodeToString(str.getBytes(StandardCharsets.UTF_8)));}Stringjoin=list.stream().collect(Collectors.joining("|"));returnjoin;}publicstaticString[]decode(Stringtext){String[]strs=text.split("\\|",-1);List<String>list=newArrayList<>();for(Stringbase64:strs){list.add(newString(Base64.getDecoder().decode(base64),StandardCharsets.UTF_8));}returnlist.toArray(newString[0]);}publicstaticStringencodeZip(Object[]objs){returnencodeZip(toStrings(objs));}publicstaticStringencodeZip(String[]strs){List<String>list=newArrayList<>();for(Stringstr:strs){list.add(StringZipUtil.zipBase64(str));}Stringjoin=list.stream().collect(Collectors.joining("|"));returnStringZipUtil.zipBase64(join);}publicstaticString[]decodeZip(Stringtext){Stringstr=StringZipUtil.unzipBase64(text);String[]strs=str.split("\\|",-1);List<String>list=newArrayList<>();for(Stringbase64:strs){list.add(StringZipUtil.unzipBase64(base64));}returnlist.toArray(newString[0]);}}

    SpringBootBeanUtil

    packagecom.proc.util;importjava.util.Map;importorg.springframework.beans.BeansException;importorg.springframework.context.ApplicationContext;importorg.springframework.context.ApplicationContextAware;importorg.springframework.stereotype.Component;@ComponentpublicclassSpringBootBeanUtilimplementsApplicationContextAware{privatestaticApplicationContextapplicationContext;@OverridepublicvoidsetApplicationContext(ApplicationContextapplicationContext)throwsBeansException{SpringBootBeanUtil.applicationContext=applicationContext;}publicstaticApplicationContextgetApplicationContext(){returnapplicationContext;}publicstaticObjectgetBean(Stringname){returnapplicationContext.getBean(name);}publicstatic<T>TgetBean(Class<T>clazz){return(T)applicationContext.getBean(clazz);}publicstatic<T>TgetBean(Stringname,Class<T>clazz){returnapplicationContext.getBean(name,clazz);}publicstatic<T>Map<String,T>getBeansOfType(Class<T>clazz){returnapplicationContext.getBeansOfType(clazz);}}

    ProcessBeanUtil

    用于执行目标方法

    packagecom.proc.util;importjava.lang.reflect.Method;importorg.springframework.util.ReflectionUtils;publicclassProcessBeanUtil{privateProcessBeanUtil(){}publicstaticObjectexcuteBeanMethod(StringtargetClassName,StringmethodName,Class<?>[]parameterTypes,Object[]args){Class<?>targetClass;try{targetClass=Class.forName(targetClassName);}catch(ClassNotFoundExceptione){thrownewRuntimeException("未找到类",e);}returnexcuteBeanMethod(targetClass,methodName,parameterTypes,args);}publicstaticObjectexcuteBeanMethod(Class<?>targetClass,StringmethodName,Class<?>[]parameterTypes,Object[]args){Objectbean=SpringBootBeanUtil.getBean(targetClass);Methodmethod=ReflectionUtils.findMethod(targetClass,methodName,parameterTypes);returnReflectionUtils.invokeMethod(method,bean,args);}}

    CheckedTransmitableUtil

    用于传递业务参数

    packagecom.proc.util;importcom.alibaba.ttl.TransmittableThreadLocal;importcom.proc.model.CheckedParam;publicclassCheckedTransmitableUtil{privatestaticfinalTransmittableThreadLocal<CheckedParam>threadLocal=newTransmittableThreadLocal<>();privateCheckedTransmitableUtil(){}publicstaticvoidset(CheckedParamcheckedParam){threadLocal.set(checkedParam);}publicstaticCheckedParamgetAndRemove(){CheckedParamcheckedParam=threadLocal.get();threadLocal.remove();returncheckedParam;}}

    PrivateTransmitableUtil

    为Aspect判断是否拦截提供依据

    packagecom.proc.util;importcom.alibaba.ttl.TransmittableThreadLocal;publicclassPrivateTransmitableUtil{privatestaticfinalStringCHECKED="__CHECKED__";privatestaticfinalTransmittableThreadLocal<String>threadLocal=newTransmittableThreadLocal<>();privatePrivateTransmitableUtil(){}publicstaticvoidset(){threadLocal.set(CHECKED);}//是否执行的审批程序publicstaticbooleanisCheck(){Stringchecked=threadLocal.get();threadLocal.remove();returnCHECKED.equals(checked);}}

    一些Bean

    PostProcess

    用于拦截方法后做的个性处理

    packagecom.proc.bean;publicinterfacePostProcess<T>{//返回说明内容,审批时在页面显示Stringdescription(Stringtag,Class<?>[]parameterTypes,Object[]args);//返回代替的返回值TretObject(Stringtag,Class<?>[]parameterTypes,Object[]args);}

    TestCheckPostProcess

    测试用

    packagecom.proc.bean;importorg.springframework.stereotype.Component;@ComponentpublicclassTestCheckPostProcessimplementsPostProcess<String>{@OverridepublicStringdescription(Stringtag,Class<?>[]parameterTypes,Object[]args){returntag+"测试testCheck";}@OverridepublicStringretObject(Stringtag,Class<?>[]parameterTypes,Object[]args){returntag+"返回拦截响应";}}

    Aspect注解

    packagecom.proc.config;importstaticjava.lang.annotation.ElementType.METHOD;importstaticjava.lang.annotation.RetentionPolicy.RUNTIME;importjava.lang.annotation.Retention;importjava.lang.annotation.Target;importcom.proc.bean.PostProcess;@Retention(RUNTIME)@Target(METHOD)public@interfaceChecked{Stringtag()default"";/***@seecom.proc.util.JacksonCanonicalUtil*@return*/String[]paramCanonical();Class<?extendsPostProcess<?>>postProcess();}

    切面类 CheckedAop

    packagecom.proc.config;importorg.aspectj.lang.ProceedingJoinPoint;importorg.aspectj.lang.annotation.Around;importorg.aspectj.lang.annotation.Aspect;importorg.aspectj.lang.annotation.Pointcut;importorg.aspectj.lang.reflect.MethodSignature;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.stereotype.Component;importcom.proc.bean.PostProcess;importcom.proc.model.CheckedParam;importcom.proc.model.ProcessDbModel;importcom.proc.service.ProcessDbService;importcom.proc.util.Base64Util;importcom.proc.util.CheckedTransmitableUtil;importcom.proc.util.PrivateTransmitableUtil;importcom.proc.util.SpringBootBeanUtil;@Component@AspectpublicclassCheckedAop{@AutowiredprivateProcessDbServiceprocessDbService;//拦截Checked注释的方法@Pointcut("@annotation(com.proc.config.Checked)")publicvoidcheck(){}@Around(value="com.proc.config.CheckedAop.check()&&@annotation(checked)")publicObjectaround(ProceedingJoinPointjoinPoint,Checkedchecked)throwsThrowable{MethodSignaturesignature=(MethodSignature)joinPoint.getSignature();Class<?>[]parameterTypes=signature.getParameterTypes();StringmethodName=signature.getMethod().getName();Object[]args=joinPoint.getArgs();if(PrivateTransmitableUtil.isCheck()){//审批后,执行业务代码ObjectreturnVal=joinPoint.proceed();returnreturnVal;}else{//不是审批操作,拦截Class<?extendsPostProcess<?>>postProcess=checked.postProcess();PostProcess<?>bean=SpringBootBeanUtil.getBean(postProcess);//组装持久化数据ProcessDbModeldbModel=newProcessDbModel();dbModel.setTargetClassName(joinPoint.getTarget().getClass().getName());dbModel.setMethodName(methodName);Stringtag=checked.tag();CheckedParamcheckedParam=CheckedTransmitableUtil.getAndRemove();if(checkedParam==null||checkedParam.getTagPageJs()==null||checkedParam.getTagPageJs().isEmpty()){//不是页面调用的业务,使用注解的tag,data保存为service的参数,这时需要页面专门解析渲染String[]argStrs=Base64Util.toStrings(args);dbModel.setParamArgs(Base64Util.encodeZip(argStrs));dbModel.setData(Base64Util.encode(argStrs));}else{tag=checkedParam.getTagPageJs();dbModel.setParamArgs(Base64Util.encodeZip(args));dbModel.setData(Base64Util.encode(checkedParam.getData().toArray(newString[0])));}dbModel.setTag(tag);dbModel.setParamTypes(Base64Util.encodeZip(checked.paramCanonical()));dbModel.setDescription(bean.description(tag,parameterTypes,args));//持久化数据processDbService.save(dbModel);returnbean.retObject(tag,parameterTypes,args);}}}

    线程池配置

    测试用

    packagecom.proc.config;importjava.util.concurrent.Executor;importjava.util.concurrent.ThreadPoolExecutor;importorg.springframework.context.annotation.Bean;importorg.springframework.context.annotation.Configuration;importorg.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;importcom.alibaba.ttl.threadpool.TtlExecutors;@ConfigurationpublicclassTaskExecutePoolConfig{@BeanpublicExecutorprocessExecutor(){ThreadPoolTaskExecutorexecutor=newThreadPoolTaskExecutor();//核心线程池大小executor.setCorePoolSize(10);//最大线程数executor.setMaxPoolSize(10);//队列容量executor.setQueueCapacity(500);//活跃时间executor.setKeepAliveSeconds(60);//线程名字前缀executor.setThreadNamePrefix("ProcessExecutor-");executor.setRejectedExecutionHandler(newThreadPoolExecutor.AbortPolicy());//等待所有任务结束后再关闭线程池executor.setWaitForTasksToCompleteOnShutdown(true);executor.initialize();//用transmittable-thread-local包装,才可以正确给线程池中的线程传递数据returnTtlExecutors.getTtlExecutor(executor);}}

    持久化service

    为测试方便,未真正实现持久化

    packagecom.proc.service;importcom.proc.model.ProcessDbModel;publicinterfaceProcessDbService{voidsave(ProcessDbModelmodel);ProcessDbModelget();}

    packagecom.proc.service.impl;importorg.slf4j.Logger;importorg.slf4j.LoggerFactory;importorg.springframework.stereotype.Component;importcom.proc.model.ProcessDbModel;importcom.proc.service.ProcessDbService;@ComponentpublicclassProcessDbServiceImplimplementsProcessDbService{privatestaticfinalLoggerlog=LoggerFactory.getLogger(ProcessDbService.class);privatevolatileProcessDbModelmodel;@Overridepublicvoidsave(ProcessDbModelmodel){this.model=model;log.info(model.toString());}@OverridepublicProcessDbModelget(){returnthis.model;}}

    审批用的service

    packagecom.proc.service;importcom.proc.model.ProcessDbModel;publicinterfaceProcessCheckService{voidprocess(ProcessDbModelmodel);}

    packagecom.proc.service.impl;importjava.util.ArrayList;importjava.util.List;importjava.util.Objects;importorg.slf4j.Logger;importorg.slf4j.LoggerFactory;importorg.springframework.stereotype.Service;importcom.fasterxml.jackson.core.JsonProcessingException;importcom.fasterxml.jackson.databind.JavaType;importcom.fasterxml.jackson.databind.json.JsonMapper;importcom.proc.model.ProcessDbModel;importcom.proc.service.ProcessCheckService;importcom.proc.util.Base64Util;importcom.proc.util.JacksonCanonicalUtil;importcom.proc.util.PrivateTransmitableUtil;importcom.proc.util.ProcessBeanUtil;@ServicepublicclassProcessCheckServiceImplimplementsProcessCheckService{privatestaticfinalLoggerlog=LoggerFactory.getLogger(ProcessCheckServiceImpl.class);privatestaticfinalJsonMapperMAPPER=newJsonMapper();@Overridepublicvoidprocess(ProcessDbModelmodel){PrivateTransmitableUtil.set();String[]paramArgs=Base64Util.decodeZip(model.getParamArgs());String[]paramTypes=Base64Util.decodeZip(model.getParamTypes());List<Class<?>>parameterTypes=newArrayList<>();List<Object>args=newArrayList<>();try{for(inti=0;i<paramTypes.length;i++){JavaTypejavaType=JacksonCanonicalUtil.constructFromCanonical(paramTypes[i]);parameterTypes.add(javaType.getRawClass());args.add(MAPPER.readValue(paramArgs[i],javaType));}}catch(JsonProcessingExceptione){thrownewRuntimeException("反序列化对象出错",e);}Objectret=ProcessBeanUtil.excuteBeanMethod(model.getTargetClassName(),model.getMethodName(),parameterTypes.toArray(newClass<?>[0]),args.toArray(newObject[0]));log.info(Objects.toString(ret));}}

    测试用的service

    packagecom.proc.service;importcom.proc.model.Person;importcom.proc.model.Score;publicinterfaceTestService{StringtestCheck(Person<Score>person,Stringteam);StringtestCheck2(Person<Score>person,Stringteam);StringtestCheckAsync(Person<Score>person,Stringteam);}

    packagecom.proc.service.impl;importorg.slf4j.Logger;importorg.slf4j.LoggerFactory;importorg.springframework.scheduling.annotation.Async;importorg.springframework.stereotype.Service;importcom.proc.bean.TestCheckPostProcess;importcom.proc.config.Checked;importcom.proc.model.Person;importcom.proc.model.Score;importcom.proc.service.TestService;@ServicepublicclassTestServiceImplimplementsTestService{privatestaticfinalLoggerlog=LoggerFactory.getLogger(TestServiceImpl.class);//paramCanonical对应testCheck的参数类型@Checked(paramCanonical={"com.proc.model.Person<com.proc.model.Score>","java.lang.String"},postProcess=TestCheckPostProcess.class)@OverridepublicStringtestCheck(Person<Score>person,Stringteam){log.info(team+">>>>"+person);return"target方法";}@Checked(tag="A1",paramCanonical={"com.proc.model.Person<com.proc.model.Score>","java.lang.String"},postProcess=TestCheckPostProcess.class)@OverridepublicStringtestCheck2(Person<Score>person,Stringteam){log.info(team+">>2>>"+person);return"target2方法";}@Async("processExecutor")@Checked(paramCanonical={"com.proc.model.Person<com.proc.model.Score>","java.lang.String"},postProcess=TestCheckPostProcess.class)@OverridepublicStringtestCheckAsync(Person<Score>person,Stringteam){log.info(team+">>>>"+person);return"target方法";}}

    审批用的controller

    packagecom.proc.ctrl;importorg.slf4j.Logger;importorg.slf4j.LoggerFactory;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.web.bind.annotation.GetMapping;importorg.springframework.web.bind.annotation.RestController;importcom.proc.model.ProcessDbModel;importcom.proc.service.ProcessCheckService;importcom.proc.service.ProcessDbService;@RestControllerpublicclassProcessCheckController{privatestaticfinalLoggerlog=LoggerFactory.getLogger(ProcessCheckController.class);@AutowiredprivateProcessDbServiceprocessDbService;@AutowiredprivateProcessCheckServiceprocessCheckService;@GetMapping(value="process")publicStringprocess(){ProcessDbModelprocessDbModel=processDbService.get();log.info(processDbModel.toString());processCheckService.process(processDbModel);return"审批成功";}}

    测试用的controller

    packagecom.proc.ctrl;importjava.math.BigDecimal;importjava.util.ArrayList;importjava.util.List;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.web.bind.annotation.GetMapping;importorg.springframework.web.bind.annotation.RestController;importcom.proc.model.CheckedParam;importcom.proc.model.Person;importcom.proc.model.Score;importcom.proc.service.TestService;importcom.proc.util.CheckedTransmitableUtil;@RestControllerpublicclassTestController{@AutowiredprivateTestServicetestService;//模拟页面调用@GetMapping(value="index")publicStringtestCheck(){CheckedParamcheckedParam=newCheckedParam();checkedParam.setTagPageJs("01");List<String>data=newArrayList<>();data.add("前端传进来的数据1");data.add("前端传进来的数据2");checkedParam.setData(data);CheckedTransmitableUtil.set(checkedParam);Person<Score>person=newPerson<>();person.setName("一个人");person.setAge("18");person.setSex("1");person.setSalary("20000.00");person.setTestName("测试人");person.setWork("工作");Scorescore1=newScore();score1.setEnglish(newBigDecimal("12.4"));score1.setLangue(newBigDecimal("764"));score1.setMath(newBigDecimal("87.4"));Scorescore2=newScore();score2.setEnglish(newBigDecimal("12.4"));score2.setLangue(newBigDecimal("764"));score2.setMath(newBigDecimal("87.4"));List<Score>list=newArrayList<>();list.add(score1);list.add(score2);person.setGrades(list);testService.testCheck(person,"team>>>>>>>>");return"12345";}//模拟其他渠道调用@GetMapping(value="index2")publicStringtestCheck2(){Person<Score>person=newPerson<>();person.setName("一个人");person.setAge("18");person.setSex("1");person.setSalary("20000.00");person.setTestName("测试人");person.setWork("工作");Scorescore1=newScore();score1.setEnglish(newBigDecimal("12.4"));score1.setLangue(newBigDecimal("764"));score1.setMath(newBigDecimal("87.4"));Scorescore2=newScore();score2.setEnglish(newBigDecimal("12.4"));score2.setLangue(newBigDecimal("764"));score2.setMath(newBigDecimal("87.4"));List<Score>list=newArrayList<>();list.add(score1);list.add(score2);person.setGrades(list);testService.testCheck2(person,"team>>>2>>>>>");return"12345";}//模拟调用异步方法@GetMapping(value="index3")publicStringtestCheckAsync(){CheckedParamcheckedParam=newCheckedParam();checkedParam.setTagPageJs("01");List<String>data=newArrayList<>();data.add("前端传进来的数据1");data.add("前端传进来的数据2");checkedParam.setData(data);CheckedTransmitableUtil.set(checkedParam);Person<Score>person=newPerson<>();person.setName("一个人");person.setAge("18");person.setSex("1");person.setSalary("20000.00");person.setTestName("测试人");person.setWork("工作");Scorescore1=newScore();score1.setEnglish(newBigDecimal("12.4"));score1.setLangue(newBigDecimal("764"));score1.setMath(newBigDecimal("87.4"));Scorescore2=newScore();score2.setEnglish(newBigDecimal("12.4"));score2.setLangue(newBigDecimal("764"));score2.setMath(newBigDecimal("87.4"));List<Score>list=newArrayList<>();list.add(score1);list.add(score2);person.setGrades(list);testService.testCheckAsync(person,"team>>>3>>>>>");return"12345";}}

    开启异步功能

    packagecom.proc;importorg.springframework.boot.SpringApplication;importorg.springframework.boot.autoconfigure.SpringBootApplication;importorg.springframework.scheduling.annotation.EnableAsync;@SpringBootApplication@EnableAsyncpublicclassProcessTestApplication{publicstaticvoidmain(String[]args){SpringApplication.run(ProcessTestApplication.class,args);}}

    关于springboot注解Aspect的实现方案是什么就分享到这里啦,希望上述内容能够让大家有所提升。如果想要学习更多知识,请大家多多留意小编的更新。谢谢大家关注一下亿速云网站!

    发布于 2022-01-10 23:47:57
    收藏
    分享
    海报
    0 条评论
    46
    上一篇:Go中是怎么实现用户的每日限额 下一篇:C++ OpenCV如何实现图像修复功能
    目录

      0 条评论

      本站已关闭游客评论,请登录或者注册后再评论吧~

      忘记密码?

      图形验证码