Book Image

Spring 2.5 Aspect Oriented Programming

Book Image

Spring 2.5 Aspect Oriented Programming

Overview of this book

Developing powerful web applications with clean, manageable code makes the maintenance process much easier. Aspect-Oriented Programming (AOP) is the easiest and quickest way to achieve such results. Spring is the only Java framework to offer AOP features. The combined power of Spring and AOP gives a powerful and flexible platform to develop and maintain feature-rich web applications quickly. This book will help you to write clean, manageable code for your Java applications quickly, utilizing the combined power of Spring and AOP. You will master the concepts of AOP by developing several real-life AOP-based applications with the Spring Framework, implementing the basic components of Spring AOP: Advice, Joinpoint, Pointcut, and Advisor. This book will teach you everything you need to know to use AOP with Spring. It starts by explaining the AOP features of Spring and then moves ahead with configuring Spring AOP and using its core classes, with lot of examples. It moves on to explain the AspectJ support in Spring. Then you will develop a three-layered example web application designed with Domain-Driven Design (DDD) and built with Test-Driven Development methodology using the full potential of AOP for security, concurrency, caching, and transactions.
Table of Contents (13 chapters)

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;
}
}

Before advice

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:

After returning advice

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:

Around advice

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:

After (finally) advice

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:

After throwing advice

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.

Before advice

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>

After advice

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}

After returning 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 !");
}
}

After throwing advice

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");
}
}

Around advice

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}