Spring 2.X 中AOP的使用浅析

http://tech.ddvip.com   2007年09月01日    社区交流

内容摘要:Spring2.X下的切面有两种实现方式,一种是以Java文件定义切面(其只是普通的Java类),然后在配置文件中声明定义的切面;另一种是在Java类中引入和AOP相关的元数据(注释)。

< tx:advice id = " txAdvice " transaction - manager = " transactionManager " >
< tx:attributes >
< tx:method name = " get* " read - only = " true " />
< tx:method name = " find* " read - only = " true " />
< tx:method name = " * " />
</ tx:attributes >
</ tx:advice >
< aop:config >
< aop:pointcut id = " demoServiceMethods " expression = " execution(* hibernatesample.service.*.*(..)) " />
< aop:advisor advice - ref = " txAdvice " pointcut - ref = " demoServiceMethods " />
< aop:aspect id = " logAspect " ref = " logAspectTarget " >
< aop:pointcut id = " businessService " expression = " execution(* hibernatesample.service.*.*(..)) " />
< aop:after pointcut - ref = " businessService " method = " logMethod " />
</ aop:aspect >
</ aop:config >

  完成上面的工作相当于完成了 Spring1.X 的 自动代理。 我们接下来需要定义的任何 Service Bean 都可以很纯粹很纯粹了:

< bean id ="accountService" class ="hibernatesample.service.impl.AccountServiceImpl" >
< property name ="accountDAO" ref ="accountDAO" ></ property >
</ bean >

  第二种实现 AOP 的方式和第一种相比,只是在 LogingAspect 中加入了注释,而省去了配置文件中和 LogingAspect 相关的配置。重新编写的 LogingAspect 如下:

来源:javablog    作者:边城愚夫    责编:豆豆技术应用

正在加载评论...