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

获取参数(重要) 

主要是JoinPoint

获取返回值(讲了)

主要是ProceedingJoinPoint

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

主要是参数啥的


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

MyAdvise(主要是写Spring的通知)

package com.itheima.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

import java.util.Arrays;

@Component    //让1配置类知道是bean
@Aspect       //让配置类知道是造Aop,去识别一下的内容
public class MyAdvise {

    //1.定义切入点
    @Pointcut("execution(* com.itheima.dao.BookDao.findName(..))")
    private void a(){}


    //@Before("a()")
    public void before(JoinPoint joinPoint){
        Object[] args = joinPoint.getArgs();
        System.out.println(Arrays.toString(args));
    }

    //@After("a()")
    public void after(JoinPoint joinPoint){
        Object[] args = joinPoint.getArgs();
        System.out.println(Arrays.toString(args));
    }

//    @Around("a()")
    public Object around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        Object[] args = proceedingJoinPoint.getArgs();
        System.out.println(Arrays.toString(args));
        args[0]=66;//这里其实表示,我门在获取通知后,在发出数据之前,可以对数据进行操作,例如矫正传入数据的格式等等
        Object proceed = proceedingJoinPoint.proceed(args);
        return proceed;
    }

    @AfterReturning(value = "a()",returning = "ret") //(获取返回值)
    public void afterReturning(Object ret){
        System.out.println("通知类中的afterThrowing被触发........ret为参数");
    }

    @AfterThrowing(value = "a()",throwing = "throwable")//只有抛异常的时候才触发此通知
    public void afterThrowing(Throwable throwable){
        System.out.println("通知类中的afterThrowing被触发");
    }
}

SpringConfig(Spring配置文件Java类)

package com.itheima.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;

@Configuration                   //说明此文件为Spring配置类
@ComponentScan("com.itheima")    //包扫描,加载bean
@EnableAspectJAutoProxy          //启动了MyAdvise内的Aspect注解,
public class SpringConfig {
}

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

package com.itheima.dao.impl;

import com.itheima.dao.BookDao;
import org.springframework.stereotype.Repository;

@Repository
public class BookDaoImpl implements BookDao {

    @Override
    public void update() {
        System.out.println("update is running.......");
    }

    @Override
    public int select() {
        System.out.println("select is running.......");
        int i = 1/0;
        return 100;
    }

    @Override
    public String findName(int id, String password) {
        System.out.println("id为"+id+"密码为"+password+"");
        if (true)throw new NullPointerException();
        return "天王盖葫芦";
    }

}

App(mian方法主类)

package com.itheima;

import com.itheima.config.SpringConfig;
import com.itheima.dao.BookDao;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;


public class App {
    public static void main(String[] args) {

        //获取Java配置类
        AnnotationConfigApplicationContext acct = new AnnotationConfigApplicationContext(SpringConfig.class);

        //获取bean
        BookDao bean = acct.getBean(BookDao.class);

        String s = bean.findName(100,"123456");

        System.out.println(s);
    }
}

 总结:

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

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

很丧

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值