您好,登录后才能下订单哦!
今天就跟大家聊聊有关如何在SpringBoot中实现定位切点,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。
execution 表达式
execution表达式的方式主要是在定义切点的时候,通过表达式的方式选取到所需要增强的方法。
execution表达式解读
execution(<修饰符模式>?<返回类型模式><方法名模式>(<参数模式>)<异常模式>?)
| 类型 | 解读 | 是否必须 | 示例 | 
|---|---|---|---|
| <修饰符模式> | 表示所选的修饰符类型 | 否 | public/private/... | 
| <返回类型模式> | 表示所选的返回值类型 | 是 | void/int/... | 
| <方法名模式> | 表示所选的包或者方法 | 是 | com.luke.service/com.luke.controller.*/... | 
| (<参数模式>) | 表示所选方法的参数 | 是 | *(..)/*(String name)/*(int size, ..)/... | 
| <异常模式> | 表示所选方法的异常类型 | 否 | throws Exception/... | 
// 匹配指定包中的所有方法 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**注解,调用该方法,查看效果。
方法执行前
方法执行了
方法执行后
看完上述内容,你们对如何在SpringBoot中实现定位切点有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注亿速云行业资讯频道,感谢大家的支持。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。