• 當前位置:首頁 > IT技術 > 編程語言 > 正文

    Spring AOP之3w的配置
    2022-03-06 17:57:41


    1、3w是什么?

    ■ what、where、when 或者 what、when、where



    2、what、where、when【通用】

    (1)what:增強器-bean【配置一個bean對象】

    (2)where:被增強的連接點-aop:pointcut【配置被增強的方法的屬性-expression】

    (3)when: 被增強的時機-aop:before/after-returning/after-throwing/after/around【前置、后置、異常、最終、環繞】


    ★ 3w 之間的關聯:

    ■ where 和 when 同處在 元素aop:aspect 內部:

    【where、when】整體-關聯 what 是通過 元素aop:aspect 的屬性 ref 關聯是bean的增強器(what)

    ■ 內部的 when 和 where 之間:when 通過 pointcut-ref 關聯到 where

    	<!-- AOP 配置:what、where、when -->
    	<!-- 1、what:做什么增強 -->
    	<bean id="transactionManager" class="com.shan.tx.TransactionManager"/>
    	
    	<aop:config>
    		<!-- 配置AOP切面 --> 
    		<aop:aspect ref="transactionManager"> <!-- ? 關聯what -->
    			<!-- 2、where:在哪些包中的哪些類中的哪些方法上做增強 -->
    			<aop:pointcut id="txPoint" expression="execution(* com.shan.service..*Service*.*(..))"/>
    			<!-- 3、when:在方法執行的什么時機做增強 -->
    			<aop:before method="open" pointcut-ref="txPoint"/>  <!-- ? 關聯where -->
    		</aop:aspect>
    	</aop:config>
    




    3、what、when、where【事務管理器特有】

    (1)what:增強器-bean【配置一個bean對象】

    (2)when: 被增強的時機(事務環繞增強特有)-tx:advice

    (3)where:被增強的連接點-aop:pointcut【配置被增強的方法的屬性-expression】


    ★ 3w 之間的關聯:

    ■ when 和 what 之間:when 通過 (事務環繞增強特有)-tx:advice的屬性 transaction-manager 關聯是bean的增強器(what)

    ■ where 和 when 同處在 元素aop:config 內部:

    通過元素aop:config的子元素aop:pointcut關聯到where,然后又通過元素aop:config的子元素aop:advisor關聯到when

    	<!-- 1、what:配置jdbc事務管理器 -->
    	<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    		<property name="dataSource" ref="dataSource"/>
    	</bean>
    	
    	<!-- 2:when:配置事務管理器增強(環繞增強) --><!-- ? 關聯what -->
    	<tx:advice id="txAdvice" transaction-manager="txManager">
    		<tx:attributes>
    			<tx:method name="trans"/>
    		</tx:attributes>
    	</tx:advice>
    	
    	<!-- 3、where:配置切面 -->
    	<aop:config>
    		<aop:pointcut id="txPc" expression="execution(* com.shan.service.*Service.*(..))" />
    		<aop:advisor advice-ref="txAdvice" pointcut-ref="txPc"/><!-- ? 關聯when -->
    	</aop:config>
    

    本文摘自 :https://www.cnblogs.com/

    開通會員,享受整站包年服務
    国产呦精品一区二区三区网站|久久www免费人咸|精品无码人妻一区二区|久99久热只有精品国产15|中文字幕亚洲无线码