ThrowsAdvice和自动代理的例子

参考:
[url]http://www.blogjava.net/liuwentao253/archive/2007/01/23/95494.html[/url]

[b]MethodInterceptor --> org.springframework.aop.support.RegexpMethodPointcutAdvisor
ThrowsAdvice --> org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator[/b]

接口:
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
}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值