SpringBoot —— AOP注解式拦截与方法规则拦截

AspectJ是一个面向切面的框架,它扩展了Java语言。AspectJ定义了AOP语法,因此它有一个专门的编译器用来生成遵照Java字节编码规范的Class文件。html

SpringBoot中AOP的使用方式主要有两种:注解式拦截与方法规则拦截,具体使用以下文所示。java

1、建立一个简单springboot 2.03项目,添加aop依赖web

<dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-aop</artifactId>
</dependency>
 此依赖已包含AspectJ相关依赖包。

2、编写拦截规则的注解spring

package com.cenobitor.aop.annotation;

import java.lang.annotation.*;

@Target(ElementType.METHOD) 
@Retention(RetentionPolicy.RUNTIME) 
@Documented
public @interface Action {
    String name();
}

注解说明:元注解是指注解的注解,包括@Retention @Target @Document @Inherited四种。数据库

1.@Retention: 定义注解的保留策略

  @Retention(RetentionPolicy.SOURCE)   //注解仅存在于源码中,在class字节码文件中不包含
  @Retention(RetentionPolicy.CLASS)     // 默认的保留策略,注解会在class字节码文件中存在,但运行时没法得到,
  @Retention(RetentionPolicy.RUNTIME)  // 注解会在class字节码文件中存在,在运行时能够经过反射获取到
  首先要明确生命周期长度 SOURCE < CLASS < RUNTIME ,因此前者能做用的地方后者必定也能做用。通常若是须要在运行时去动态获取注解信息,那只能用 RUNTIME 注解;若是要在编译时进行一些预处理操做,好比生成一些辅助代码(如 ButterKnife),就用 CLASS注解;若是只是作一些检查性的操做,好比 @Override 和 @SuppressWarnings,则可选用 SOURCE 注解。

2.@Target:定义注解的做用目标
 源码为:编程

@Documented  
@Retention(RetentionPolicy.RUNTIME)  
@Target(ElementType.ANNOTATION_TYPE)  
public @interface Target {  
    ElementType[] value();  
}

@Target(ElementType.TYPE) //接口、类、枚举、注解
@Target(ElementType.FIELD) //字段、枚举的常量
@Target(ElementType.METHOD) //方法
@Target(ElementType.PARAMETER) //方法参数
@Target(ElementType.CONSTRUCTOR) //构造函数
@Target(ElementType.LOCAL_VARIABLE)//局部变量
@Target(ElementType.ANNOTATION_TYPE)//注解
@Target(ElementType.PACKAGE) ///包
3.@Document:说明该注解将被包含在javadoc中
4.@Inherited:说明子类能够继承父类中的该注解springboot

3、编写使用注解的被拦截类框架

package com.cenobitor.aop.service;

import com.cenobitor.aop.annotation.Action;
import org.springframework.stereotype.Service;

@Service
public class DemoAnnotationService {
    @Action(name = "注解式拦截的add操做")
    public void add(){}
}

4、编写使用方法规则被拦截类ide

package com.cenobitor.aop.service;

import org.springframework.stereotype.Service;

@Service
public class DemoMethodService {
    public void add(){}
}

5、编写切面svg

package com.cenobitor.aop.aspect;

import com.cenobitor.aop.annotation.Action;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import java.lang.reflect.Method;

@Aspect
@Component
public class LogAspect {

    @Pointcut("@annotation(com.cenobitor.aop.annotation.Action)")
    public void annotationPoinCut(){}

    @After("annotationPoinCut()")
    public void after(JoinPoint joinPoint){
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        Action action = method.getAnnotation(Action.class);
        System.out.println("注解式拦截 "+action.name());
    }

    @Before("execution(* com.cenobitor.aop.service.DemoMethodService.*(..))")
    public void before(JoinPoint joinPoint){
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        System.out.println("方法规则式拦截,"+method.getName());
    }
}

AOP注解说明:

@Aspect 定义切面:切面由切点和加强(引介)组成(能够包含多个切点和多个加强),它既包括了横切逻辑的定义,也包括了链接点的定义,SpringAOP就是负责实施切面的框架,它将切面所定义的横切逻辑织入到切面所指定的连接点中。
@Pointcut 定义切点:切点是一组链接点的集合。AOP经过“切点”定位特定的链接点。经过数据库查询的概念来理解切点和链接点的关系再适合不过了:链接点至关于数据库中的记录,而切点至关于查询条件。
@Before :在目标方法被调用以前作加强处理,@Before只须要指定切入点表达式便可。
@AfterReturning : 在目标方法正常完成后作加强,@AfterReturning除了指定切入点表达式后,还能够指定一个返回值形参名returning,表明目标方法的返回值。
@Afterthrowing: 主要用来处理程序中未处理的异常,@AfterThrowing除了指定切入点表达式后,还能够指定一个throwing的返回值形参名,能够经过该形参名来访问目标方法中所抛出的异常对象。
@After: 在目标方法完成以后作加强,不管目标方法时候成功完成。@After能够指定一个切入点表达式。
@Around: 环绕通知,在目标方法完成先后作加强处理,环绕通知是最重要的通知类型,像事务,日志等都是环绕通知,注意编程中核心是一个ProceedingJoinPoint。
6、运行
public class main {
    public static void main(String[] args) {

        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AopApplication.class);
        DemoAnnotationService demoAnnotationService = context.getBean(DemoAnnotationService.class);
        DemoMethodService demoMethodService = context.getBean(DemoMethodService.class);
        demoAnnotationService.add();
        demoMethodService.add();
    }
}

AopApplication.class为本项目的启动类。

运行结果以下:

注解式拦截 注解式拦截的add操做
方法规则式拦截,add

转自:http://www.javashuo.com/article/p-zusylyeo-cg.html