在应用系统开发过程中,我们通常需要对系统的运行性能有所把握,特别是对于关键业
务逻辑的执行效能,而对于执行效能中的执行时间,则可能是重中之重。
我们这里的实例的实现目标,就是打印出目标Bean中方法的执行时间。
首先,围绕开篇中提到的几个重要概念,我们来看看Spring中对应的实现。
1. 切点(PointCut)
一系列连接点的集合,它指明处理方式(Advice)将在何时被触发。
对于我们引用开发而言,“何时触发”的条件大多是面向Bean的方法进行制定。实
际上,只要我们在开发中用到了Spring的配置化事务管理,那么就已经进行了PointCut
设置,我们可以指定对所有save开头的方法进行基于AOP的事务管理:
<property name="transactionAttributes"> <props> <prop key="save*">PROPAGATION_REQUIRED</prop> </props> </property>
同样,对于我们的AOP组件而言,我们也可以以方法名作为触发判定条件。
我们可以通过以下节点,为我们的组件设定触发条件。
<bean id="myPointcutAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdv isor"> <property name="advice"> <ref local="MyInterceptor" /> </property> <property name="patterns"> <list> <value>.*do.*</value> <value>.*execute.*</value> </list> </property> </bean>
RegexpMethodPointcutAdvisor是Spring中提供的,通过逻辑表达式指定方法
判定条件的支持类。其中的逻辑表达式解析采用了Apache ORO组件实现,关于逻
辑表达式的语法请参见Apache ORO文档。
上面我们针对MyInterceptor设定了一个基于方法名的触发条件,也就是说,当
目标类的指定方法运行时,MyInterceptor即被触发。
MyInterceptor是我们对应的AOP逻辑处理单元,也就是所谓的Advice。
2. Advice
Spring中提供了以下几种Advice:
1. Interception around advice
Spring中最基本的Advice类型,提供了针对PointCut的预处理、后处理过程
支持。
我们将使用Interception around advice完成这里的实例。
2. Before advice
仅面向了PointCut的预处理。
3. Throws advice
仅面向PointCut的后处理过程中的异常处理。
4. After Returning advice
仅面向PointCut返回后的后处理过程。
5. Introduction advice
Spring中较为特殊的一种Advice,仅面向Class层面(而不像上述Advice面
向方法层面)。通过Introduction advice我们可以实现多线程访问中的类锁
定。
Spring中采用了AOP联盟(AOP Alliance)12的通用AOP接口(接口定义位于
aopalliance.jar)。这里我们采用aopalliance.jar中定义的MethodInterceptor作为
我们的Advice实现接口:
MethodTimeCostInterceptor.java
package aop.sample;
import java.io.Serializable;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class MethodTimeCostInterceptor implements MethodInterceptor,Serializable
{
protected static final Log logger = LogFactory.getLog(MethodTimeCostInterceptor.class);
public Object invoke(MethodInvocation invocation) throws
Throwable {
long time = System.currentTimeMillis();
Object rval = invocation.proceed();
time = System.currentTimeMillis() - time;
logger.info("Method Cost Time => " + time + " ms");
return rval;
}
}
对应配置如下:
<bean id="MyInterceptor" class="aop.sample.MethodTimeCostInterceptor" />
除此之外,我们还需要定义一个Spring AOP ProxyFactory用以加载执行AOP组件。
定义如下:
<bean id="myAOPProxy" class="org.springframework.aop.framework.ProxyFactoryBean"> <property name="proxyInterfaces"> <value>aop.sample.ITest</value> </property> <!-- 是否强制使用CGLIB进行动态代理 <property name="proxyTargetClass"> <value>true</value> </property> --> <property name="target"> <ref local="test" /> </property> <property name="interceptorNames"> <value>myPointcutAdvisor</value> </property> </bean> <bean id="test" class="aop.sample.Test" />
其中的test是我们用于测试的一个类,它实现了ITest接口。
ITest.java
package aop.sample;
public interface ITest {
public abstract void doTest();
public abstract void executeTest();
}
通过以上工作,我们的MyInterceptor即被加载,并将在Test.doTest和
Test.executeTest方法调用时被触发,打印出这两个方法的执行时间。
Test.java
package aop.sample;
public class Test implements ITest {
public void doTest() {
// TODO Auto-generated method stub
int count= 0;
for (int i=0;i<10000;i++){
count++;
}
System.out.println("excute doTest!!!!!");
}
public void executeTest() {
// TODO Auto-generated method stub
int count =0;
for (int i=0;i<25000;i++){
count++;
}
System.out.println("excute executeTest!!!!!");
}
}
通过以上工作,我们的MyInterceptor即被加载,并将在Test.doTest和
Test.executeTest方法调用时被触发,打印出这两个方法的执行时间。
testAOP.java
package aop.sample;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
public class testAOP {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
ApplicationContext ctx=new
FileSystemXmlApplicationContext("/src/aop/sample/bean.xml");
ITest test = (ITest) ctx.getBean("myAOPProxy");
test.doTest();
test.executeTest();
}
}