Springboot的自定义注解_第1页
Springboot的自定义注解_第2页
Springboot的自定义注解_第3页
Springboot的自定义注解_第4页
Springboot的自定义注解_第5页
全文预览已结束

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

Springboot的⾃定义注解springBoot的⾃定义注解在有些业务场景中,springboot的注解并不能满⾜我们的要求,这时候就需要使⽤springboot的⾃定义注解功能了,在SpringBoot中⽀持的⾃定义注解可让代码更加简洁,效率更加的⾼效。@Target:@Target:注解的作⽤⽬标ElementType.TYPE——接⼝、类、枚举、注解ElementType.FIELD——字段、枚举的常量ElementType.METHOD——⽅法ElementType.PARAMETER——⽅法参数ElementType.CONSTRUCTOR——构造函数ElementType.LOCAL_VARIABLE——局部变量ElementType.ANNOTATION_TYPE)——注解ElementType.PACKAGE)——包@Documented注解只是⽤来做标识,没什么实际作⽤,了解就好@Retention注解的⽣命周期source:注解只保留在源⽂件,当Java⽂件编译成class⽂件的时候,注解被遗弃;被编译器忽略class:注解被保留到class⽂件,但jvm加载class⽂件时候被遗弃,这是默认的⽣命周期runtime:注解不仅被保存到class⽂件中,jvm加载class⽂件之后,仍然存在@Target(ElementType.METHOD)@Documented@Retention(RetentionPolicy.RUNTIME)public@interfaceMyAnno{Stringmsg()default“”;}引⼊切⾯的AOP⽀持@Aspect:作⽤是把当前类标识为⼀个切⾯供容器读取@Pointcut:Pointcut是植⼊Advice的触发条件。每个Pointcut的定义包括2部分,⼀是表达式,⼆是⽅法签名。⽅法签名必须是public及void型。可以将Pointcut中的⽅法看作是⼀个被Advice引⽤的助记符,因为表达式不直观,因此我们可以通过⽅法签名的⽅式为此表达式命名。因此Pointcut中的⽅法只需要⽅法签名,⽽不需要在⽅法体内编写实际代码。//====================⽅法before上⾯的注解有:@Around:环绕增强,相当于MethodInterceptor@AfterReturning:后置增强,相当于AfterReturningAdvice,⽅法正常退出时执⾏@Before:标识⼀个前置增强⽅法,相当于BeforeAdvice的功能,相似功能的还有@AfterThrowing:异常抛出增强,相当于ThrowsAdvice@After:final增强,不管是抛出异常或者正常退出都会执⾏@Aspect@ComponentpublicclassCurrentUserHandlerMethodArgReslover{@Pointcut("@annotation(com.gx.mydemo.config.demo2.MyAnno)")publicvoidfun(){}@Before("fun()")publicvoidbefore(JoinPointjoinPoint){}}测试:调⽤实体类,触发⾃定义注解@RequestMapping("/Demo1")publicvoidDemo1(HttpServletResponseresponse){Userperson=newUser();System.out.println(person.getName()+""+person.getAge());}场景⼆:放在字段上⾯,读取并设置名称,⽣成Excel表格放在字段上⾯,是没办法通过AOP切⾯切⼊的,这⾥我想到了两种办法:第⼀:编写⼯具类,在⽅法上⾯调⽤;第⼆:通过拦截器拦截,并且解析⾃定义我这⾥使⽤的是第⼀种:@Documented@Target({ElementType.METHOD,ElementType.FIELD})@Retention(RetentionPolicy.RUNTIME)public@interfaceMyAnnotation{Stringmsg()default"";}在实体类字段的上⾯加⼊注解,设置表头名称@DatapublicclassBStudentimplementsSerializable{@MyAnnotation(msg="姓名")privateStringstudentName;@MyAnnotation(msg="⾝份证号")privateStringsfzNumber;@MyAnnotation(msg="性别")privateStringsex;@MyAnnotation(msg="年龄")privateStringage;@MyAnnotation(msg="电话号码")privateStringstudentPhone;}⼯具类:publicclassanalysisAnnotation<T>{privateTExcelTipsVo;/***解读⾃定义注解的参数msg值,并且返回*@paramExcelTipsVo*@return*/publicMap<String,List<String>>getAnnotation(TExcelTipsVo){List<String>ExcelTips=newArrayList<>();List<String>VoName=newArrayList<>();Map<String,List<String>>map=newHashMap<>();Field[]fields=ExcelTipsVo.getClass().getDeclaredFields();for(inti=0;i<fields.length;i++){Annotationannotation=fields[i].getAnnotation(MyAnnotation.class);if(annotationinstanceofMyAnnotation){MyAnnotationcustomAnnotation=(MyAnnotation)annotation;VoName.add(fields[i].getName());ExcelTips.add((String)customAnnotation.msg());}}map.put("Annotation",ExcelTips);map.put("Name",VoName);returnmap;}设置excel表格配置:publicclassExcelUtil<T>{publicvoidsetExelTips(TmyPojoe,List<T>ExcelValue,StringfileName,HttpServletResponseresponse){analysisAnnotationanalysisAnnotation=newanalysisAnnotation();Map<String,List<String>>map=analysisAnnotation.getAnnotation(myPojoe);introwNum=1;HSSFWorkbookworkbook=newHSSFWorkbook();HSSFSheetsheet=workbook.createSheet("信息表");HSSFRowrow=sheet.createRow(0);List<String>annotation=map.get("Annotation");List<String>VoName=map.get("Name");if(!fileName.contains(".xls")){fileName+=".xls";}for(inti=0;i<annotation.size();i++){HSSFCellcell=row.createCell(i);HSSFRichTextStringtext=newHSSFRichTextString((String)annotation.get(i));cell.setCellValue(text);}try{for(Tt:ExcelValue){HSSFRowrow1=sheet.createRow(rowNum);for(inti=0;i<VoName.size();i++){//将属性名字的⾸字母⼤写Stringname=VoName.get(i).replaceFirst(VoName.get(i).substring(0,1),VoName.get(i).substring(0,1).toUpperCase());Methodm=t.getClass().getMethod("get"+name);Stringvalue=String.valueOf(m.invoke(t));System.err.println("value==>"+value);row1.createCell(i).setCellValue(value);}rowNum++;}response.setContentType("application/octet-stream");response.setHeader("Content-disposition","attachment;filename="+fileName);response.flushBuffer();workbook.write(response.getOutputStream());}catch(Exceptione){e.printStackTrace();}}}然后就是在控制器调⽤,⽣成Excel表格@RequestMapping("/Demo2")publicvoidDemo2(HttpServletResponseresponse){List<BStudent>list=newArrayList<>();for(inti=1;i<=6;i++){BStudentBStudent=newBStudent();BStudent.setStudentName("杨"+i);BStudent.setAge("1"+i

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论