Spring AOP之Hello World

们使用一个简单的例子来演示一下Spring中的AOP,这是一个log的例子,实际上log是一个对于AOP来说很不好的例子,这里我们只为说明SpringAOP的使用。
        一,首先我们来创建一个自己的interceptor。这个类必须继承org.aopalliance.intercept.MethodInterceptor接口。Spring的AOP框架就是参照aopalliance这个标准实现的,所以我们的MyInterceptor要继承这个标准中的接口。
        这个接口只有一个要求实现的方法:
publicObjectinvoke(MethodInvocationmethodInvocation)throwsThrowable;
下面是我们的MyIntercptor:
publicclassMyInterceptorimplementsMethodInterceptor{
privatefinalLoglogger=LogFactory.getLog(getClass());

publicObjectinvoke(MethodInvocationmethodInvocation)throwsThrowable{
logger.info("Beginningmethod(1):"+
methodInvocation.getMethod().getDeclaringClass()+"."+
methodInvocation.getMethod().getName()+"()");
longstartTime=System.currentTimeMillis();
try{
Objectresult=methodInvocation.proceed();
returnresult;
}finally{
logger.info("Endingmethod(1):"+
methodInvocation.getMethod().getDeclaringClass()+"."+
methodInvocation.getMethod().getName()+"()");
logger.info("Methodinvocationtime(1):"+
(System.currentTimeMillis()-startTime)+"ms.");
}
}
}
对于上面的代码需要说明的是下面两行代码:
Objectresult=methodInvocation.proceed();
returnresult;
整个程序的流程是这样的:
1,先是执行在Objectresult=methodInvocation.proceed();前面的代码;
2,接着执行Objectresult=methodInvocation.proceed();,它把执行控制权交给了interceptorstack(拦截器栈)内的下一个interceptor,如果没有了就交给真正的业务方法;
3,然后执行returnresult;之前的代码;
4,最后执行returnresult;,它把控制权交回它之上的interceptor,如果没有了就退出interceptorstack。
二,写出我们的业务对象及其接口
为了方便我们的业务接口只有一个hello方法:
publicinterfaceBusinessInterface{
publicvoidhello();
}
业务对象的代码如下:
publicclassBusinessInterfaceImplimplementsBusinessInterface{
publicvoidhello(){
System.out.println("helloSpringAOP.");
}
}
三,接下来,我们来看看如何使用我们的写的interceptor。
我们把业务对象作为AOP的target:
接着在bean定义中声明interceptor:
最后,我们来声明真正的业务对象,通过使用它的接口以及Spring的ProxyFactoryBean:
com.rst.spring.testaop.BusinessInterface
myInterceptor
businessTarget
这里需要说明两点:
proxyInterfaces:就是我们的业务对象的实际接口;
interceptorNames:定义了所有interceptors的执行顺序,其中业务对象的target作为list的最后一个。记着一定要把业务对象的target放到list中,否则你的业务对象就不会工作。
四,最后,写我们的测试类
ClassPathResourceresource=
newClassPathResource("com/rst/spring/testaop/aop_bean.xml");
XmlBeanFactorybeanFactory=newXmlBeanFactory(resource);
BusinessInterfacebusinessBean=
(BusinessInterface)beanFactory.getBean("businessBean");
businessBean.hello();
一切正常就可以在log上看到相应的信息了。
以下是附件源代码的执行效果:
2004-09-0816:04:51,210INFO-Beginningmethod(1):interfacecom.rst.spring.testaop.BusinessInterface.hello()
2004-09-0816:04:51,210INFO-Beginningmethod(2):interfacecom.rst.spring.testaop.BusinessInterface.hello()
helloSpringAOP.
2004-09-0816:04:51,210INFO-Endingmethod(2):interfacecom.rst.spring.testaop.BusinessInterface.hello()
2004-09-0816:04:51,210INFO-Endingmethod(1):interfacecom.rst.spring.testaop.BusinessInterface.hello()
2004-09-0816:04:51,210INFO-Methodinvocationtime(1):0ms.
源代码需要spring.jar,aopallience.jar,commons-logging.jar。
SpringAOP是一个很方便高效的框架,尤其在事务管理中使用到很多,希望能和大家交流。
 『 点击下载 』

欢迎讨论SpringFramework相关的问题:
http://www.matrix.org.cn/forum_list.asp?forum_id=26

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值