Spring ThrowsAdvice使用例子

ThrowsAdvice用于拦截异常

import java.lang.reflect.Method;

import org.springframework.aop.ThrowsAdvice;

public class DaoThrowsAdvice implements ThrowsAdvice {
	public void afterThrowing(Method m, Object args, Object target,
			Throwable subclass) {
		System.out.println("[DaoThrowsAdvice]: An exception occur: "
				+ subclass.getClass().getSimpleName());
		System.exit(0);
	}
}



配置文件
<bean id="beforeDaoAdvice" class="com.zakisoft.workspace.initcompt.advice.DaoMethodBeforeAdvice"></bean>
    <bean id="afterDaoAdvice" class="com.zakisoft.workspace.initcompt.advice.DaoMethodAfterAdvice"></bean>
    <bean id="throwsDaoAdvice" class="com.zakisoft.workspace.initcompt.advice.DaoThrowsAdvice"></bean>
    <bean id="aroundAdvice" class="com.zakisoft.workspace.initcompt.advice.DaoMethodAroundAdvice"></bean>
    
    <bean id="daoAopService" class="org.springframework.aop.framework.ProxyFactoryBean">
    	<property name="target" ref="mybatisDaoBean"></property>
    	<property name="interceptorNames">
    		<list>
    			<value>beforeDaoAdvice</value>
    			<value>afterDaoAdvice</value>
    			<value>throwsDaoAdvice</value>
    			<value>aroundAdvice</value>
    		</list>
    	</property>
    </bean>


完整程序代码请参考附件。

你可能感兴趣的:(java,spring,AOP)