通用化切入点表达式
原配置文件
- AOP的切点表达式配置繁琐
<?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">
<!--Spring的IOC配置:把service对象配置起来-->
<!--1. 有一个需要增强方法的service,需要加个日志-->
<bean id="accountService" class="cn.luis.service.impl.AccountServiceImpl"></bean>
<!--配置Logger类-->
<!--2. 写了一个日志的通知,它里面有加日志的方法-->
<bean id="logger" class="cn.luis.util.Logger"></bean>
<!--配置AOP-->
<aop:config>
<!--配置切面-->
<!--3. 配置的这个切面引用了我们配置的通知2-->
<aop:aspect id="logAdvice" ref="logger">
<!--配置前置通知:在切入点方法之前执行-->
<aop:before method="beforePrintLog" pointcut="execution( public void cn.luis.service.impl.AccountServiceImpl.saveAccount())"></aop:before>
<!--配置后置通知:在切入点方法正常执行之后执行【后置通知和异常通知只能执行一个】-->
<aop:after-returning method="aftereReturnPrintLog" pointcut="execution( public void cn.luis.service.impl.AccountServiceImpl.saveAccount())"></aop:after-returning>
<!--配置异常通知:在切入点方法产生异常后执行-->
<aop:after-throwing method="afterThrowingPrintLog" pointcut="execution( public void cn.luis.service.impl.AccountServiceImpl.saveAccount())"></aop:after-throwing>
<!--配置最终通知:无论切入点方法是否正常执行,它都会执行-->
<aop:after method="afterPrintLog" pointcut="execution( public void cn.luis.service.impl.AccountServiceImpl.saveAccount())"></aop:after>
</aop:aspect>
</aop:config>
</beans>
通用化切入点表达式
1. 配置切入点表达式
<!--配置切入点表达式-->
<aop:pointcut id="pt" expression="execution( * cn.luis.service.impl.*.*(..))"/>
2.切入点表达式位置
- 在
<aop:aspect>
标签内部时,只能当前切面使用此表达式
<aop:config>
<!--配置切面-->
<aop:aspect id="logAdvice" ref="logger">
<!--配置前置通知-->
<aop:before method="beforePrintLog" pointcut="execution( * cn.luis.service.impl.*.*(..))"></aop:before>
<!--配置后置通知-->
<aop:after-returning method="aftereReturnPrintLog" pointcut="execution( * cn.luis.service.impl.*.*(..))"></aop:after-returning>
<!--配置异常通知-->
<aop:after-throwing method="afterThrowingPrintLog" pointcut="execution( * cn.luis.service.impl.*.*(..))"></aop:after-throwing>
<!--配置最终通知-->
<aop:after method="afterPrintLog" pointcut="execution( * cn.luis.service.impl.*.*(..))"></aop:after>
<!--配置切入点表达式-->
<aop:pointcut id="pt" expression="execution( * cn.luis.service.impl.*.*(..))"/>
</aop:aspect>
</aop:config>
-
在
<aop:aspect>
标签前面时,所有切面都可以使用此表达式【写在
<aop:aspect>
标签后面会报错,要遵循xml里定义的语法规则】
<aop:config>
<!--配置切入点表达式-->
<aop:pointcut id="pt" expression="execution( * cn.luis.service.impl.*.*(..))"/>
<!--配置切面-->
<aop:aspect id="logAdvice" ref="logger">
<!--配置前置通知-->
<aop:before method="beforePrintLog" pointcut="execution( * cn.luis.service.impl.*.*(..))"></aop:before>
<!--配置后置通知-->
<aop:after-returning method="aftereReturnPrintLog" pointcut="execution( * cn.luis.service.impl.*.*(..))"></aop:after-returning>
<!--配置异常通知-->
<aop:after-throwing method="afterThrowingPrintLog" pointcut="execution( * cn.luis.service.impl.*.*(..))"></aop:after-throwing>
<!--配置最终通知-->
<aop:after method="afterPrintLog" pointcut="execution( * cn.luis.service.impl.*.*(..))"></aop:after>
</aop:aspect>
</aop:config>