分享
 
 
 

Spring AOP之Hello World

王朝other·作者佚名  2008-05-31
窄屏简体版  字體: |||超大  

我们使用一个简单的例子来演示一下Spring中的AOP,这是一个log的例子,实际上log是一个对于AOP来说很不好的例子,这里我们只为说明Spring AOP的使用。

1.首先我们来创建一个自己的interceptor

这个类必须继续org.aopalliance.intercept. MethodInterceptor接口。Spring的AOP框架就是参照aopalliance这个标准实现的,所以我们的MyInterceptor要继续这个标准中的接口。

这个接口只有一个要求实现的方法:

public Object invoke(MethodInvocation methodInvocation) throws Throwable;

下面是我们的MyIntercptor:

public class MyInterceptor implements MethodInterceptor {

private final Log logger = LogFactory.getLog(getClass());

public Object invoke(MethodInvocation methodInvocation) throws Throwable {

logger.info("Beginning method (1): " +

methodInvocation.getMethod().getDeclaringClass() + "." +

methodInvocation.getMethod().getName() + "()");

long startTime = System.currentTimeMillis();

try{

Object result = methodInvocation.proceed();

return result;

}finally{

logger.info("Ending method (1): " +

methodInvocation.getMethod().getDeclaringClass() + "." +

methodInvocation.getMethod().getName() + "()");

logger.info("Method invocation time (1): " +

(System.currentTimeMillis() - startTime) + " ms.");

}

}

}

对于上面的代码需要说明的是下面两行代码:

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。

2.写出我们的业务对象及其接口

为了方便我们的业务接口只有一个hello方法:

public interface BusinessInterface {

public void hello();

}

业务对象的代码如下:

public class BusinessInterfaceImpl implements BusinessInterface{

public void hello() {

System.out.println("hello Spring AOP.");

}

}

3.接下来,我们来看看如何使用我们的写的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"

<valuecom.rst.spring.testaop.BusinessInterface</value

</property

<property name="interceptorNames"

<list

<valuemyInterceptor</value

<valuebusinessTarget</value

</list

</property

</bean

这里需要说明两点:

proxyInterfaces:就是我们的业务对象的实际接口;

interceptorNames:定义了所有interceptors的执行顺序,其中业务对象的target作为list的最后一个。记着一定要把业务对象的target放到list中,否则你的业务对象就不会工作。

4.最后,写我们的测试类

ClassPathResource resource =

new ClassPathResource("com/rst/spring/testaop/aop_bean.XML");

XmlBeanFactory beanFactory = new XmlBeanFactory(resource);

BusinessInterface businessBean =

(BusinessInterface) beanFactory.getBean("businessBean");

businessBean.hello();

一切正常就可以在log上看到相应的信息了。

以下是附件源代码的执行效果:

2004-09-08 16:04:51,210 INFO - Beginning method (1): interface com.rst.spring.testaop.BusinessInterface.hello()

2004-09-08 16:04:51,210 INFO - Beginning method (2): interface com.rst.spring.testaop.BusinessInterface.hello()

hello Spring AOP.

2004-09-08 16:04:51,210 INFO - Ending method (2): interface com.rst.spring.testaop.BusinessInterface.hello()

2004-09-08 16:04:51,210 INFO - Ending method (1): interface com.rst.spring.testaop.BusinessInterface.hello()

2004-09-08 16:04:51,210 INFO - Method invocation time (1): 0 ms.

源代码需要spring.jar, aopallience.jar, commons-logging.jar。

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有