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