Spring中的Aop有着几个定义分别是切面(aspect) 、切点(切入点)、Advice(通知)等
一、切面(Aspect)
定义:切入业务流程的一个独立的模块。我理解的就是如果A类的某些方法需要在B类方法之前或者之后实现,那么在XML中配置为A类( <aop:aspect id="log" ref="A类的bean id的名字">)。
二、通知(Advice)
不同的通知通常需要切入到不同的连接点上。我理解的是你要在哪些方法上运用前置或者后置方法。例如我向在B类的getName方法之前调用A类的beforAdvice方法,我就可以配置成
<aop:aspect id="log" ref="A类的名字">
<aop:pointcut expression="execution(* com.tutorialspoint.*.getName(..))" id="selectAll"/>
<aop:before pointcut-ref="selectAll" method="beforeAdvice"/>
三、切点
定义:如果通知定义了“什么”和“何时”。那么切点就定义了“何处”。切点会匹配通知所要织入的一个或者多个连接点。通常使用明确的类或者方法来指定这些切点。作用:定义通知被应用的位置(在哪些连接点)
四、Xml配置
<?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-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd ">
<aop:config>
<aop:aspect id="log" ref="logging">
<aop:pointcut expression="execution(* com.tutorialspoint.*.*(..))" id="selectAll"/>
<aop:before pointcut-ref="selectAll" method="beforeAdvice"/>
<aop:after method="afterAdvice" pointcut-ref="selectAll" />
<aop:after-returning method="afterReturingAdvice" returning="retVal" pointcut-ref="selectAll" />
<aop:after-throwing method="AfterThrowingAdvice" throwing="ex" pointcut-ref="selectAll"/>
</aop:aspect>
</aop:config>
<bean id="student" class="com.tutorialspoint.Student">
<property name="name" value="Zara"></property>
<property name="age" value="11"></property>
</bean>
<bean id="logging" class="com.tutorialspoint.logging"></bean>
<!-- Definition for student bean -->
</beans>