SpringAop @Pointcut(“@annotation“)\@Aspect练习

今天药忘吃喽~ 2023-10-01 08:44 19阅读 0赞

切面记录日志

切面类

  1. @Slf4j
  2. @Aspect
  3. @Component
  4. public class AspectForFeign {
  5. @Pointcut("execution(public * com.keke.remote..*Feign.*(..))")
  6. public void pointcut() {
  7. }
  8. @Around("pointcut()")
  9. public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
  10. long start = System.currentTimeMillis();
  11. log.info("@Around:开始执行目标方法:{}ms", start);
  12. Object result = null;
  13. try {
  14. result = joinPoint.proceed();
  15. } catch (Exception e) {
  16. System.out.println(e.getMessage());
  17. }
  18. long end = System.currentTimeMillis();
  19. log.info("@Around:结束执行目标方法:{}ms", end);
  20. //获取方法签名
  21. MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
  22. String methodName = methodSignature.getName();
  23. log.info("方法名:{} 耗时:{}ms", methodName, end - start);
  24. //如果不返回result,则目标对象实际返回值会被置为null
  25. return result;
  26. }
  • 注意:返回结果如果不返回result,则目标对象实际返回值会被置为null

@Component

  • 需要将切面类配置为bean

@Aspect

  • 标记为切面类

@Pointcut

  • 需要表达式命名,而不需要在方法体内编写实际代码。

    execution(modifiers-pattern? ret-type-pattern declaring-type-pattern? name-pattern(param-pattern)throws-pattern?)

@Around

  • 环绕增强,@Pointcut和@Around联合使用等同于:

    @Around(“execution(public com.keke.remote..Feign.*(..))”)

切点拦截记录访问日志

拦截器数据源

  1. @Retention(RetentionPolicy.RUNTIME)
  2. @Target(ElementType.METHOD)//注解在方法上
  3. public @interface ApiAccess{
  4. }

拦截器业务实现代码

  1. @Aspect
  2. @Component
  3. @Slf4j
  4. public class ApiAccessAspect {
  5. @Pointcut("@annotation(com.keke.annotation.ApiAccess)")
  6. public void pointcut() {
  7. }
  8. @Around("pointcut()")
  9. public Object apiAccessAspect(ProceedingJoinPoint joinPoint) throws Throwable {
  10. HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
  11. log.info("url:{}被访问了.....",request.getRequestURL().toString());
  12. return joinPoint.proceed(joinPoint.getArgs());
  13. }
  14. }

@Pointcut的表达式-@annotation

限制连接点的匹配,其中连接点的主题(在 Spring AOP 中执行的方法)具有给定的 annotation。

官方案例:

任何连接点(仅在 Spring AOP 中执行方法),其中执行方法具有@Transactional annotation:

  1. @annotation(org.springframework.transaction.annotation.Transactional)

官方的案例已经说的很清楚了,就是@annotation是匹配拥有指定注解的方法的。这里要注意,@annotation只匹配实现类中的有注解的方法,不会匹配接口中的注解方法。

看案例:

我们准备接口:

  1. /**
  2. * @description
  3. */
  4. public interface IBookService {
  5. //@DkAnnotation// 这里的注解是不会被匹配的
  6. public void saveBook(String title);
  7. }

实现类:

  1. /**
  2. * @description
  3. */
  4. @Component
  5. public class BookService implements IBookService{
  6. @Override
  7. @DkAnnotation //这里的注解会被匹配
  8. public void saveBook(String title){
  9. System.out.println("保存图书:"+title);
  10. }
  11. public void saveBook(String title,int count){
  12. System.out.println("保存"+title+","+count+"次");
  13. }
  14. }

修改Aspect类:

  1. /**
  2. * @description
  3. */
  4. @Component //将当前bean交给spring管理
  5. @Aspect //定义为一个AspectBean
  6. public class DkAspect {
  7. //使用@annotation配置匹配所有还有指定注解的方法
  8. @Pointcut("@annotation(com.st.dk.demo7.annotations.DkAnnotation)")
  9. private void pointCut1(){
  10. }
  11. //定义一个前置通知
  12. @Before("pointCut1()")
  13. private static void befor(){
  14. System.out.println("---前置通知---");
  15. }
  16. }

测试:

  1. @Test
  2. public void testAopPoint_annotation(){
  3. ApplicationContext ac =
  4. new AnnotationConfigApplicationContext(Appconfig.class);
  5. IBookService bean = ac.getBean(IBookService.class);
  6. bean.saveBook("程序员的修养");
  7. }

结果:
在这里插入图片描述

发表评论

表情:
评论列表 (有 0 条评论,19人围观)

还没有评论,来说两句吧...

相关阅读

    相关 SpringAop

    面向切面编程,使代码原有的从上倒下的执行顺序做出改变,加入了spring的Aop的面向切面编程这个思想,那么我们代码的执行顺序则不再是从上到下,而是走到了目标执行对象的时...

    相关 SpringAOP

    AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术。 举个现实中的例子:

    相关 springAop

    一、       Introduction 在软件业,AOP为Aspect Oriented Programming的缩写,意为:[面向切面编程][Link 1],通过[预编

    相关 SpringAOP

    AOP:面向切面编程,是Spring的两大基石之一。 AOP:作用 1、日志 按照以前的写法,会造成: 1)、代码混乱,越来越多的非业务需求(日志、验证等)加入后,原有

    相关 springAOP

    1、原理: 通过代理的模式为目标对象生产代理对象,并将横切逻辑插入到目标方法的执行前后。横切逻辑其实就是通知(Advice),Spring 提供了5种通知 2、AOP 术语

    相关 SpringAOP

    例子:方法性能监控,使用AOP后可以将监控和需要监控的类进行分离 JoinPoint 连接点      类的初始化前后、方法执行的前后,这些特定的点为连接点 Poi