继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

Spring 中基于 AOP 的 XML架构

慕田峪4524236
关注TA
已关注
手记 204
粉丝 19
获赞 52

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>


打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP