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
