`
junli0411
  • 浏览: 135865 次
  • 来自: ...
社区版块
存档分类
最新评论

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类有关系。

待续。

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics