java aop拦截增删改成_SpringBoot —— AOP注解式拦截与方法规则拦截

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

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

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

org.springframework.boot

spring-boot-starter-aop

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

二、编写拦截规则的注解

packagecom.cenobitor.aop.annotation;import java.lang.annotation.*;

@Target(ElementType.METHOD)

@Retention(RetentionPolicy.RUNTIME)

@Documentedpublic @interfaceAction {

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 @interfaceTarget {

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:说明子类可以继承父类中的该注解

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

packagecom.cenobitor.aop.service;importcom.cenobitor.aop.annotation.Action;importorg.springframework.stereotype.Service;

@Servicepublic classDemoAnnotationService {

@Action(name= "注解式拦截的add操作")public voidadd(){}

}

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

packagecom.cenobitor.aop.service;importorg.springframework.stereotype.Service;

@Servicepublic classDemoMethodService {public voidadd(){}

}

五、编写切面

packagecom.cenobitor.aop.aspect;importcom.cenobitor.aop.annotation.Action;importorg.aspectj.lang.JoinPoint;importorg.aspectj.lang.annotation.After;importorg.aspectj.lang.annotation.Aspect;importorg.aspectj.lang.annotation.Before;importorg.aspectj.lang.annotation.Pointcut;importorg.aspectj.lang.reflect.MethodSignature;importorg.springframework.stereotype.Component;importjava.lang.reflect.Method;

@Aspect

@Componentpublic classLogAspect {

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

@After("annotationPoinCut()")public voidafter(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 voidbefore(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 classmain {public static voidmain(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做些许修改,省略一些配置项。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java中,可以使用注解AOP(面向切面编程)技术来拦截Controller的所有方法。 首先,需要创建一个自定义的注解,用于标识需要拦截方法。可以使用如下的注解定义: ```java import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) public @interface Intercept { } ``` 接下来,创建一个切面类,用于实现拦截逻辑。可以使用如下的切面类定义: ```java 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.springframework.stereotype.Component; @Aspect @Component public class ControllerInterceptor { @Pointcut("@annotation(Intercept)") public void interceptedMethods() {} @Before("interceptedMethods()") public void beforeIntercept() { // 在方法执行之前拦截的逻辑 } @After("interceptedMethods()") public void afterIntercept() { // 在方法执行之后拦截的逻辑 } } ``` 在上述的切面类中,使用了`@Aspect`注解表示这是一个切面类,使用了`@Component`注解将切面类交由Spring管理。`@Pointcut`注解定义了需要拦截方法,此处使用了`@annotation(Intercept)`表示拦截带有`Intercept`注解方法。`@Before`和`@After`注解分别表示在方法执行前和执行后进行拦截处理。 最后,在需要拦截的Controller的方法上使用`@Intercept`注解进行标记,例如: ```java @RestController public class MyController { @Intercept @GetMapping("/") public String index() { return "Hello World"; } } ``` 这样,只要在Controller的方法上使用了`@Intercept`注解,就会触发切面类中的拦截逻辑。 需要注意的是,上述代码使用了Spring AOP来实现AOP功能,因此需要在Spring配置文件中开启AOP的支持。另外,还需要引入相关的依赖,例如Spring AOP的依赖。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值