黑马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 }
测试类

posted @ 2019-06-05 10:19  没有理由不会呀  阅读(453)  评论(0编辑  收藏  举报