Spring4學習筆記-AOP(基於配置文件的方式)


原創作品,允許轉載,轉載時請務必以超鏈接形式標明文章  原始出處 、作者信息和本聲明。否則將追究法律責任。 http://shamrock.blog.51cto.com/2079212/1557743

引入的jar包與基於注解的方式引入的jar包相同

wKiom1QidHCgp74BAADrO2jIId0739.jpg

ArithmeticCalculator接口

1
2
3
4
5
6
7
8
9
10
11
package  com.spring.aop.impl.xml;
 
public  interface  ArithmeticCalculator {
     public  int  add( int  i,  int  j);
 
     public  int  sub( int  i,  int  j);
 
     public  int  mul( int  i,  int  j);
 
     public  int  div( int  i,  int  j);
}

接口實現類 ArithmeticCalculatorImpl.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package  com.spring.aop.impl.xml;
 
public  class  ArithmeticCalculatorImpl  implements  ArithmeticCalculator{
 
     @Override
     public  int  add( int  i,  int  j) {
         int  result = i + j;
         return  result;
     }
 
     @Override
     public  int  sub( int  i,  int  j) {
         int  result = i - j;
         return  result;
     }
 
     @Override
     public  int  mul( int  i,  int  j) {
         int  result = i * j;
         return  result;
     }
 
     @Override
     public  int  div( int  i,  int  j) {
         int  result = i / j;
         return  result;
     }
 
}

切面類 LoggingAspect.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package  com.spring.aop.impl.xml;
 
import  java.util.Arrays;
import  java.util.List;
 
import  org.aspectj.lang.JoinPoint;
import  org.aspectj.lang.ProceedingJoinPoint;
 
public  class  LoggingAspect {
     
     public  void  beforeMethod(JoinPoint joinpoint) {
         String methodName = joinpoint.getSignature().getName();
         List<Object>args = Arrays.asList(joinpoint.getArgs());
         System.out.println( "前置通知:The method " + methodName + " begins with "  + args);
     }
     
     public  void  afterMethod(JoinPoint joinpoint) {
         String methodName = joinpoint.getSignature().getName();
         //List<Object>args = Arrays.asList(joinpoint.getArgs());  后置通知方法中可以獲取到參數
         System.out.println( "后置通知:The method " + methodName + " ends " );
     }
     
     public  void  afterReturnning(JoinPoint joinpoint, Object result) {
         String methodName = joinpoint.getSignature().getName();
         System.out.println( "返回通知:The method " + methodName + " ends with "  + result);
     }
     
     public  void  afterThrowing(JoinPoint joinpoint, Exception e) {
         String methodName = joinpoint.getSignature().getName();
         System.out.println( "異常通知:The method " + methodName + " occurs exception "  + e);
     }
     
     public  Object aroundMethod(ProceedingJoinPoint point) {
         Object result =  null ;
         String methodName = point.getSignature().getName();
         try  {
             //前置通知
             System.out.println( "The method " + methodName + " begins with "  + Arrays.asList(point.getArgs()));
             //執行目標方法
             result = point.proceed();
             //翻譯通知
             System.out.println( "The method " + methodName + " ends with "  + result);
         catch  (Throwable e) {
             //異常通知
             System.out.println( "The method " + methodName + " occurs exception "  + e);
             throw  new  RuntimeException(e);
         }
         //后置通知
         System.out.println( "The method " + methodName + " ends" );
         return  result;
     }
}

切面類 ValidationAspect.java

1
2
3
4
5
6
7
8
9
10
11
12
package  com.spring.aop.impl.xml;
 
import  java.util.Arrays;
 
import  org.aspectj.lang.JoinPoint;
 
public  class  ValidationAspect {
 
     public  void  validateArgs(JoinPoint joinPoint) {
         System.out.println( "validate:"  + Arrays.asList(joinPoint.getArgs()));
     }
}

applicationContext-xml.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<? 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"
     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-4.1.xsd">
 
     <!-- 配置bean -->
     < bean  id = "arithmeticCalculator"  class = "com.spring.aop.impl.xml.ArithmeticCalculatorImpl" ></ bean >
     <!-- 配置切面的bean -->
     < bean  id = "loggingAspect"  class = "com.spring.aop.impl.xml.LoggingAspect" ></ bean >
     
     < bean  id = "validationAspect"  class = "com.spring.aop.impl.xml.ValidationAspect" ></ bean >
     
     <!-- 配置AOP -->
     < aop:config >
         <!-- 配置切點表達式 -->
         < aop:pointcut  expression = "execution(* com.spring.aop.impl.xml.ArithmeticCalculator.*(..))"  id = "pointcut" />
         <!-- 配置切面及通知,使用order指定優先級 -->
         < aop:aspect  ref = "loggingAspect"  order = "1" >
             <!-- 環繞通知 -->
             <!--  
                 <aop:around method="aroundMethod" pointcut-ref="pointcut"/>
             -->
             <!-- 前置通知 -->
             < aop:before  method = "beforeMethod"  pointcut-ref = "pointcut" />
             <!-- 后置通知 -->
             < aop:after  method = "afterMethod"  pointcut-ref = "pointcut" />
             <!-- 異常通知 -->
             < aop:after-throwing  method = "afterThrowing"   pointcut-ref = "pointcut"  throwing = "e" />
             <!-- 返回通知 -->
             < aop:after-returning  method = "afterReturnning"  pointcut-ref = "pointcut"  returning = "result" />
             
         </ aop:aspect >
         < aop:aspect  ref = "validationAspect"  order = "2" >
             <!-- 前置通知 -->
             < aop:before  method = "validateArgs"  pointcut-ref = "pointcut" />
         </ aop:aspect >
     </ aop:config >
</ beans >

Main.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package  com.spring.aop.impl.xml;
 
import  org.springframework.context.ApplicationContext;
import  org.springframework.context.support.ClassPathXmlApplicationContext;
 
public  class  Main {
public  static  void  main(String[] args) {
     //創建spring IOC容器
     ApplicationContext applicationContext =  new  ClassPathXmlApplicationContext( "applicationContext-xml.xml" );
     //從IOC容器中獲取bean實例
     ArithmeticCalculator arithmeticCalculator = applicationContext.getBean(ArithmeticCalculator. class );
     int  result = arithmeticCalculator.add( 4 6 );
     System.out.println(result);
     result = arithmeticCalculator.sub( 4 6 );
     System.out.println(result);
     System.out.println(result);
     result = arithmeticCalculator.mul( 4 6 );
     System.out.println(result);
     System.out.println(result);
     result = arithmeticCalculator.div( 4 0 );
     System.out.println(result);
}
}



源碼 http://yunpan.cn/cgsrQHmUvrIQt  提取碼 6564

本文出自 “優賽工作室” 博客,請務必保留此出處http://shamrock.blog.51cto.com/2079212/1557743


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM