在method方法上添加@Before注解
@Component
@Aspect
public class MyAdvice {
/**
* 定义切入点
*/
@Pointcut("execution(int com.itheima.dao.BookDao.update())")
public void pt(){}
/**
* 定义一个共性方法
* 切面 绑定通知 和 切入点
*/
@Before("pt()")
public void method(){
System.out.println(System.currentTimeMillis());
}
}

在method方法上添加@After注解
@Component
@Aspect
public class MyAdvice {
/**
* 定义切入点
*/
@Pointcut("execution(int com.itheima.dao.BookDao.update())")
public void pt(){}
/**
* 定义一个共性方法
* 切面 绑定通知 和 切入点
*/
@After("pt()")
public void method(){
System.out.println(System.currentTimeMillis());
}
}

在方法上方加上@Around注解;
@Component
@Aspect
public class MyAdvice {
/**
* 定义切入点
*/
@Pointcut("execution(int com.itheima.dao.BookDao.update())")
public void pt(){}
/**
* 定义一个共性方法
* 切面 绑定通知 和 切入点
* 执行环绕通知的时候ProceedingJoinPoint pjp参数一定要有,只有通过该参数调用pip.proceed方法才能执行原始方法的内容。
* pip.proceed()调用原始方法会有一个Object res类型的返回值,如果原始方法有返回值则res对该返回值进行接收并且可以进行返回,如果没有返回值则res=null.
*/
@Around("pt()")
public Object method(ProceedingJoinPoint pjp) throws Throwable {
System.out.println("before around ....");
// 表示对原始操作的调用
Object res = pjp.proceed();
System.out.println("原始方法中的返回值为:" + res);
System.out.println("after around ....");
return res;
}
}

环绕通知注意事项
在方法上方加上@AfterReturning注解;
@Component
@Aspect
public class MyAdvice {
/**
* 定义切入点
*/
@Pointcut("execution(int com.itheima.dao.BookDao.update())")
public void pt(){}
/**
* 定义一个共性方法
* 切面 绑定通知 和 切入点
*/
@AfterReturning("pt()")
public void method() throws Throwable {
System.out.println("AfterReturning .... ");
}
}

在方法上方加上@AfterThrowing注解;
@Component
@Aspect
public class MyAdvice {
/**
* 定义切入点
*/
@Pointcut("execution(int com.itheima.dao.BookDao.update())")
public void pt(){}
/**
* 定义一个共性方法
* 切面 绑定通知 和 切入点
*/
@AfterThrowing("pt()")
public void method() throws Throwable {
System.out.println("AfterThrowing .... ");
}
}

知识点1:@Before
| 名称 | @Before |
|---|---|
| 类型 | 方法注解 |
| 位置 | 通知方法定义上方 |
| 作用 | 设置当前通知方法与切入点之间的绑定关系,当前通知方法在原始切入点方法前运行 |
知识点2:@After
| 名称 | @After |
|---|---|
| 类型 | 方法注解 |
| 位置 | 通知方法定义上方 |
| 作用 | 设置当前通知方法与切入点之间的绑定关系,当前通知方法在原始切入点方法后运行 |
知识点3:@Around
| 名称 | @Around |
|---|---|
| 类型 | 方法注解 |
| 位置 | 通知方法定义上方 |
| 作用 | 设置当前通知方法与切入点之间的绑定关系,当前通知方法在原始切入点方法前后运行 |
知识点4:@AfterReturning
| 名称 | @AfterReturning |
|---|---|
| 类型 | 方法注解 |
| 位置 | 通知方法定义上方 |
| 作用 | 设置当前通知方法与切入点之间绑定关系,当前通知方法在原始切入点方法正常执行完毕后执行 |
知识点5:@AfterThrowing
| 名称 | @AfterThrowing |
|---|---|
| 类型 | 方法注解 |
| 位置 | 通知方法定义上方 |
| 作用 | 设置当前通知方法与切入点之间绑定关系,当前通知方法在原始切入点方法运行抛出异常后执行 |