在网上例子上改的,因为那个例子不完整,无法运行,呵呵。也算原创吧,嘻
我们使用一个简单的例子来演示一下Spring中的AOP,这是一个log的例子,实际上log是一个对于AOP来说很不好的例子,这里我们只为说明Spring AOP的使用。
一,首先我们来创建一个自己的interceptor。这个类必须继承org.aopalliance.intercept. MethodInterceptor接口。Spring的AOP框架就是参照aopalliance这个标准实现的,所以我们的MyInterceptor要继承这个标准中的接口。
这个接口只有一个要求实现的方法:
public Object invoke(MethodInvocation methodInvocation) throws Throwable;
下面是我们的MyIntercptor:
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
public class MyInterceptor
implements MethodInterceptor {
private final Log logger = LogFactory.getLog(getClass());
//调用方法
public Object invoke(MethodInvocation methodInvocation) throws Throwable {
logger.info("开始调用:" + methodInvocation.getMethod().getDeclaringClass() +
"." + methodInvocation.getMethod().getName() + "()");
long startTime = System.currentTimeMillis();
Object result = null;
try {
result = methodInvocation.proceed();
return result;
}
catch (Exception ex) {
ex.printStackTrace();
}finally{
logger.info("结束调用:" + methodInvocation.getMethod().getDeclaringClass() +
"." + methodInvocation.getMethod().getName() + "()");
logger.info("方法调用耗时:" + (System.currentTimeMillis() - startTime) + "ms");
}
return result;
}
}
对于上面的代码需要说明的是下面两行代码:
Object result = methodInvocation.proceed();
return result;
整个程序的流程是这样的:
1,先是执行在Object result = methodInvocation.proceed();前面的代码;
2,接着执行Object result = methodInvocation.proceed();,它把执行控制权交给了interceptor stack(拦截器栈)内的下一个interceptor,如果没有了就交给真正的业务方法;
3,然后执行return result;之前的代码;
4,最后执行return result;,它把控制权交回它之上的interceptor,如果没有了就退出interceptor stack。
二,写出我们的业务对象及其接口
为了方便我们的业务接口只有一个hello方法:
public interface BusinessInterface {
public void hello();
}
业务对象的代码如下:
public class BusinessInterfaceImpl implements BusinessInterface{
public void hello() {
System.out.println("hello Spring AOP.");
}
}
三,接下来,我们来看看如何使用我们的写的interceptor。
我们把业务对象作为AOP的target:
<bean id="businessTarget" class="com.rst.spring.testaop.BusinessInterfaceImpl"/>
接着在bean定义中声明interceptor:
<bean id="myInterceptor" class="com.rst.spring.testaop.MyInterceptor"/>
最后,我们来声明真正的业务对象,通过使用它的接口以及Spring的ProxyFactoryBean:
<bean id="businessBean"
class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="proxyInterfaces">
<value>com.rst.spring.testaop.BusinessInterface</value>
</property>
<property name="interceptorNames">
<list>
<value>myInterceptor</value>
<value>businessTarget</value>
</list>
</property>
</bean>
这里需要说明两点:
proxyInterfaces:就是我们的业务对象的实际接口;
interceptorNames:定义了所有interceptors的执行顺序,其中业务对象的target作为list的最后一个。记着一定要把业务对象的target放到list中,否则你的业务对象就不会工作。
aop_bean.xml代码如下:
<?xml version="1.0" encoding="UTF-8"?>
<!-- $Id: beans1.xml,v 1.1 2004/01/27 00:03:45 colins Exp $ -->
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="businessTarget" class="BusinessInterfaceImpl" />
<bean id="myInterceptor" class="MyInterceptor" />
<bean id="businessBean" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="proxyInterfaces">
<value>BusinessInterface</value>
</property>
<property name="interceptorNames">
<list>
<value>myInterceptor</value>
<value>businessTarget</value>
</list>
</property>
</bean>
</beans>
四,最后,写我们的测试类TestAOP
import org.springframework.core.io.ClassPathResource;
import org.springframework.beans.factory.xml.XmlBeanFactory;
public class TestAOP {
public static void main(String[] args){
ClassPathResource resource = new ClassPathResource("aop_bean.xml");
XmlBeanFactory beanFactory = new XmlBeanFactory(resource);
BusinessInterface businessBean = (BusinessInterface)beanFactory.getBean("businessBean");
businessBean.hello();
}
}
一切正常就可以在log上看到相应的信息了。
以下是附件源代码的执行效果:
********************************************************************************************************
2006-9-1 11:04:56 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [aop_bean.xml]
2006-9-1 11:04:56 org.springframework.beans.factory.support.AbstractBeanFactory getBean
信息: Creating shared instance of singleton bean 'businessBean'
2006-9-1 11:04:56 org.springframework.core.CollectionFactory <clinit>
信息: Using JDK 1.4 collections
2006-9-1 11:04:56 org.springframework.beans.factory.support.AbstractBeanFactory getBean
信息: Creating shared instance of singleton bean 'myInterceptor'
2006-9-1 11:04:56 org.springframework.aop.framework.ProxyFactoryBean createAdvisorChain
信息: Bean with name 'businessTarget' concluding interceptor chain is not an advisor class: treating it as a target or TargetSource
2006-9-1 11:04:56 org.springframework.beans.factory.support.AbstractBeanFactory getBean
信息: Creating shared instance of singleton bean 'businessTarget'
2006-9-1 11:04:56 MyInterceptor invoke
信息: 开始调用:interface BusinessInterface.hello()
2006-9-1 11:04:56 MyInterceptor invoke
信息: 结束调用:interface BusinessInterface.hello()
2006-9-1 11:04:56 MyInterceptor invoke
信息: 方法调用耗时:0ms
hello Spring AOP
********************************************************************************************************
源代码需要spring.jar, aopallience.jar, commons-logging.jar。