Before advice
package com.aop.advice; import java.lang.reflect.Method; import org.springframework.aop.MethodBeforeAdvice; public class MyBeforeAdvice implementsMethodBeforeAdvice { public void before(Method method, Object[]parameters, Object target) throws Throwable { System.err.println("==========before==========="); System.out.println(method.getName()); System.out.println(parameters); System.out.println(target); } } |
Around advice
package com.aop.advice; import java.util.HashSet; import java.util.Set; import org.aopalliance.intercept.MethodInterceptor; import org.aopalliance.intercept.MethodInvocation; public class MyAroundAdvice implements MethodInterceptor { private Set set=new HashSet(); public Object invoke(MethodInvocation method) throws Throwable { set.add(method); System.out.println("============around before=========="); Object result=method.proceed(); System.out.println("============around after=========="); return result; } } |
After return advice
package com.aop.advice; import java.lang.reflect.Method; importorg.springframework.aop.AfterReturningAdvice; public class MyAfterAdvice implements AfterReturningAdvice { public void afterReturning(Object result, Method method,Object[] parameter, Object target) throws Throwable { System.err.println("==============afterreturn=============="); System.out.println("result:"+result+";method:"+method+";parameter:"+parameter+";target:"+target); } } |
Throwadvice
package com.aop.advice; import org.springframework.aop.ThrowsAdvice; import com.aop.MyException; public class MyThrowsAdvice implements ThrowsAdvice { public void afterThrowing(MyException e) { // 可以定义多个方法,只要传入的参数是不同异常 System.err.println(e.toString()); } public void afterThrowing(RuntimeException e) { // 可以定义多个方法,只要传入的参数是不同异常 System.err.print("RuntimeException!"); } } |
My exception
package com.aop; public class MyException extends RuntimeException { public MyException(String msg) { super(msg); } public MyException() { } public MyException(String arg0, Throwable arg1) { super(arg0, arg1); } public MyException(Throwable arg0) { super(arg0); } } |
Service接口
package com.aop; public interface IServ { String[]doSth(); void doA(); } |
Service实现
package com.aop; public class Serv implements IServ { public String[] doSth() { System.out.println(this); return new String[]{"cindy","guoguo","xixi"}; } public voiddoA() { throw new MyException("111111111"); } } |
Context文件
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN""http://www.springframework.org/dtd/spring-beans.dtd"> <beans default-lazy-init="true"> <bean name="beforeAdvice"/> <bean name="aroundAdvice"/> <bean name="afterAdvice"/> <bean name="throwsAdvice"/> <bean id="servTarget" /> <bean id="invoker" > <property name="proxyInterfaces" value="com.aop.IServ" /> <property name="target" ref="servTarget" /> <property name="singleton" value="false" /> <property name="interceptorNames"> <list> <value>beforeAdvice</value> <value>aroundAdvice</value> <value>afterAdvice</value> <value>afterAdvice</value> <value>throwsAdvice</value> </list> </property> </bean> </beans> |
测试类
public static void main(String[] args) { ClassPathXmlApplicationContext ctx = newClassPathXmlApplicationContext(new String[] { "com/aop/aop-context.xml" }); IServ ser = (IServ) ctx.getBean("invoker"); ser.doSth(); try { ser.doA(); } catch (Throwable t) { t.printStackTrace(); } } |