Spring学习笔记 (六) 在Spring中使用AOP例子

实践了《指南》中的例子165页到169页,最后补充一个Action,
public class AOPMethodCostTimeAction extends ActionSupport {

 public ActionForward execute(ActionMapping mapping, ActionForm form,
   HttpServletRequest request, HttpServletResponse response)
   throws Exception {
  WebApplicationContext wac = this.getWebApplicationContext();
  ITest dao =(ITest)wac.getBean("myAOPProxyFactory");
  dao.doTest();
  dao.executeTest();
  return mapping.findForward("success");
 }

}
具体代码已经放到gmail邮箱了。

仅仅从这个例子总结一下:

1,配置文件中需要
<!-- aop test-->
 <bean id="myPointcutAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
  <property name="advice">
   <ref local="MyInterceptor"/>
  </property>
  <property name="patterns">
   <list>
    <value>.*do.*</value>
    <value>.*execute.*</value>
   </list>
  </property>
 </bean>
 <!-- user by PointcutAdvisor -->
 <bean id="MyInterceptor" class="srx.test.interceptor.MethodTimeCostInterceptor"></bean>
 <!-- proxy factory user by Actoin -->
 <bean id="myAOPProxyFactory" class="org.springframework.aop.framework.ProxyFactoryBean">
  <property name="proxyInterfaces">
   <value>srx.test.interfaces.ITest</value>
  </property>
  <property name="target">
   <ref local="test"/>
  </property>
  <property name="interceptorNames">
   <value>myPointcutAdvisor</value>
  </property>
 </bean>
 <!-- the dao bean be proxyed-->
 <bean id="test" class="srx.test.impl.test"></bean>

 

 2,struts-config.xml

<!-- AOP test , this is a action call two function ,and pring every method proced cost time-->
  <action path="/methodcost" type="srx.test.struts.action.AOPMethodCostTimeAction">
    <forward name="success" path="/WEB-INF/jsp/srx/test/aop/success.jsp"/>
  </action>

使用动态代理,我本来有ITest接口,及其实现类test。以及使用test的Action。现在,我想测试test

种方法的调用时间,我需要怎么做呢?

我只需要多定义三个Bean,myPointcutAdvisor,MyInterceptor,myAOPProxyFactory。

三者的依赖关系

myAOPProxyFactory [ myPointcutAdvisor MyInterceptor  ] ]

其中advice定义和具体的Spring advice类有关系。

待续。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值