AOP:注解开发
切面 = 切点+通知
引入依赖
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId>
<version>1.8.7</version>
</dependency>
通知类
//表示该类是切面类
@Aspect、
@Component
public class Notice {
//切点配置
@Pointcut("execution(* cn.itcast..*.*(..))")
public void pointcut(){};
@Before("pointcut()")
public void before(){//切点执行前执行
System.out.println("前置通知");
}
@AfterReturning("pointcut()")
public void afterReturn(){//切点返回结果之前执行,它和异常通知只能执行一个
System.out.println("后置通知");
}
@AfterThrowing(throwing = "e",pointcut = "pointcut()")
public void exception(Exception e){//出现异常,执行
System.out.println("异常通知"+e);
}
@Pointcut("pointcut()")
public void after(){//切点执行后执行,一定会执行
System.out.println("最终通知");
}
@Around(value = "pointcut()")
public void around(ProceedingJoinPoint joinPoint){//环绕通知
before();//以上的加和
try {
joinPoint.proceed();
afterReturn();
} catch (Throwable throwable) {
throwable.printStackTrace();
}
after();
}
}
~
配置文件
//开启注解aop
<aop:aspectj-autoproxy/>~