spring 自定义注解已经是常用操作 我们来复习一下操作步骤

 

第一步

引入jar 

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-aop</artifactId>

 

第二步

创建注解接口

定义参数

 

@Documented

@Retention(RUNTIME)

@Target(METHOD) public @interface LoginAnno { String value(); }

 

@interface是用来自定义JAVA Annotation的语法,
@interface是用来自定义注注解类型的

 

@Documented –注解是否将包含在JavaDoc中

@Retention –什么时候使用该注解

@Target –注解用于什么地方

@Inherited – 是否允许子类继承该注解

 

第三步

创建解析注解类 包括注解功能类

定义切点,并对切点做一些增强操作:前置增强、环绕增强、后置增强等等,切点的定义我们可以在一个空方法体的方法上使用@Pointcut注解

spring boot自定义注解 注入 springboot自定义注解 解析_自定义

 

这里 @pointcut 定义切点 名字为cut 

然后定义了具体方法 具体方法 因为 切点叫cut 所以 @before 后面要变成 ‘cut’

@Pointcut()里面定义的是切点表达式,切点表达式有很多,上面例子代码中的是注解表达式

这个注解表示何时把注解植入进spring

 execution(方法修饰符 返回类型 方法全限定名 参数) 匹配指定的方法

@Pointcut("execution(* com.tcb.controller.SDProductController.showproductDetail(..))")

@annotation() 类或者方法 表示 这个注解类或者方法都是切点 即织入程序的点

 

切点 即 在什么时间切入

一般有

@before @after @around @afterretund等

 

JoinPoint对象封装了SpringAop中切面方法的信息,在切面方法中添加JoinPoint参数,就可以获取到封装了该方法信息的JoinPoint对象. 

获取代理方法的对象和参数 并添加包装

 

 

 

ProceedingJoinPoint 实际应用中用到

 

public Object Interceptor(ProceedingJoinPoint joinPoint){
retmsg = joinPoint.proceed(); 获取方法名
System.out.println("通知之结束 +retmsg+" + retmsg);
result = joinPoint.proceed(); 程序继续执行
return result;
}

 

通俗一点,JoinPoint就是在程序执行过程中明确的点,提供访问当前被拦截方法的信息,而ProceedingJoinPoint除了JoinPoint能够获徖的信息外,还可以通过proceed()方法来继续执行被拦截的方法,常用于@Around修饰的AOP拦截。