亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

Spring 中基于 AOP 的 XML架構

標簽:
Spring

Spring 中基于 AOP 的 XML架构

为了在本节的描述中使用 aop 命名空间标签,你需要导入 spring-aop j架构,如下所述:

<?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 ">    <!-- bean definition & AOP specific configuration --> </beans>

你还需要在你的应用程序的 CLASSPATH 中使用以下 AspectJ 库文件。这些库文件在一个 AspectJ 装置的 ‘lib’ 目录中是可用的,否则你可以在 Internet 中下载它们。(注:aspectjweaver.jar 已包含其他包)

  • aspectjrt.jar

  • aspectjweaver.jar

  • aspectj.jar

  • aopalliance.jar

 声明一个 aspect

一个 aspect 是使用 元素声明的,支持的 bean 是使用 ref 属性引用的,如下所示:

<aop:config>    <aop:aspect id="myAspect" ref="aBean">    ...    </aop:aspect> </aop:config> <bean id="aBean" class="..."> ... </bean>

这里,“aBean” 将被配置和依赖注入,就像前面的章节中你看到的其他的 Spring bean 一样。

声明一个切入点

一个切入点有助于确定使用不同建议执行的感兴趣的连接点(即方法)。在处理基于配置的 XML 架构时,切入点将会按照如下所示定义:

<aop:config>    <aop:aspect id="myAspect" ref="aBean">    <aop:pointcut id="businessService"       expression="execution(* com.xyz.myapp.service.*.*(..))"/>    ...    </aop:aspect> </aop:config> <bean id="aBean" class="..."> ... </bean>

下面的示例定义了一个名为 “businessService” 的切入点,该切入点将与 com.tutorialspoint 包下的 Student 类中的 getName() 方法相匹配:

<aop:config>    <aop:aspect id="myAspect" ref="aBean">    <aop:pointcut id="businessService"       expression="execution(* com.tutorialspoint.Student.getName(..))"/>    ...    </aop:aspect> </aop:config> <bean id="aBean" class="..."> ... </bean>

声明建议

你可以在<aop:aspect>中使用<aop:{通知类型名}>元素声明任意五中类型的通知,如下:

<aop:config>    <aop:aspect id="myAspect" ref="aBean">       <aop:pointcut id="businessService"          expression="execution(* com.xyz.myapp.service.*.*(..))"/>       <!-- a before advice definition -->       <aop:before pointcut-ref="businessService"           method="doRequiredTask"/>       <!-- an after advice definition -->       <aop:after pointcut-ref="businessService"           method="doRequiredTask"/>       <!-- an after-returning advice definition -->       <!--The doRequiredTask method must have parameter named retVal -->       <aop:after-returning pointcut-ref="businessService"          returning="retVal"          method="doRequiredTask"/>       <!-- an after-throwing advice definition -->       <!--The doRequiredTask method must have parameter named ex -->       <aop:after-throwing pointcut-ref="businessService"          throwing="ex"          method="doRequiredTask"/>       <!-- an around advice definition -->       <aop:around pointcut-ref="businessService"           method="doRequiredTask"/>    ...    </aop:aspect> </aop:config> <bean id="aBean" class="..."> ... </bean>


點擊查看更多內容
TA 點贊

若覺得本文不錯,就分享一下吧!

評論

作者其他優質文章

正在加載中
  • 推薦
  • 評論
  • 收藏
  • 共同學習,寫下你的評論
感謝您的支持,我會繼續努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進行掃碼打賞哦
今天注冊有機會得

100積分直接送

付費專欄免費學

大額優惠券免費領

立即參與 放棄機會
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號

舉報

0/150
提交
取消