書名: Spring 2.5 Aspect Oriented Programming作者名: Massimiliano Dessi本章字數: 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}
- 常用工具軟件案例教程
- ANSYS19.0實例詳解
- Final Cut Pro X 影視包裝剪輯完全自學教程(培訓教材版)
- Spring Python 1.1
- 中文版Maya 2012實用教程(第2版)
- AutoCAD 2014 中文版從入門到精通
- Plone 3 Multimedia
- Blender 2.5 Materials and Textures Cookbook
- SolidWorks2016中文版從入門到精通/CAX工程應用叢書
- After Effects 2022從入門到精通
- 巧學巧用Flash CS6制作動畫
- 巧用ChatGPT高效搞定Excel數據分析
- 剪映真傳:88招玩轉短視頻剪輯
- HBase企業應用開發實戰
- Java EE 5 Development with NetBeans 6