springboot 自定义注解(含源码)

springboot 自定义注解(含源码)spring 注解一般在记录日志 定时器中使用非常方便 在 springmvc 框架广泛应用 可以注解的随处可见 近几年流行的 springboot 框架 更把注解用到了极致 这框架的基本消灭了大部分传统框架上 xml 配制后改为注解代替 既然注解这么使用这么多 那么如何自定义注解呢以下例子以 springboot 中使用自定义注解来简单讲解 源码地址 https gitee com xing xin my

spring注解 一般在记录日志、定时器中使用非常方便,在springmvc框架广泛应用,可以注解的随处可见,近几年流行的springboot框架,更把注解用到了极致,这框架的基本消灭了大部分传统框架上xml配制后改为注解代替,既然注解这么使用这么多,那么如何自定义注解呢

以下例子以springboot中使用自定义注解来简单讲解(源码地址:https://gitee.com/xing_xin/my-annotation.git)

 
   
   
    
    
    
    
    
    
    
      org.springframework.boot 
     
    
      spring-boot-starter-parent 
     
    
      2.0.0.RELEASE 
     
     
    
   
     4.0.0 
    
   
     my-annotation 
    
    
     
     
       org.springframework.boot 
      
     
       spring-boot-starter-web 
      
     
     
     
       org.springframework.boot 
      
     
       spring-boot-starter-aop 
      
     
    
    
    
     
      
      
        org.springframework.boot 
       
      
        spring-boot-maven-plugin 
       
      
     
    
   

2、定义注解

 几引用注解的解释 @Documented 注解 功能:指明修饰的注解,可以被例如javadoc此类的工具文档化,只负责标记,没有成员取值。 
package com.test.my.annotation; import java.lang.annotation.*; / * Created by Administrator on 2019/2/25/025. */ @Documented @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) public @interface MyAnnotation { String value() default "first one"; } 

3、定义切面类

@After 在切点后,return前执行,

import org.aspectj.lang.JoinPoint; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.*; import org.aspectj.lang.reflect.MethodSignature; import org.springframework.stereotype.Component; import java.lang.reflect.Method; / * @ClassName TestAspect * @Description TODO * @Date 2019/2/25/02515:12 * @Version 1.0 / @Aspect @Component public class TestAspect { // @Pointcut("execution(public * com.test.my.annotation.TestController.*(..)) && @annotation(com.test.my.annotation.MyAnnotation)" ) @Pointcut("@annotation(com.test.my.annotation.MyAnnotation)" ) public void addAdvice(){} @Around("addAdvice()") public Object Interceptor(ProceedingJoinPoint joinPoint){ System.out.println("====Interceptor===="); System.out.println("通知之开始"); Object retmsg=null; try { retmsg = joinPoint.proceed(); System.err.println("++++++++"+retmsg); } catch (Throwable e) { e.printStackTrace(); } System.out.println("通知之结束 +retmsg+" + retmsg); Object result = null; Object[] args = joinPoint.getArgs(); if (args != null && args.length > 0) { String deviceId = (String) args[0]; if (!"03".equals(deviceId)) { return "no anthorization"; } } try { result = joinPoint.proceed(); } catch (Throwable e) { e.printStackTrace(); } return result; } @Before("addAdvice()") public void before(JoinPoint joinPoint){ MethodSignature sign = (MethodSignature)joinPoint.getSignature(); Method method = sign.getMethod(); MyAnnotation annotation = method.getAnnotation(MyAnnotation.class); System.out.println("打印:" + annotation.value() + " 开始前"); //System.out.println("===开始前==="); } @After("addAdvice()") public void after() { System.out.println("after方法执行后"); } } 

4、controller 代码

package com.test.my.annotation; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; / * @ClassName TestController * @Description TODO * @Date 2019/2/25/02515:07 * @Version 1.0 / @RestController public class TestController { @MyAnnotation() @RequestMapping("/add1") public String addData1(String deviceId) { System.out.println("=====addData1====="); return "success"; } @MyAnnotation() @RequestMapping("/add2") public String addData2(String deviceId) { System.out.println("=====addData2====="); return "success"; } @MyAnnotation() @RequestMapping("/add3") public String addData3(String deviceId) { System.out.println("=====addData3====="); return "success"; } @MyAnnotation() @RequestMapping("/update1") public String updateData1(String deviceId) { System.out.println("=====updateData1====="); return "success"; } } 

5、application 代码

package com.test.my.annotation; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.EnableAspectJAutoProxy; / * @ClassName TestApplication * @Description TODO * @Date 2019/2/25/02515:07 * @Version 1.0 / @SpringBootApplication @EnableAspectJAutoProxy public class TestApplication { public static void main( String[] args ) { SpringApplication.run(TestApplication.class, args); } } 

在这里插入图片描述

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/201243.html原文链接:https://javaforall.net

(0)
上一篇 2026年3月20日 上午9:46
下一篇 2026年3月20日 上午9:46


相关推荐

  • LaTeX 换行、换页、空白空间[通俗易懂]

    LaTeX 换行、换页、空白空间[通俗易懂]一般来说,我们不推荐你改变默认的LaTeX文档结构。当然,我们有时候也有这个需求。所以,在本文中,我们将解释如何在文档中插入空行,以及插入任意的空白。

    2022年5月14日
    154
  • pycharm如何返回上一个步骤_pycharm如何返回上一个步骤

    pycharm如何返回上一个步骤_pycharm如何返回上一个步骤view>>Appearance>>Toolbar启用toolbar后用点击左键就可返回上一次编辑的位置

    2022年8月25日
    9
  • php 清楚浏览器缓存,如何清除浏览器缓存「建议收藏」

    php 清楚浏览器缓存,如何清除浏览器缓存「建议收藏」头像是根据url指定的,更改头像后url仍然不变,即url指向的图片地址没变,但图片已经更换了因为url没变所以浏览器还是用原来的图片,怎么更改后及时显示新的图片呢?是因为浏览器的缓存吗,怎么用php清楚浏览器缓存呢回复内容:头像是根据url指定的,更改头像后url仍然不变,即url指向的图片地址没变,但图片已经更换了因为url没变所以浏览器还是用原来的图片,怎么更改后及时显示新的图片呢?是因为浏…

    2022年7月18日
    22
  • OC中语法糖,最新语法总结

    OC中语法糖,最新语法总结

    2022年1月30日
    50
  • AFL 源码分析

    AFL 源码分析AFL 作为 C C 白盒 fuzzer 的鼻祖 为后来许多优秀的 fuzzer 提供了技术支持 衍生了很多 fuzzer 工具 本文只是站在巨人的肩膀上 参考了大量的博客 重新审计了部分源码 很多细节并没有深究 但对理解 AFL 的思想还是有一定作用的

    2025年11月14日
    4
  • 判断是否为数组的 JavaScript 方法总结

    判断是否为数组的 JavaScript 方法总结前言我们在日常开发中 常常有判断某值类型需求 今天我们总结一下常见的几种用来判断是否为数组的 JavaScript 方法 Array isArrayArray isArray 是 ES5 新增的方法 用于确定传递的值是否是一个数组 如果是数组 则返回 true 否则返回 false letarr console log Array isArray arr true 下面的函数调用都返回 true Array isArray Array isArray 1

    2026年3月16日
    1

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号