實踐了《指南》中的例子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類有關系。
待續。