自定义注解 +AOP

1、自定义注解

1、元注解

@Target:

   @Target说明了Annotation所修饰的对象范围:Annotation可被用于 packages、types(类、接口、枚举、Annotation类型)、类型成员(方法、构造方法、成员变量、枚举值)、方法参数和本地变量(如循环变量、catch参数)。在Annotation类型的声明中使用了target可更加明晰其修饰的目标。

  作用:用于描述注解的使用范围(即:被描述的注解可以用在什么地方)

  **取值(ElementType)有:**
​
    1.CONSTRUCTOR:用于描述构造器
    2.FIELD:用于描述域
    3.LOCAL_VARIABLE:用于描述局部变量
    4.METHOD:用于描述方法
    5.PACKAGE:用于描述包
    6.PARAMETER:用于描述参数
    7.TYPE:用于描述类、接口(包括注解类型) 或enum声明

@Retention:

  @Retention定义了该Annotation被保留的时间长短:某些Annotation仅出现在源代码中,而被编译器丢弃;而另一些却被编译在class文件中;编译在class文件中的Annotation可能会被虚拟机忽略,而另一些在class被装载时将被读取(请注意并不影响class的执行,因为Annotation与class在使用上是被分离的)。使用这个meta-Annotation可以对 Annotation的“生命周期”限制。

  作用:表示需要在什么级别保存该注释信息,用于描述注解的生命周期(即:被描述的注解在什么范围内有效)

  **取值(RetentionPoicy)有:**
​
    1.SOURCE:在源文件中有效(即源文件保留)
    2.CLASS:在class文件中有效(即class保留)
    3.RUNTIME:在运行时有效(即运行时保留)

自定注解示例 :@NotificationResults

package com.sino.apprelation.dns.annotation;
​
import java.lang.annotation.*;
​
​
/**
 * @Description:
 * @Author: liujian
 * @Version: 1.0
 * @Create Date Time: 2021-08-10 10:56
 * @Update Date Time:
 * @see
 */
​
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface NotificationResults {
}

2、AOP切面

1、定义一个切面类:

@Aspect 用于声明一个类为切面

加在类上,如下

@Aspect
@Slf4j                                                     //是否需要日志,若需要添加上这个注解
@Component
public class NotificationResultsAspect {
...
}

2、AOP中使用的方法

常用:
    //获取参数
    Object[] args = joinPoint.getArgs();
    //获取参数名
    String[] aaa = ((CodeSignature)pjp.getSignature()).getParameterNames();
​
其他
    1.获取的实体类
    Object target = point.getTarget();
    2.获取的方法名称
    String methodName = point.getSignature().getName();
    3.获取的方法参数
    Object[] args = point.getArgs();
    4.获取的参数类型
    Class[] parameterTypes = ((MethodSignature)point.getSignature()).getMethod().getParameterTypes();
    5.通过反射获得拦截的method
    Method m = target.getClass().getMethod(methodName, parameterTypes);
    6.通过反射执行目标对象的连接点的方法
    Object o = point.proceed();
    7.通过反射执行目标对象连接点处的方法,使用新的参数列表替换原来的:
    point.proceed(Object[] args);   

3、proceed方法

1、环绕通知 ProceedingJoinPoint 执行proceed方法的作用是让目标方法执行,这也是环绕通知和前置、后置通知方法的一个最大区别。
2、简单理解,环绕通知=前置+目标方法执行+后置通知,proceed方法就是用于启动目标方法执行的.

AOP示例:

package com.sino.apprelation.dns.annotation;
​
import com.sino.apprelation.dns.entity.DnsLocalRecord;
import com.sino.service.api.common.Result;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
​
/**
 * @Description: DNS域名审批通知切入点实现方法-发送邮件通知
 * @Author: liujan
 * @Version: 1.0
 * @Create Date Time: 2021-08-10 15:25
 * @Update Date Time:
 * @see
 */
​
@Aspect
@Slf4j
@Component
public class NotificationResultsAspect {
​
    @Around("@annotation(NotificationResults)")
    public Object doNotification(ProceedingJoinPoint point) throws Throwable {
        //获取方法参数
        Object[] args = point.getArgs();
        //获取第一个参数
        DnsLocalRecord obj = (DnsLocalRecord) args[0];
        String userEmail = obj.getUserEmail();
​
        Result ret = (Result) point.proceed();
        if(ret.getCode()==1){
            //code=1,说明操纵成功,发送邮件
            System.out.println("============================邮件通知===============================");
            return ret;
        }
        //操作失败返回Result的执行结果
        return ret;
​
    }
  • 1
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值