xml實現的spring-aop

接口:java

 1 package spring.aop2;  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.aop2;  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 }

切面類1:express

 1 package spring.aop2;  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 public class aspect { 13 
14 
15     public void beforeMethod(JoinPoint joinPoint){ 16         String methodName = joinPoint.getSignature().getName(); 17         List<Object> args = Arrays.asList(joinPoint.getArgs()); 18         System.out.println("methodName:"+methodName+"--->before args: "+args); 19  } 20 
21 
22     public void afterMethod(JoinPoint joinPoint){ 23         String methodName = joinPoint.getTarget().getClass().getName(); 24         List<Object> args = Arrays.asList(joinPoint.getArgs()); 25         System.out.println("methodName:"+methodName+"--->after args: "+args); 26  } 27 
28 
29     public void afterReturning(JoinPoint joinPoint,Object result){ 30         String methodName = joinPoint.getSignature().getName(); 31         System.out.println("methodName:"+methodName+"--->afterReturning args: "+result); 32 
33  } 34 
35     public void afterThrowing(JoinPoint joinPoint,Exception e){ 36         String methodName = joinPoint.getSignature().getName(); 37         System.out.println("methodName:"+methodName+"--->afterThrowing args: "+e); 38 
39  } 40 }

切面類2:ide

 1 package spring.aop2;  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 public class VliAspect { 12 
13     public void validateArgs(JoinPoint joinPoint){ 14 
15         System.out.println("validateArgs -> "+ Arrays.asList(joinPoint.getArgs())); 16  } 17 }

配置:aop2.xml測試

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4  xmlns:p="http://www.springframework.org/schema/p"
 5  xmlns:context="http://www.springframework.org/schema/context"
 6  xmlns:aop="http://www.springframework.org/schema/aop"
 7  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd  8  http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd  9  http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
10 
11     <!--自動爲匹配的類生成代理對象-->
12     <aop:aspectj-autoproxy proxy-target-class="true" />
13 
14     <!--配置bean-->
15     <bean id="arithmeticImpl" class="spring.aop2.ArithmeticImpl"/>
16 
17     <!--配置切面bean-->
18     <bean id="vliAspect" class="spring.aop2.VliAspect"/>
19     <bean id="aspect" class="spring.aop2.aspect"/>
20 
21     <!--配置aop-->
22     <aop:config>
23         <!--配置切點表達式-->
24         <aop:pointcut id="pointcut" expression="execution(* spring.aop2.*.*(..))"/>
25         <!--配置切面和通知-->
26         <aop:aspect ref="aspect" order="2">
27             <aop:before method="beforeMethod" pointcut-ref="pointcut"/>
28             <aop:after method="afterMethod" pointcut-ref="pointcut"/>
29             <aop:after-throwing method="afterThrowing" pointcut-ref="pointcut" throwing="e"/>
30             <aop:after-returning method="afterReturning" pointcut-ref="pointcut" returning="result"/>
31 
32         </aop:aspect>
33         <aop:aspect ref="vliAspect" order="1">
34             <aop:before method="validateArgs" pointcut-ref="pointcut"/>
35         </aop:aspect>
36     </aop:config>
37 
38 
39 
40 </beans>

測試:spa

 1 package spring.aop2;  2 
 3 import org.springframework.context.ApplicationContext;  4 import org.springframework.context.support.ClassPathXmlApplicationContext;  5 
 6 public class Main {  7 
 8     public static void main(String[] args){  9 
10         ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:aop2.xml"); 11         Arithmetic proxy = ac.getBean("arithmeticImpl",ArithmeticImpl.class); 12         proxy.add(3,5); 13         System.out.println("-----------------------"); 14         proxy.sub(3,0); 15         System.out.println("-----------------------"); 16         proxy.div(3,0); 17         System.out.println("-----------------------"); 18  } 19 }

結果:代理

相關文章
相關標籤/搜索