書名: Spring 2.5 Aspect Oriented Programming作者名: Massimiliano Dessi本章字數(shù): 704字更新時間: 2021-05-21 20:23:12
AOP with IoC in Spring 2.5
Now we will see what Spring 2.5 offers compared to the 1.x version.
AspectJ annotations
Now let's see some introductory examples of using the syntax of AspectJ with annotations; the purpose is to have the same sorts of advice that we saw in the programmatic examples.
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> <aop:aspectj-autoproxy/> ... </beans>
In the configuration, if we use a tag <aop:aspectj-autoproxy/>
, Spring prepares autoproxy for the classic mode that uses AspectJ annotations and we define two beans to check the annotations' behavior.
This is the body of the Target Class that we will use in the examples.
public class Hello { public void greeting(){ System.out.println(label); } private String label = "reader"; public void setLabel(String label) { this.label = label; } }
Now we see how to use before advice with the annotations.
package org.springaop.chapter.one.annotation.before; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; @Aspect public class BeforeAspect { @Before("execution(* greeting(..))") public void beforeGreeting() { System.out.println("Good morning "); } } <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> <aop:aspectj-autoproxy/> <bean id="hello" class="org.springaop.target.Hello" p:label="writer"/> <bean id="before" class="org.springaop.chapter.one.annotation.before.BeforeAspect"/> </beans>
Result:

The following code explains how to use after returning advice with the annotations:
package org.springaop.chapter.one.annotation.after.returning; import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.Aspect; @Aspect public class AfterReturningAspect { @AfterReturning("execution(* greeting(..))") public void afterGreeting() { System.out.println("this is a aop !"); } } <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> <aop:aspectj-autoproxy/> <bean id="hello" class="org.springaop.target.Hello" p:label="writer"/> <bean id="afterReturning" class="org.springaop.chapter.one.annotation.after.returning.AfterReturningAspect"/> </beans>
Result:

Now we see how to use around advice with the annotations.
package org.springaop.chapter.one.annotation.around; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; @Aspect public class AroundAspect { @Around("execution(* greeting(..))") public Object aroundGreeting(ProceedingJoinPoint pjp) throws Throwable { System.out.print("Hello "); try { return pjp.proceed(); } finally { System.out.println("this is around aop !"); } } } <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> <aop:aspectj-autoproxy/> <bean id="hello" class="org.springaop.target.Hello" p:label="writer"/> <bean id="around" class="org.springaop.chapter.one.annotation.around.AroundAspect"/> </beans>
Result:

The following code explains how to use after (finally) advice
with the annotations:
package org.springaop.chapter.one.annotation.afterfinally; import org.aspectj.lang.annotation.After; import org.aspectj.lang.annotation.Aspect; @Aspect public class AfterFinallyAspect { @After("execution(* greeting(..))") public void afterGreeting() { System.out.println("this is afterAspect !"); } } <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> <aop:aspectj-autoproxy/> <bean id="hello" class="org.springaop.target.Hello" p:label="writer"/> <bean id="afterFinally" class="org.springaop.chapter.one.annotation.afterfinally.AfterFinallyAspect"/> </beans>
Result:

For the after throwing advice example, we're going to use a target class different from Hello, in order to be able to trigger exceptions deliberately.
This is the body of the exception target:
package org.springaop.target; public class ExceptionTarget { public void errorMethod() throws Exception { throw new Exception("Fake exception"); } public void otherErrorMethod() throws IllegalArgumentException { throw new NullPointerException("Other Fake exception"); } }
Example after throwing:
package org.springaop.chapter.one.annotation.throwsadvice; import org.aspectj.lang.annotation.AfterThrowing; import org.aspectj.lang.annotation.Aspect; @Aspect public class AfterThrowingAspect { @AfterThrowing("execution(* errorMethod(..))") public void afterGreeting() { System.out.println("+++"); System.out.println("Exception !"); System.out.println("+++"); } }
This is the body of the ExceptionTest
class:
package org.springaop.chapter.one.annotation.throwsadvice; import org.springaop.target.ExceptionTarget; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class ExceptionTest { public static void main(String[] args) { String[] paths = {"org/springaop/conf/applicationContext.xml"}; ApplicationContext ctx = new ClassPathXmlApplicationContext(paths); ExceptionTarget exceptiontarget = (ExceptionTarget)ctx.getBean("exceptionTarget"); try { exceptiontarget.errorMethod(); } catch (Exception ignored) { } } } <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> <aop:aspectj-autoproxy/> <bean id="exceptionTarget" class="org.springaop.target.ExceptionTarget"/> <bean id="throws" class="org.springaop.chapter.one.annotation.throwsadvice.AfterThrowingAspect"/> </beans>
The result will be:

