官术网_书友最值得收藏!

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:

Before advice

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:

After returning advice

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:

Around advice

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 (finally) advice

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:

After throwing advice

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}
主站蜘蛛池模板: 中江县| 凌云县| 屏山县| 新竹市| 宾川县| 泰兴市| 武胜县| 丽江市| 红桥区| 兴文县| 黔江区| 白河县| 清河县| 南靖县| 襄城县| 博湖县| 峡江县| 罗田县| 聂拉木县| 内乡县| 盐城市| 禹州市| 义马市| 伊金霍洛旗| 聊城市| 班戈县| 桂阳县| 湖南省| 潼关县| 当涂县| 岑溪市| 商都县| 英德市| 北辰区| 保康县| 怀集县| 石首市| 罗平县| 祁东县| 黎川县| 治县。|