目录
四种基本通知类型
前置通知,在切入点方法执行前执行
后置通知,在切入点方法执行后执行
异常通知,切入点方法执行异常后执行,它和后置通知只能执行一个
最终通知 无论是否发生异常 都执行
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- bean definitions here -->
<bean id="custormerservice" class="cn.pro.service.impl.ICustormerServiceImpl"></bean>
<bean id="logger" class="cn.pro.utils.Logger"></bean>
<!-- 使用aop的名称空间,并且使用aop:config开始aop的配置-->
<aop:config>
<!-- 定义通用的切入点表达式-->
<aop:pointcut expression="execution(* cn.pro.service.impl.*.*(..))" id="pt1"/>
<aop:pointcut expression="execution(* cn.pro.service.impl.*.*(..))" id="pt2"/>
<!-- 配置切面 -->
<aop:aspect id="logAdvice" ref="logger">
<!-- 4. 配置通知的类型 指定增强的方法何时执行 -->
<!-- <aop:pointcut expression="execution(* com.pro.service.impl.*.*(..))" id="pt1"/> -->
<!-- 前置通知,在切入点方法执行前执行 -->
<aop:before method="printLog" pointcut-ref="pt1"/>
<!-- 后置通知,在切入点方法执行后执行 -->
<aop:after method="printLogAfter" pointcut-ref="pt1"/>
<!-- 异常通知,切入点方法执行异常后执行,它和后置通知只能执行一个 -->
<aop:after-throwing method="exceptionAfter" pointcut-ref="pt1"/>
<!-- 最终通知 无论是否发生异常 都执行 -->
<aop:after-returning method="finalAfter" pointcut-ref="pt1"/>
</aop:aspect>
</aop:config>
</beans>
环绕通知
它是spring框架为我们提供的一种可以在代码中手动控制通知方法什么时候执行的方式
问题:
当配置了环绕通知后,切入点里面方法没有执行,而环绕通知里面的代码执行了
分析:由动态代理可知:环绕通知指的是invoke方法并且里面有明确的接入点方法调用,而我们现在方法没有明确的切入点调用
解决:
spring为我们提供了一个接口:proceedingjoinpoint,该接口可以作为环绕通知的方法参数来使用
在程序运行时,spring框架,会为我们提供该接口的实现类,供我们使用。
该接口中有一个方法,proceed(),它的作用就相当于method.invoke()方法,就是明确调用业务层核心方法,就是切入点方法。
环绕通知方法:
配置文件中配置:
<bean id="custormerservice" class="cn.pro.service.impl.ICustormerServiceImpl"></bean>
<bean id="logger" class="cn.pro.utils.Logger"></bean>
<aop:config>
<!-- 定义通用的切入点表达式-->
<aop:pointcut expression="execution(* cn.pro.service.impl.*.*(..))" id="pt1"/>
<aop:pointcut expression="execution(* cn.pro.service.impl.*.*(..))" id="pt2"/>
<!-- 配置切面 -->
<aop:aspect id="logAdvice" ref="logger">
<!-- 4. 配置通知的类型 指定增强的方法何时执行 -->
<!-- 配置环绕通知 -->
<aop:around method="roundadvice" pointcut-ref="pt1"/>
</aop:aspect>
</aop:config>
测试:
结果:
log4j:WARN No appenders could be found for logger (org.springframework.core.env.StandardEnvironment).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
环绕通知开始
记录日志前
删除了客户
记录日志后
最终要做的事情
环绕通知结束