一、要加強的類java
package com.test.springadvicetype; import org.springframework.stereotype.Component; import java.text.SimpleDateFormat; import java.util.Date; /** * 服務員類 */ @Component public class Waiter { /** * 服務 * @param name */ public String serve(String name) { System.out.println(name + ",您好,很高興爲您服務。"); SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); return name + ",您好,如今是北京時間" + format.format(new Date()); } /** * 開車 * @param name */ public void driving(String name) { throw new RuntimeException(name + ",您好,禁止酒後駕車!"); } }
二、異常拋出加強,實現 ThrowsAdvice 接口spring
package com.test.springadvicetype.throwsadvice; import org.springframework.aop.ThrowsAdvice; import org.springframework.stereotype.Component; /** * 異常拋出加強 */ @Component public class SpringThrowsAdvice implements ThrowsAdvice { public void afterThrowing(Exception e) throws Throwable{ System.out.printf("異常拋出加強執行:%s%n", e); } }
三、xml測試
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 開啓註解掃描 --> <context:component-scan base-package="com.test.springadvicetype"/> </beans>
四、測試代碼spa
package com.test.springadvicetype.throwsadvice; import com.test.springadvicetype.Waiter; import org.springframework.aop.framework.ProxyFactory; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * Spring異常拋出加強測試 */ public class SpringThrowsAdviceDemo { public static void main(String[] args) { System.out.println("Spring異常拋出加強測試"); System.out.println("==========我是分割線=========="); ApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring-chapter3-springaoptype.xml"); Waiter waiter = (Waiter) context.getBean("waiter"); SpringThrowsAdvice advice = (SpringThrowsAdvice) context.getBean("springThrowsAdvice"); //Spring提供的代理工廠 ProxyFactory pf = new ProxyFactory(); //設置代理目標 pf.setTarget(waiter); pf.addAdvice(advice); //生成代理實例 Waiter proxy = (Waiter)pf.getProxy(); proxy.driving("Michael"); } }
五、測試結果代理
Spring異常拋出加強測試 ==========我是分割線========== 異常拋出加強執行:java.lang.RuntimeException: Michael,您好,禁止酒後駕車! Exception in thread "main" java.lang.RuntimeException: Michael,您好,禁止酒後駕車! at com.test.springadvicetype.Waiter.driving(Waiter.java:30) at com.test.springadvicetype.Waiter$$FastClassBySpringCGLIB$$57d4d7c2.invoke(<generated>) at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218) at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:749) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163) at org.springframework.aop.framework.adapter.ThrowsAdviceInterceptor.invoke(ThrowsAdviceInterceptor.java:112) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:688) at com.test.springadvicetype.Waiter$$EnhancerBySpringCGLIB$$e5915ab1.driving(<generated>) at com.test.springadvicetype.throwsadvice.SpringThrowsAdviceDemo.main(SpringThrowsAdviceDemo.java:28)