Spring Boot 自定义注解

一、引入依赖

<dependency>
	<groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-aop</artifactId>
</dependency>

二、创建注解接口并定义参数

@Documented
@Target({ElementType.PARAMETER, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface Log {
}
  • @interface是用来自定义JAVA Annotation的语法,
  • @interface是用来自定义注注解类型的
  • @Documented –注解是否将包含在JavaDoc(用于描述类或者方法的作用;Javadoc可以写在类上面和方法上面)中
  • @Retention –什么时候使用该注解
  • @Target –注解用于什么地方
  • @Inherited – 是否允许子类继承该注解

三、创建解析注解类

@Aspect
@Component
public class HLogAspect {
    
    @Pointcut("@annotation(com.threegroups.shopping.Log)")
    public  void annotationPointCut() {
    }

    @Before("annotationPointCut()")
    public void before(JoinPoint joinPoint){
    	MethodSignature sign =  (MethodSignature)joinPoint.getSignature();
        Method method = sign.getMethod();
        System.out.print("自定义注解已生效!");
    }
}

切面注解 :

@Aspect – 标识为一个切面供容器读取,作用于类
@Pointcut – (切入点):就是带有通知的连接点
@Before – 前置
@AfterThrowing – 异常抛出
@After – 后置
@AfterReturning – 后置增强,执行顺序在@After之后
@Around – 环绕

  • 定义切点(@Pointcut)并对其做增强:前置增强、环绕增强、后置增强等。
  • @Pointcut()定义表达式切点
  • @annotation() 类或者方法表示这个注解类或者方法是切点,即植入程序的点。
  • execution定义一个切入点表达式,用来确定哪些类需要代理。@Pointcut(“execution(* com.threegroups.shopping..(…))”)代表shopping包下所有类的所有方法都会被代理
  • 时间切入点一般有:@before @after @around @afterretund等。
  • JoinPoint对象封装了SpringAop中切面方法的信息,在切面方法中添加JoinPoint参数,就可以获取到封装了该方法信息的JoinPoint对象。
    获取代理方法的对象和参数 并添加包装。
JoinPoint的用法
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值