最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
SpringBoot中定位切点的两种常用方法代码示例
时间:2022-06-29 02:23:01 编辑:袖梨 来源:一聚教程网
本篇文章小编给大家分享一下SpringBoot中定位切点的两种常用方法代码示例,文章代码介绍的很详细,小编觉得挺不错的,现在分享给大家供大家参考,有需要的小伙伴们可以来看看。
有时候,我们使用AOP来进行放的增强,编写切面类的时候,需要定位在哪个方法上试用该切面进行增强,本片文章主要讲解两种在SpringBoot中定位切点的方法,一种是使用execution表达式的方法,一种则是利用自定义注解的方法。
接下来以一个简单的例子来讲解这两种方法的使用方式。
<==========方法执行前==========>
method();
<==========方法执行后==========>
execution 表达式
execution表达式的方式主要是在定义切点的时候,通过表达式的方式选取到所需要增强的方法。
execution表达式解读
execution(<修饰符模式>?<返回类型模式><方法名模式>(<参数模式>)<异常模式>?)
// 匹配指定包中的所有方法 execution(* com.luke.service.*(..)) // 匹配当前包中的所有public方法 execution(public * UserService.*(..)) // 匹配指定包中的所有public方法,并且返回值是int类型的方法 execution(public int com.luke.service.*(..)) // 匹配指定包中的所有public方法,并且第一个参数是String,返回值是int类型的方法 execution(public int com.luke.service.*(String name, ..))
自定义切面类:
@Aspect
@Component
public class LogAspect {
@Pointcut("execution(* com.luke.springdata.controller.*.*(..))")
public void operationLog(){}
/**
* 这里只定义一个Around的增强做展示
*/
@Around("operationLog()")
public Object doAround(ProceedingJoinPoint joinPoint) {
Object proceed = null;
try {
System.out.println("方法执行前");
proceed = joinPoint.proceed();
System.out.println("方法执行后");
} catch (Throwable throwable) {
throwable.printStackTrace();
}
return proceed;
}
}
此切点的execution表达式为com.luke.springdata.controller包下的所有方法。
使用**@Around**注解表明增强的方法,并且指定切点。
测试用Controller类
@RestController
@RequestMapping("/person")
public class PersonController {
@GetMapping("/test")
public void test(){
System.out.println("方法执行了");
}
}
运行项目,调用该方法,查看结果。
方法执行前
方法执行了
方法执行后
自定义注解的方法
自定义注解的方式就是在需要增强的方法上面加上自定义的注解即可。
自定义注解类:
@Documented
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface Log{
}
这里自定义了一个注解Log,该注解只能加在方法上。
自定义切面类:
@Aspect
@Component
public class LogAspect {
@Pointcut("@annotation(com.luke.springdata.annotation.Log)")
public void operationLog(){}
/**
* 这里只定义一个Around的增强做展示
*/
@Around("operationLog()")
public Object doAround(ProceedingJoinPoint joinPoint) {
Object proceed = null;
try {
System.out.println("方法执行前");
proceed = joinPoint.proceed();
System.out.println("方法执行后");
} catch (Throwable throwable) {
throwable.printStackTrace();
}
return proceed;
}
}
这里编写的自定义个切面类,用**@Pointcut注解定义一个切面,并且这次采用@annotation(xxx)**的方式表明如果哪个方法上添加了xxx注解,则就使用该切面做增强。
同时在每个增强的方法上使用该切面,随后编写正常的方法增强逻辑即可。
测试用Controller类
@RestController
@RequestMapping("/person")
public class PersonController {
@Log
@GetMapping("/test")
public void test(){
System.out.println("方法执行了");
}
}
此时在需要使用切面的方法上加入**@Log**注解,调用该方法,查看效果。
方法执行前
方法执行了
方法执行后
相关文章
- 暗喻幻想:ReFantazio无敌面料理获取攻略 10-29
- 暗喻幻想:ReFantazio召唤龙神条件说明 10-29
- 燕云十六声鲮货郎怎么解锁-鲮货郎经营解锁方法 10-29
- 暗喻幻想:ReFantazio召唤狮鹫王条件说明 10-29
- 暗喻幻想找玛丽亚聊旅途见闻任务攻略 10-29
- 暗喻幻想:ReFantazio新世界旅行记阅读攻略 10-29
