AOP使用详解
首先创建maven项目
添加依赖在pom.xml里

创建三层结构和spring.xml文件,只要用到注解就得写扫描包在spring.xml里

上篇文章的知识点总结




对上篇文章excution详细解释

如果把前置通知修改成这个代表只有带有@Logger注解的才会生效


合并注解的方法用&&

在before或者其他通知上加入JoinPoint 可以获取执行的方法名字和参数使得日志更加有意义

在后置放回通知上获取返回值的方法,在@AfterReturning里面添加returning="returnValue",方法里面添加参数Object returnValue

在后置异常通知上添加throwing="ex",在方法上添加Exception ex,用来获取异常信息

获取异常栈信息的方法如下图所示,就是下下图的红字信息打印到控制台上了


引用切点的方式让其他通知引用,意思是把execution放入到方法用,在其他的通知里引用此方法

可以获取bean方法上注解的信息,用@annotation加上通知里参数名字注意通知里的参数不能随便写


环绕通知的写法如下图所示

用基于xml和注解的方式开启AOP ref为要引用的增强类(日志类),id是expression写的内容的名字可以用id引用如pointcut
&;&意识是java里的&&,&;&是xml里的书写格式
@annotation(logger)是类中的注解

Spring通知有哪些类型
@Before
@After
@AfterThrowing
@AfterReturning
@Around
解释基于XML SChema(xml)方式的切面实现

解释基于注解的切面实现
整篇文章是基于注解的切面实现