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

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

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

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

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

 此依赖已包含AspectJ相关依赖包。

二、编写拦截规则的注解

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四种。

[email protected]: 定义注解的保留策略

  • @Retention(RetentionPolicy.SOURCE)   //注解仅存在于源码中,在class字节码文件中不包含
  • @Retention(RetentionPolicy.CLASS)     // 默认的保留策略,注解会在class字节码文件中存在,但运行时无法获得,
  • @Retention(RetentionPolicy.RUNTIME)  // 注解会在class字节码文件中存在,在运行时可以通过反射获取到

  首先要明确生命周期长度 SOURCE < CLASS < RUNTIME ,所以前者能作用的地方后者一定也能作用。一般如果需要在运行时去动态获取注解信息,那只能用 RUNTIME 注解;如果要在编译时进行一些预处理操作,比如生成一些辅助代码(如 ButterKnife),就用 CLASS注解;如果只是做一些检查性的操作,比如 @Override 和 @SuppressWarnings,则可选用 SOURCE 注解。

[email protected]:定义注解的作用目标
 源码为:

@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) ///包

[email protected]:说明该注解将被包含在javadoc中
[email protected]:说明子类可以继承父类中的该注解

三、编写使用注解的被拦截类

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(){}
}

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

package com.cenobitor.aop.service;

import org.springframework.stereotype.Service;

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

五、编写切面

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。

六、运行

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

注:摘抄自《JavaEE开发的颠覆者SpringBoot 实战》,根据springboot 2.0.3做些许修改,省略一些配置项。

原文地址:https://www.cnblogs.com/gdwkong/p/9307673.html

时间: 2024-08-11 17:14:45

SpringBoot —— AOP注解式拦截与方法规则拦截的相关文章

Struts2拦截指定方法的拦截器

作者:禅楼望月 默认情况下,我们为一个Action配置一个拦截器,该拦截器会拦截该Action中的所有方法,但是有时候我们只想拦截指定的方法.为此,需要使用struts2拦截器的方法过滤特性. 要使用struts2拦截器的方法过滤特性其实也很简单,只需让拦截器的实现类继承com.opensymphony.xwork2.interceptor.MethodFilterInterceptor类.该类是AbstractInterceptor的子类.它重写了AbstractInterceptor类的in

AOP注解式拦截

1. 自己定义的拦截注解 package com.spring.aop; import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Retention(R

11.SpringMVC注解式开发-处理器方法的返回值

处理器方法的返回值 使用@Controller 注解的处理器的处理器方法,其返回值常用的有四种类型 1.ModelAndView 2.String 3.void 4.自定义类型对象 1.返回ModelAndView 若处理器方法处理完后,需要跳转到其他资源,且又要在跳转的资源间传递数据,此时处理器方法 返回ModelAndView比较好.当然,若要返回ModelAndView,则处理器方法中需要定义ModelAndView对象 在使用时,若该处理器方法只是进行跳转而不传递数据或只是传递数据而不向

SpringBoot aop 注解 数据权限校验

注解类: @Retention(RetentionPolicy.RUNTIME) public @interface DataAuthValid { //位置 public int index() default 0; //字段 id //public String id() default "id"; //字段 id public String orgId() default "org_id"; //mapper @SuppressWarnings("r

springboot的注解声明过滤器配置错误导致拦截所有请求。

究其原因, 原来spring 扫包时候 扫了Webfilter 注解,注册了一次过滤匹配路径,扫了Component注解(又注册了一次过滤匹配路径,默认是全路径). Component注解后于WebFilter注解加载,所以导致Component注解覆盖了前面的匹配路径. 原文地址:https://www.cnblogs.com/valarchie/p/10887789.html

springmvc 注解式开发 处理器方法的返回值

1.返回void -Ajax请求 后台: 前台:

【Spring开发】—— AOP之方法级拦截

前言: 前面介绍了Spring的核心模块以及相关的依赖注入等概念.这篇讲解一下spring的另一个重点,AOP面向切面编程. 说道AOP不得不提到几个概念: 切面:也就是我们自己的一些业务方法. 通知:用于拦截时出发的操作. 切点:具体拦截的某个业务点. 这样说可能还是有点抽象,举个例子,下面是一个纸糊的多面体. 每个面都是一个业务方法,我们通过刺穿每一个面,都可以进入到内部,这个面就是一个切面. 刺穿的时候会发出声响,这就是一种通知. 而具体从哪个面刺入,这就是一个切入点的选择了. 这样说,应

springBoot AOP学习(一)

AOP学习(一) 1.简介 AOp:面向切面编程,相对于OOP面向对象编程. Spring的AOP的存在目的是为了解耦.AOP可以让一切类共享相同的行为.在OOP中只能通过继承类或者实现接口,使代码的耦合度增强,且类继承只能为单继承,阻碍更多行为添加到一组类上,AOP弥补了OOP的不足. Spring支持AspectJ的注解式切面编程. (1)使用@Aspect声明是一个切面: (2)使用@After.@Before.@Around定义通知(Advice),可直接将拦截规则(切点)作为参数: (

Spring AOP +EHcache为Service层方法增加缓存

在铁科院做了一个关于医保报销的项目,在这个个系统中大量使用了下拉列表框,系统主要是给机关单位使用而且都是一些干部退休了啥的,年龄都比较大不愿意自己输入东西,因此界面上的很多值都是下拉列表框从数据字典表里面加载出来. 如此以来字典表的数据量变的越来越大,在一个界面上往往需要频繁的与字典表交互,觉的很影响性能于是我们增加了缓存,即为service层中的指定方法缓存功能,具体实现是利用Spring AOP+EHcache来做. 第一次执行某个方法的时候会去数据库里面查询,当第二次执行该方法时就会去从缓