Spring 2.0中的AOP实现-aspectj-Advice
在Spring 2.0中,除了传统的通过实现AOP AIP的方式来实现Advice之外,还提供了两种更加简便的方式来实现Advice:1)基于XML Schema的设置;2)基于Annotation的支持,采用这两种方式,Advice将不用实现特定的接口。现在让我们来看看如何使用这两种方式来分别实现Before Advice、After Advice、Around Advice、Throwing Advice。 一、Before Advice:基于XML Schema 当基于XML Schema实现Before Advice时,你的Advice类不用实现org.springframework.aop.MethodBeforeAdvice接口,例如:
二、Before Advice:基于Annotation 使用Annotation来实现Advice,在XML文件上的定义要比基于XML Sechema的方法要简便的多,但在实现Before Advice类时,则需要使用到@Aspect、@Before标识,并需要引入org.aspectj.lang.annotation包中的类。还以LogBeforeAdvice为例,LogBeforeAdvice类需要改为:
三、After Advice:基于XML Sechma 和Before Advice一样,基于XML Sechma实现After Returning Advice时,不再需要org.springframework.aop.AfterReturningAdvice接口:
四、After Advice:基于Annotation 和Before Advice相似,使用@AfterReturning来表示After Returning Advice:
五、Around Advice:基于XML Sechma 在Spring 2.0中,Around Advice不用实现org.aoplliance.intercept.MethodInterceptor接口,但Advice的方法必须返回对象,并且必须定义一个ProceedingJoinPoint参数,例如:
六、Around Advice:基于Annotation 和Before Advice相似,使用@Around来表示Around Advice:
七、Throw Advice:基于XML Sechma 在Spring 2.0中,Throw Advice不用实现org.springframework.aop.ThrowsAdvice接口,但Advice的方法必须定义Throwable(或其子类)参数,例如:
八、Throw Advice:基于Annotation
Tags: Spring  aop  aspectj  Advice  
分页:[«] 1[»]
Feed订阅集
勤劳致富^.^
Powered By Z-Blog   STYLE by busfly . FatMouse
Copyright © 2007 巴士飞扬技术博客. . 沪ICP备07027972号. 会员群1(VS为主):3769186.