在bean配置文件中,所有的Spring AOP配置都必须定义在<aop:config>
元素内部。对于每个切面而言,都要创建一个<aop:aspect>
元素来为具体的切面实现引用后端bean实例。 ?
切面bean必须有一个标识符,供<aop:aspect>
元素引用。
<bean id="calculatorLoggingAspect" class="com.jdy.spring2020.aop.CalculatorLoggingAspect"/> <aop:config> <aop:aspect id="calculatorLoggingAspect" order="0" ref="calculatorLoggingAspect"> </aop:aspect> </aop:config>
切入点使用<aop:pointcut>元素声明。
切入点必须定义在<aop:aspect>元素下,或者直接定义在<aop:config>元素下。
定义在<aop:aspect>元素下:只对当前切面有效
定义在<aop:config>元素下:对所有切面都有效
基于XML的AOP配置不允许在切入点表达式中用名称引用其他切入点。
<bean id="calculatorLoggingAspect" class="com.jdy.spring2020.aop.CalculatorLoggingAspect"/> <aop:config> <aop:pointcut id="pointcut01" expression="execution( com.jdy.spring2020.scan.service.impl.ArithmeticCalculatorImpl.add(int,int))"/> <aop:aspect id="calculatorLoggingAspect" order="0" ref="calculatorLoggingAspect"> </aop:aspect> </aop:config>
在aop名称空间中,每种通知类型都对应一个特定的XML元素。
通知元素需要使用<pointcut-ref>来引用切入点,或用<pointcut>直接嵌入切入点表达式。
method属性指定切面类中通知方法的名称
<bean id="calculatorLoggingAspect" class="com.jdy.spring2020.aop.CalculatorLoggingAspect"/> <aop:config> <aop:pointcut id="pointcut01" expression="execution( com.jdy.spring2020.scan.service.impl.ArithmeticCalculatorImpl.add(int,int))"/> <aop:aspect id="calculatorLoggingAspect" order="0" ref="calculatorLoggingAspect"> <aop:after method="after" pointcut-ref="pointcut01">
</aop:after> </aop:aspect> </aop:config>
原文:https://www.cnblogs.com/jdy1022/p/13678278.html