接口:java
1 package spring.aop; 2
3 public interface Arithmetic { 4
5 Integer add(Integer a,Integer b); 6 Integer sub(Integer a,Integer b); 7 Integer div(Integer a,Integer b); 8
9
10 }
實現類:spring
1 package spring.aop; 2
3 import org.springframework.stereotype.Component; 4
5 @Component 6 public class ArithmeticImpl implements Arithmetic{ 7
8 @Override 9 public Integer add(Integer a, Integer b) { 10 System.out.println("add -> "+(a+b)); 11 return a+b; 12 } 13
14 @Override 15 public Integer sub(Integer a, Integer b) { 16 System.out.println("sub -> "+(a-b)); 17 return a-b; 18 } 19
20 @Override 21 public Integer div(Integer a, Integer b) { 22 System.out.println("div -> "+(a/b)); 23 return a/b; 24 } 25
26
27 }
配置文件:aop.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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--
-->
<context:component-scan base-package="spring.aop"/>
<!--自動爲匹配的類生成代理對象-->
<aop:aspectj-autoproxy proxy-target-class="true" />
</beans>
驗證切面:測試
1 package spring.aop; 2
3 import org.aspectj.lang.JoinPoint; 4 import org.aspectj.lang.annotation.Aspect; 5 import org.aspectj.lang.annotation.Before; 6 import org.springframework.core.annotation.Order; 7 import org.springframework.stereotype.Component; 8
9 import java.util.Arrays; 10
11 @Order(1) 12 @Aspect 13 @Component 14 public class VliAspect { 15
16 @Before("spring.aop.aspect.declareJointPointExpression()") 17 public void validateArgs(JoinPoint joinPoint){ 18
19 System.out.println("validateArgs -> "+ Arrays.asList(joinPoint.getArgs())); 20 } 21 }
主切面spa
1 package spring.aop; 2
3 import org.aspectj.lang.JoinPoint; 4 import org.aspectj.lang.annotation.*; 5 import org.springframework.core.annotation.Order; 6 import org.springframework.stereotype.Component; 7
8 import java.util.Arrays; 9 import java.util.List; 10
11 //聲明這個切面的優先級,值越小,優先級越高
12 @Order(2) 13 //聲明這個類是一個切面
14 @Aspect 15 @Component 16 public class aspect { 17
18 //定義一個方法,用於聲明切點表達式,通常該方法不寫其餘代碼
19 @Pointcut("execution( * spring.aop.*.*(*,*))") 20 public void declareJointPointExpression(){} 21
22 //聲明該方法是一個前置通知
23 @Before("declareJointPointExpression()") 24 public void beforeMethod(JoinPoint joinPoint){ 25 String methodName = joinPoint.getSignature().getName(); 26 List<Object> args = Arrays.asList(joinPoint.getArgs()); 27 System.out.println("methodName:"+methodName+"--->before args: "+args); 28 } 29
30
31 //聲明該方法是一個後置通知,不管是否發生異常,都會在目標方法執行後執行 32 //後置通知沒法訪問目標方法的執行結果
33 @After("declareJointPointExpression()") 34 public void afterMethod(JoinPoint joinPoint){ 35 String methodName = joinPoint.getTarget().getClass().getName(); 36 List<Object> args = Arrays.asList(joinPoint.getArgs()); 37 System.out.println("methodName:"+methodName+"--->after args: "+args); 38 } 39
40 //返回通知,目標方法正常結束後執行 41 //能夠訪問方法的返回值
42 @AfterReturning(value = "declareJointPointExpression()",returning = "result") 43 public void afterReturning(JoinPoint joinPoint,Object result){ 44 String methodName = joinPoint.getSignature().getName(); 45 System.out.println("methodName:"+methodName+"--->afterReturning args: "+result); 46
47 } 48
49 @AfterThrowing(value = "declareJointPointExpression())",throwing = "e") 50 public void afterThrowing(JoinPoint joinPoint,Exception e){ 51 String methodName = joinPoint.getSignature().getName(); 52 System.out.println("methodName:"+methodName+"--->afterThrowing args: "+e); 53
54 } 55 }
測試類代理
package spring.aop; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main { public static void main(String[] args){ ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:aop.xml"); Arithmetic proxy = ac.getBean("arithmeticImpl",ArithmeticImpl.class); proxy.add(3,5); System.out.println("-----------------------"); proxy.sub(3,0); System.out.println("-----------------------"); proxy.div(3,0); System.out.println("-----------------------"); } }
結果code