• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

Spring AOP通知获取数据参数,返回值,异常

武飞扬头像
看到我,请让我去学习
帮助1

获取参数(重要) 

主要是JoinPoint

获取返回值(讲了)

主要是ProceedingJoinPoint

获取异常(扫了一眼,不重要)

主要是参数啥的


由于代码都是在一块写的,所以就直接粘贴全部的代码了,供以后的我参考!!!

MyAdvise(主要是写Spring的通知)

  1.  
    package com.itheima.aop;
  2.  
     
  3.  
    import org.aspectj.lang.JoinPoint;
  4.  
    import org.aspectj.lang.ProceedingJoinPoint;
  5.  
    import org.aspectj.lang.annotation.*;
  6.  
    import org.springframework.stereotype.Component;
  7.  
     
  8.  
    import java.util.Arrays;
  9.  
     
  10.  
    @Component //让1配置类知道是bean
  11.  
    @Aspect //让配置类知道是造Aop,去识别一下的内容
  12.  
    public class MyAdvise {
  13.  
     
  14.  
    //1.定义切入点
  15.  
    @Pointcut("execution(* com.itheima.dao.BookDao.findName(..))")
  16.  
    private void a(){}
  17.  
     
  18.  
     
  19.  
    //@Before("a()")
  20.  
    public void before(JoinPoint joinPoint){
  21.  
    Object[] args = joinPoint.getArgs();
  22.  
    System.out.println(Arrays.toString(args));
  23.  
    }
  24.  
     
  25.  
    //@After("a()")
  26.  
    public void after(JoinPoint joinPoint){
  27.  
    Object[] args = joinPoint.getArgs();
  28.  
    System.out.println(Arrays.toString(args));
  29.  
    }
  30.  
     
  31.  
    // @Around("a()")
  32.  
    public Object around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
  33.  
    Object[] args = proceedingJoinPoint.getArgs();
  34.  
    System.out.println(Arrays.toString(args));
  35.  
    args[0]=66;//这里其实表示,我门在获取通知后,在发出数据之前,可以对数据进行操作,例如矫正传入数据的格式等等
  36.  
    Object proceed = proceedingJoinPoint.proceed(args);
  37.  
    return proceed;
  38.  
    }
  39.  
     
  40.  
    @AfterReturning(value = "a()",returning = "ret") //(获取返回值)
  41.  
    public void afterReturning(Object ret){
  42.  
    System.out.println("通知类中的afterThrowing被触发........ret为参数");
  43.  
    }
  44.  
     
  45.  
    @AfterThrowing(value = "a()",throwing = "throwable")//只有抛异常的时候才触发此通知
  46.  
    public void afterThrowing(Throwable throwable){
  47.  
    System.out.println("通知类中的afterThrowing被触发");
  48.  
    }
  49.  
    }
学新通

SpringConfig(Spring配置文件Java类)

  1.  
    package com.itheima.config;
  2.  
     
  3.  
    import org.springframework.context.annotation.ComponentScan;
  4.  
    import org.springframework.context.annotation.Configuration;
  5.  
    import org.springframework.context.annotation.EnableAspectJAutoProxy;
  6.  
     
  7.  
    @Configuration //说明此文件为Spring配置类
  8.  
    @ComponentScan("com.itheima") //包扫描,加载bean
  9.  
    @EnableAspectJAutoProxy //启动了MyAdvise内的Aspect注解,
  10.  
    public class SpringConfig {
  11.  
    }

BookDaoImpl(接口的实现类,关于异常还需要去实现类里面,手写一下,throws)

  1.  
    package com.itheima.dao.impl;
  2.  
     
  3.  
    import com.itheima.dao.BookDao;
  4.  
    import org.springframework.stereotype.Repository;
  5.  
     
  6.  
    @Repository
  7.  
    public class BookDaoImpl implements BookDao {
  8.  
     
  9.  
    @Override
  10.  
    public void update() {
  11.  
    System.out.println("update is running.......");
  12.  
    }
  13.  
     
  14.  
    @Override
  15.  
    public int select() {
  16.  
    System.out.println("select is running.......");
  17.  
    int i = 1/0;
  18.  
    return 100;
  19.  
    }
  20.  
     
  21.  
    @Override
  22.  
    public String findName(int id, String password) {
  23.  
    System.out.println("id为" id "密码为" password "");
  24.  
    if (true)throw new NullPointerException();
  25.  
    return "天王盖葫芦";
  26.  
    }
  27.  
     
  28.  
    }
学新通

App(mian方法主类)

  1.  
    package com.itheima;
  2.  
     
  3.  
    import com.itheima.config.SpringConfig;
  4.  
    import com.itheima.dao.BookDao;
  5.  
    import org.springframework.context.annotation.AnnotationConfigApplicationContext;
  6.  
     
  7.  
     
  8.  
    public class App {
  9.  
    public static void main(String[] args) {
  10.  
     
  11.  
    //获取Java配置类
  12.  
    AnnotationConfigApplicationContext acct = new AnnotationConfigApplicationContext(SpringConfig.class);
  13.  
     
  14.  
    //获取bean
  15.  
    BookDao bean = acct.getBean(BookDao.class);
  16.  
     
  17.  
    String s = bean.findName(100,"123456");
  18.  
     
  19.  
    System.out.println(s);
  20.  
    }
  21.  
    }
学新通

 总结:

其实在此之前我们就已经是,知道了参数与返回值是如何进行配置的,异常方面,老师也没有细讲,他说是不重要,只有是在异常被触发可才进行通知,感觉比较鸡肋,后期自己知道了,写代码的时候会抛什么异常才能对这个有所了解,现在就简单接触一下即可。

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhfkigeh
系列文章
更多 icon
同类精品
更多 icon
继续加载