Schema-based configuration
Now let's see some introductory examples for schema-based configuration, using the code of the classes of the examples used with the annotations, and with the same output results.
The following before advice example example explains how to use the before advice with XML Schema configuration:
package org.springaop.aspects.schema; public class SpringAopAspectBeforeExample { public void beforeGreeting() { System.out.println("Good morning "); } } <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <aop:config> <aop:aspect ref="before"> <aop:before method="beforeGreeting" pointcut="execution(* greeting(..))" /> </aop:aspect> </aop:config> <bean id="hello" class="org.springaop.target.Hello" p:label="writer" /> <bean id="before" class="org.springaop.aspects.schema.SpringAopAspectBeforeExample" /> </beans>
The following after advice example explains how to use the after advice with XML Schema configuration:
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <bean id="hello" class="org.springaop.target.Hello" p:label="writer" /> <aop:config> <aop:aspect ref="after"> <aop:after method="afterGreeting" pointcut="execution(* greeting(..))" /> </aop:aspect> </aop:config> <bean id="after" class="org.springaop.aspects.schema.SpringAopAspectAfterExample" /> </beans> package org.springaop.aspects.schema; public class SpringAopAspectAfterExample { public void afterGreeting() { System.out.println("this is afterAspect !"); } schema based configurations, Spring AOP 2.5after advice}
The following after returning advice example explains how to use the after returning advice with XML Schema configuration:
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <bean id="hello" class="org.springaop.target.Hello" p:label="writer"/> <aop:config> <aop:aspect ref="afterReturning"> <aop:after-returning method="afterGreeting" pointcut="execution(* greeting(..))" /> </aop:aspect> </aop:config> <bean id="afterReturning" class="org.springaop.aspects.schema.SpringAopAspectAfterReturningExample"/> </beans> package org.springaop.aspects.schema; public class SpringAopAspectAfterReturningExample { public void afterGreeting() { System.out.println("this is a aop !"); } }
The following after throwing advice example explains how to use the after throwing advice with XML Schema configuration:
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <bean id="exceptionTarget" class="org.springaop.chapter.one.schema.throwsadvice.ExceptionTarget" /> <aop:config> <aop:aspect ref="afterThrowing"> <aop:after-throwing method="afterErrorMethod" pointcut="execution(* errorMethod(..)) throws Exception" /> </aop:aspect> </aop:config> <bean id="afterThrowing" class="org.springaop.aspects.schema.SpringAopAspectAfterThrowingExample" /> </beans> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <bean id="exceptionTarget" class="org.springaop.chapter.one.schema.throwsadvice.ExceptionTarget" /> <aop:config> <aop:aspect ref="afterThrowing"> <aop:after-throwing method="afterErrorMethod" pointcut="execution(* errorMethod(..)) throws Exception" /> </aop:aspect> </aop:config> <bean id="afterThrowing" class="org.springaop.aspects.schema.SpringAopAspectAfterThrowingExample" /> </beans> Target Class: package org.springaop.target; public class ExceptionTarget { public void errorMethod() throws Exception { throw new Exception("Fake exception"); } public void otherErrorMethod() throws NullPointerException { throw new NullPointerException("Other Fake exception"); } } Aspect: package org.springaop.aspects.schema; public class SpringAopAspectAfterThrowingExample { public void afterErrorMethod() { System.out.println("+++"); System.out.println("Exception !"); System.out.println("+++\n"); } }
The following around advice example explains how to use the around advice with XML Schema configuration:
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <bean id="hello" class="org.springaop.target.Hello" p:label="writer" /> <aop:config> <aop:aspect ref="around"> <aop:around method="aroundGreeting" pointcut="execution(* greeting(..))" /> </aop:aspect> </aop:config> <bean id="around" class="org.springaop.aspects.schema.SpringAopExampleAroundExample" /> </beans> package org.springaop.aspects.schema; public class SpringAopExampleAroundExample { public Object aroundGreeting(ProceedingJoinPoint pjp) throws Throwable { System.out.print("Hello "); try { return pjp.proceed(); } finally { System.out.println("this is around aop !"); } } after returning advice, Spring AOP 2.5around advice}
- Adobe創(chuàng)意大學After Effects CS5 產(chǎn)品專家認證標準教材
- CorelDRAW服裝設計實用教程(第四版)
- 新媒體美工一冊通(全彩)
- Drupal: Creating Blogs, Forums, Portals, and Community Websites
- PS App UI設計從零開始學
- 有趣的Flutter:從0到1構建跨平臺App
- SolidWorks2016中文版從入門到精通/CAX工程應用叢書
- AutoCAD 2016中文版基礎教程(全圖解視頻版)
- Getting Started with Oracle BPM Suite 11gR1 – A Hands/On Tutorial
- 中文版Flash CC實例教程
- Photoshop CC圖形圖像處理實戰(zhàn)教程(微課版)
- 3ds Max三維動畫制作項目式教程
- 48小時精通CREO Parametric 3.0中文版鈑金設計技巧
- 新手學Flash CS6動畫制作
- JSF 1.2 Components