一、須要加強的類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 + ",您好,禁止酒後駕車!"); } }
二、後置加強,實現 AfterReturningAdvice 接口spring
package com.test.springadvicetype.afterretuningadvice; import org.springframework.aop.AfterReturningAdvice; import org.springframework.stereotype.Component; import java.lang.reflect.Method; /** * 後置加強 */ @Component public class SpringAfterReturningAdvice implements AfterReturningAdvice { @Override public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable { String methodName = method.getName(); System.out.printf("AfterReturningAdvice加強的方法返回值是:%s%n", returnValue); System.out.printf("AfterReturningAdvice加強的方法是:%s%n", methodName); System.out.printf("AfterReturningAdvice加強的方法的參數是:%s%n", args[0]); System.out.printf("AfterReturningAdvice加強的對象是:%s%n", target); } }
三、xml配置 spring-chapter3-springaoptype.xmlide
<?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>
四、運行代碼測試
package com.test.springadvicetype.afterretuningadvice; import org.springframework.aop.AfterReturningAdvice; import org.springframework.stereotype.Component; import java.lang.reflect.Method; /** * 後置加強 */ @Component public class SpringAfterReturningAdvice implements AfterReturningAdvice { @Override public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable { String methodName = method.getName(); System.out.printf("AfterReturningAdvice加強的方法返回值是:%s%n", returnValue); System.out.printf("AfterReturningAdvice加強的方法是:%s%n", methodName); System.out.printf("AfterReturningAdvice加強的方法的參數是:%s%n", args[0]); System.out.printf("AfterReturningAdvice加強的對象是:%s%n", target); } }
五、運行結果code
Spring後置加強測試 ==========我是分割線========== Michael,您好,很高興爲您服務。 AfterReturningAdvice加強的方法返回值是:Michael,您好,如今是北京時間2020-01-28 10:27:56 AfterReturningAdvice加強的方法是:serve AfterReturningAdvice加強的方法的參數是:Michael AfterReturningAdvice加強的對象是:com.test.springadvicetype.Waiter@2e32ccc5 ==========我是分割線========== Tommy,您好,很高興爲您服務。 AfterReturningAdvice加強的方法返回值是:Tommy,您好,如今是北京時間2020-01-28 10:27:56 AfterReturningAdvice加強的方法是:serve AfterReturningAdvice加強的方法的參數是:Tommy AfterReturningAdvice加強的對象是:com.test.springadvicetype.Waiter@2e32ccc5