Spring AOP(3)

来源:互联网 发布:ibm软件 编辑:程序博客网 时间:2024/06/05 08:20
Introductions 
  1:简介允许一个切面声明一个实现指定接口的通知对象,并且提供了一个接口实现类来代替这些对象
  2:由<aop:aspect>中的<aop:declare-parents>元素声明 ,该元素用于声明所匹配的类型拥有一个新的parent(因此得名)
 
  <aop:aspect id="myAspect" ref="aBean"
<aop:declare-parents
types-matching="com.zit.trf.*+"
implement-interface="com.dada.dada.dad.UsageTraceked"
default-impl="com.fs.fsdfdsfs.DefaultUsageTraceked" />

<aop:before
pointcut="com.asf.fdsfs.方法() and this(usageTracked)"
method="recordUsage"/>

</aop:aspect> 
 
 public void recordUsage(UsageTraceked usageTraceked){
  usageTraceked.incrementUseCount();
 }
 
 强制类型转换
 UsageTraceked usageTraceked=(UsageTraceked)context.getBean("myServices");
 
  所有的这种配置的切面只支持singleton model
 
  Advisors(看书)
  1:advisor就像一个小的自包含的方面,只有一个advice
  2:切面自身可以通过一个bean表示,并且必须实现某个advice接口,同时,advisior也可以很好的利用AspectJ的切入点表达式
  3:Spring通过配置文件中<aop:advisor>元素支持advisior实际配合使用,大多数情况下它会和transactional advice配合使用
  4:为了定义一个advisior的优先级以便让advice可以有序,可以使用order属性来定义advisior的顺序
 
 <aop:config>
  <aop:pointcut id="businessService"
  expression="execution(* com.fds.fs.service..(..))"/>
  <aop:advisor
  pointcut-ref="businessService"
  advice-ref="tx-advice"/>
 </aop:config>
 
 <tx:advice id="tx-advice">  对于事物的相关声明
  <tx:attributes>
  <tx:method name="*" propagation="REQUIRED"/>
  </tx:attributes>
 </tx:advice>
原创粉丝点击