黑馬Spring學習 AOP XML和註解配置 5種通知 切點切面通知織入

業務類
 1 package cn.itcast.aop;  2 
 3 import org.aspectj.lang.JoinPoint;  4 import org.aspectj.lang.ProceedingJoinPoint;  5 import org.aspectj.lang.Signature;  6 import org.aspectj.lang.annotation.*;  7 import org.springframework.core.annotation.Order;  8 import org.springframework.stereotype.Component;  9 
10 import java.util.Arrays; 11 
12 @Component 13 @Order 14 @Aspect 15 public class Logger { 16     //切點抽取
17     @Pointcut("execution(* cn.itcast..*.*(..))") 18     public void p(){} 19 
20     @Before("Logger.p()") 21     public void before(JoinPoint jp) { 22         String methodName = jp.getSignature().getName(); 23         Object[] args = jp.getArgs(); 24         System.out.println("methodName:" + methodName + " args:" + Arrays.toString(args)); 25  System.out.println(); 26         System.out.println("before"); 27 // int i = 1 / 0;
28  } 29     @After("Logger.p()") 30     public void after(){ 31         System.out.println("after"); 32  } 33 
34 /* @Around("p()") 35  public Object around(ProceedingJoinPoint jp) throws Throwable { 36  System.out.println("around前"); 37  Object result = jp.proceed(); 38  System.out.println("around後"); 39  return result; 40  }*/
41 
42     //能夠強轉爲鏈接點返回的類型,沒有問題。
43    /* @Around("p()") 44  public int around(ProceedingJoinPoint jp) throws Throwable { 45  System.out.println("around前"); 46  Object result = jp.proceed(); 47  System.out.println("around後"); 48  return (int) result + 5; 49  }*/
50 
51     /*
52  注意:若是鏈接點方法有返回值,則環繞必須給返回值,否則的話返回null。 53  1.若是afterReturnning在around後,而around中獲取參數可能出問題 54  2.業務中調用鏈接點方法獲取返回值時可能會出問題 55      */
56     @Around("p()") 57     public void around(ProceedingJoinPoint jp) throws Throwable { 58         System.out.println("around前"); 59  jp.proceed(); 60         System.out.println("around後"); 61  } 62 
63     /*@AfterReturning(value = "p()", returning = "result") 64  public void afterReturning(JoinPoint jp, int result){ 65 
66  String methodName = jp.getSignature().getName(); 67  Object[] args = jp.getArgs(); 68  System.out.println("methodName:" + methodName + " args" 69  + Arrays.toString(args) + " result:" + result); 70  System.out.println("afterReturning"); 71  }*/
72 
73     /*@AfterReturning(value="p()", returning = "result") 74  public void afterReturning(int result){ 75  System.out.println("result:" + result); 76  System.out.println("afterReturning"); 77  }*/
78     @AfterReturning("p()") 79     public void afterReturning(){ 80         System.out.println("afterReturning"); 81  } 82 
83     @AfterThrowing("p()") 84     public void afterThrowing(){ 85         System.out.println("afterThrowing"); 86 // int i = 1 / 0;
87  } 88 }
切面類
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3        xmlns:aop="http://www.springframework.org/schema/aop"
 4        xmlns:context="http://www.springframework.org/schema/context"
 5        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 6        xsi:schemaLocation="  7        http://www.springframework.org/schema/beans
 8        http://www.springframework.org/schema/beans/spring-beans.xsd
 9        http://www.springframework.org/schema/aop
10        http://www.springframework.org/schema/aop/spring-aop.xsd
11        http://www.springframework.org/schema/context
12        http://www.springframework.org/schema/context/spring-context.xsd">
13 
14     <!--業務類-->
15     <bean id="userService" class="cn.itcast.aop.UserService"></bean>
16     <!--切面類-->
17     <!--<bean id="logger" class="cn.itcast.aop.Logger"></bean>-->
18 
19     <!--組件掃描-->
20     <context:component-scan base-package="cn.itcast"></context:component-scan>
21 
22     <!--開啓aop代理-->
23     <!--<aop:aspectj-autoproxy></aop:aspectj-autoproxy>-->
24 
25     <!--織入-->
26     <aop:config>
27         <aop:aspect ref="logger">
28             <aop:after-returning  method="afterReturning" pointcut="execution(public void cn.itcast.aop.UserService.deleteById(int))"></aop:after-returning>
29             <aop:around  method="around" pointcut="execution(public void cn.itcast.aop.UserService.deleteById(int))"></aop:around>
30             <aop:before method="before" pointcut="execution(public void cn.itcast.aop.UserService.deleteById(int))"></aop:before>
31             <aop:after-throwing method="afterThrowing" pointcut="execution(public void cn.itcast.aop.UserService.deleteById(int))"></aop:after-throwing>
32             <aop:after method="after" pointcut="execution(public void cn.itcast.aop.UserService.deleteById(int))"></aop:after>
33             <!--<aop:around method="start" pointcut="execution(public void cn.itcast.aop.UserService.deleteById(int))"></aop:around>-->
34         </aop:aspect>
35     </aop:config>
36 
37 </beans>
Spring核心配置
 1 package cn.itcast;  2 
 3 import cn.itcast.aop.UserService;  4 import org.junit.Test;  5 import org.junit.runner.RunWith;  6 import org.springframework.beans.factory.annotation.Autowired;  7 import org.springframework.test.context.ContextConfiguration;  8 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;  9 
10 @RunWith(SpringJUnit4ClassRunner.class) 11 @ContextConfiguration("classpath:applicationContext.xml") 12 public class AOPTest { 13 
14  @Autowired 15     private UserService userService; 16 
17  @Test 18     public void test(){ 19 // int i = userService.deleteById(5); 20 // System.out.println(i);
21         userService.deleteById(5); 22  } 23 }
測試類

相關文章
相關標籤/搜索