Spring中的Aop-基於的xml配置

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>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章