http://www.blogjava.net/liuwentao253/archive/2007/01/23/95494.html
MethodInterceptor --> org.springframework.aop.support.RegexpMethodPointcutAdvisor
ThrowsAdvice --> org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator
接口:
package com.app.aop; public interface IBizProcessImpl { void doOneThing(); void doAnotherThing(); }
实现类:
package com.app.aop; public class BizProcessImpl implements IBizProcessImpl { public void doOneThing() { System.out.println("doOneThing-->"); } public void doAnotherThing() { System.out.println("doAnotherThing-->"); int i =1/0;//模拟出现异常 //throw new RuntimeException("Boom"); } }
ThrowsAdvice类
package com.app.aop; import java.lang.reflect.Method; import org.springframework.aop.ThrowsAdvice; public class MyThrowsAdvice implements ThrowsAdvice { public void afterThrowing(Method method, Object[] args, Object target, Exception ex){ System.out.println("-----------------afterThrowing message start-----------------"); System.out.println("Method Name="+method.getName()); for(int i=0; i<args.length; i++){ System.out.println(args[i]); } System.out.println("Target Class Name="+target.getClass().getName()); System.out.println("Exception Class Name="+ex.getClass().getName()); System.out.println("Exception Message="+ex.getMessage()); System.out.println("-----------------afterThrowing message end-----------------"); } public void afterThrowing(Exception ex){ System.out.println("-----------------afterThrowing message start-----------------"); System.out.println("Exception Class Name="+ex.getClass().getName()); System.out.println("Exception Message="+ex.getMessage()); System.out.println("-----------------afterThrowing message end-----------------"); } }
applicationContext.xml配置:
配置A:
<bean id="bizOneTarget" class="com.app.aop.BizProcessImpl"></bean> <bean id="throwsAdvice" class="com.app.aop.MyThrowsAdvice"></bean> <bean id="bizOne" class="org.springframework.aop.framework.ProxyFactoryBean"> <property name="proxyInterfaces"> <value>com.app.aop.IBizProcessImpl</value> </property> <property name="target"> <ref bean="bizOneTarget" /> </property> <property name="interceptorNames"> <list> <value>throwsAdvice</value> </list> </property> </bean>
Java测试类:
public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); IBizProcessImpl t = ctx.getBean("bizOne",IBizProcessImpl.class); //IBizProcessImpl t = ctx.getBean("bizOneTarget",IBizProcessImpl.class); try { t.doAnotherThing(); } catch (Exception e) { // TODO: handle exception } }
配置B:
<bean id="bizOneTarget" class="com.app.aop.BizProcessImpl"></bean> <bean id="throwsAdvice" class="com.app.aop.MyThrowsAdvice"></bean> <bean id="beanNameAutoProxyCreator" class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator"> <property name="beanNames"> <list> <value>*bizOneTarget</value>//可以用通配符 </list> </property> <property name="interceptorNames"> <value>throwsAdvice</value> </property>
Java测试类:
public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); //IBizProcessImpl t = ctx.getBean("bizOne",IBizProcessImpl.class); IBizProcessImpl t = ctx.getBean("bizOneTarget",IBizProcessImpl.class); try { t.doAnotherThing(); } catch (Exception e) { // TODO: handle exception } }