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;
-
-
//让1配置类知道是bean
-
//让配置类知道是造Aop,去识别一下的内容
-
public class MyAdvise {
-
-
//1.定义切入点
-
-
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;
-
}
-
-
//(获取返回值)
-
public void afterReturning(Object ret){
-
System.out.println("通知类中的afterThrowing被触发........ret为参数");
-
}
-
-
//只有抛异常的时候才触发此通知
-
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;
-
-
//说明此文件为Spring配置类
-
//包扫描,加载bean
-
//启动了MyAdvise内的Aspect注解,
-
public class SpringConfig {
-
}
BookDaoImpl(接口的实现类,关于异常还需要去实现类里面,手写一下,throws)
-
package com.itheima.dao.impl;
-
-
import com.itheima.dao.BookDao;
-
import org.springframework.stereotype.Repository;
-
-
-
public class BookDaoImpl implements BookDao {
-
-
-
public void update() {
-
System.out.println("update is running.......");
-
}
-
-
-
public int select() {
-
System.out.println("select is running.......");
-
int i = 1/0;
-
return 100;
-
}
-
-
-
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);
-
}
-
}
总结:
其实在此之前我们就已经是,知道了参数与返回值是如何进行配置的,异常方面,老师也没有细讲,他说是不重要,只有是在异常被触发可才进行通知,感觉比较鸡肋,后期自己知道了,写代码的时候会抛什么异常才能对这个有所了解,现在就简单接触一下即可。
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /boutique/detail/tanhfkigeh
同类精品
更多
-
photoshop保存的图片太大微信发不了怎么办
PHP中文网 06-15 -
Android 11 保存文件到外部存储,并分享文件
Luke 10-12 -
word里面弄一个表格后上面的标题会跑到下面怎么办
PHP中文网 06-20 -
《学习通》视频自动暂停处理方法
HelloWorld317 07-05 -
photoshop扩展功能面板显示灰色怎么办
PHP中文网 06-14 -
微信公众号没有声音提示怎么办
PHP中文网 03-31 -
excel下划线不显示怎么办
PHP中文网 06-23 -
怎样阻止微信小程序自动打开
PHP中文网 06-13 -
excel打印预览压线压字怎么办
PHP中文网 06-22 -
TikTok加速器哪个好免费的TK加速器推荐
TK小达人 10-01