[b]Target Class:[/b]
[b]1:Programmatic way:[/b]
[b]Before advice[/b]
[b]After returning advice[/b]
[b]Around advice[/b]
[b]After throwing advice[/b]
[b]2:AspectJ annotations:[/b]
[b]Before advice[/b]
[b]After returning advice:[/b]方法正常执行退出,才会被advice
[b]Around advice[/b]
[b]After (finally) advice[/b]方法执行完毕,包括因异常导致退出,也会被advice
[b]After throwing advice[/b]
[b]3:Schema-based configuration:[/b]
[b]Before advice[/b]
[b]After returning advice[/b]
[b]Around advice[/b]
[b]After (finally) advice[/b]
[b]After throwing advice[/b]
public class Hello {
public void greeting() {
System.out.println("reader");
}
}
public class ExceptionTarget {
public void errorMethod() throws Exception {
throw new Exception("Fake exception");
}
public void otherErrorMethod() throws NullPointerException {
throw new NullPointerException("Other Fake exception");
}
}
[b]1:Programmatic way:[/b]
[b]Before advice[/b]
public class BeforeAdvice implements MethodBeforeAdvice {
public static void main(String[] args) {
// target class
Hello target = new Hello();
// create the proxy
ProxyFactory pf = new ProxyFactory();
// add advice
pf.addAdvice(new BeforeAdvice());
// setTarget
pf.setTarget(target);
Hello proxy = (Hello) pf.getProxy();
proxy.greeting();
}
public void before(Method method, Object[] args, Object target) throws Throwable {
System.out.println("Good morning");
}
}
[b]After returning advice[/b]
public class AfterRetuningAdvice implements AfterReturningAdvice {
public static void main(String[] args) {
// target class
Hello target = new Hello();
// create the proxy
ProxyFactory pf = new ProxyFactory();
// add advice
pf.addAdvice(new AfterRetuningAdvice());
// setTarget
pf.setTarget(target);
Hello proxy = (Hello) pf.getProxy();
proxy.greeting();
}
public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
System.out.println(",this is a afterReturningAdvice message");
}
}
[b]Around advice[/b]
public class MethodDecorator implements MethodInterceptor {
public Object invoke(MethodInvocation invocation) throws Throwable {
System.out.print("Hello ");
Object retVal = invocation.proceed();
System.out.println("this is aop !");
return retVal;
}
}
public class AroundAdvice {
public static void main(String[] args) {
// target class
Hello target = new Hello();
// create the proxy
ProxyFactory pf = new ProxyFactory();
// add advice
pf.addAdvice(new MethodDecorator());
// setTarget
pf.setTarget(target);
Hello proxy = (Hello) pf.getProxy();
proxy.greeting();
}
}
[b]After throwing advice[/b]
public class ThrowsAdviceClass implements ThrowsAdvice {
public static void main(String[] args) {
// target class
ExceptionTarget errorBean = new ExceptionTarget();
// create the proxy
ProxyFactory pf = new ProxyFactory();
// add advice
pf.addAdvice(new ThrowsAdviceClass());
// setTarget
pf.setTarget(errorBean);
ExceptionTarget proxy = (ExceptionTarget) pf.getProxy();
try {
proxy.errorMethod();
} catch (Exception ignored) {
}
try {
proxy.otherErrorMethod();
} catch (Exception ignored) {
}
}
public void afterThrowing(Exception ex) throws Throwable {
System.out.println("+++");
System.out.println("Exception Capture:" + ex.getClass().getName());
System.out.println("+++\n");
}
public void afterThrowing(Method method, Object[] args, Object target, NullPointerException ex) throws Throwable {
System.out.println("+++");
System.out.println("NullPointerException Capture: " + ex.getClass().getName());
System.out.println("Method: " + method.getName());
System.out.println("+++\n");
}
}
[b]2:AspectJ annotations:[/b]
[b]Before advice[/b]
@Aspect
public class BeforeAspect {
@Before("execution(* greeting(..))")
public void beforeGreeting() {
System.out.println("Good morning ");
}
}
<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"/>
[b]After returning advice:[/b]方法正常执行退出,才会被advice
@Aspect
public class AfterReturningAspect {
@AfterReturning("execution(* greeting(..))")
public void afterGreeting() {
System.out.println("this is a aop !");
}
}
<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"/>
[b]Around advice[/b]
@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 !");
}
}
}
<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"/>
[b]After (finally) advice[/b]方法执行完毕,包括因异常导致退出,也会被advice
@Aspect
public class AfterFinallyAspect {
@After("execution(* greeting(..))")
public void afterGreeting() {
System.out.println("this is afterAspect !");
}
}
<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"/>
[b]After throwing advice[/b]
@Aspect
public class AfterThrowingAspect {
@AfterThrowing("execution(* errorMethod(..))")
public void afterGreeting() {
System.out.println("+++");
System.out.println("Exception !");
System.out.println("+++\n");
}
}
<aop:aspectj-autoproxy/>
<bean id="exceptionTarget" class="org.springaop.target.ExceptionTarget"/>
<bean id="throws" class="org.springaop.chapter.one.annotation.throwsadvice.AfterThrowingAspect"/>
[b]3:Schema-based configuration:[/b]
[b]Before advice[/b]
public class SpringAopAspectBeforeExample {
public void beforeGreeting() {
System.out.println("Good morning ");
}
}
<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" />
[b]After returning advice[/b]
public class SpringAopAspectAfterReturningExample {
public void afterGreeting() {
System.out.println("this is a aop !");
}
}
<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"/>
[b]Around advice[/b]
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 !");
}
}
}
<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" />
[b]After (finally) advice[/b]
public class SpringAopAspectAfterExample {
public void afterGreeting() {
System.out.println("this is afterAspect !");
}
}
<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" />
[b]After throwing advice[/b]
public class SpringAopAspectAfterThrowingExample {
public void afterErrorMethod() {
System.out.println("+++");
System.out.println("Exception !");
System.out.println("+++\n");
}
}
<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" />