首页 > 编程语言 > 详细

SSM9.3【Spring:基于注解的AOP开发】

时间:2021-07-22 16:46:55      阅读:18      评论:0      收藏:0      [点我收藏+]

快速入门

技术分享图片

 

 技术分享图片

 

 技术分享图片

 

 技术分享图片

 

 技术分享图片

 

 技术分享图片

 

 技术分享图片

 

技术分享图片

 

 代码实现

 1 package com.haifei.anno;
 2 
 3 /**
 4  * 目标接口
 5  */
 6 public interface TargetInterface {
 7 
 8     public void save();
 9 
10 }
 1 package com.haifei.anno;
 2 
 3 import org.springframework.stereotype.Component;
 4 
 5 /**
 6  * 目标类
 7  */
 8 @Component("target")
 9 public class Target implements TargetInterface {
10 
11     //切点方法
12     @Override
13     public void save() {
14         System.out.println("save running...");
15     }
16 
17 }

 

 1 package com.haifei.anno;
 2 
 3 import org.aspectj.lang.ProceedingJoinPoint;
 4 import org.aspectj.lang.annotation.*;
 5 import org.springframework.stereotype.Component;
 6 
 7 /**
 8  * 切面类
 9  */
10 @Component("myAspect")
11 @Aspect //标注当前MyAspect类是一个切面类
12 public class MyAspect {
13 
14     //前置增强方法
15 //    @Before(value = "execution(* com.haifei.anno.*.*(..))")
16 //    @Before("execution(* com.haifei.anno.*.*(..))")
17     public void beforeTarget(){
18         System.out.println("前置增强..........");
19     }
20 
21     //后置增强方法
22 //    @After("execution(* com.haifei.anno.*.*(..))")
23     public void afterReturning(){
24         System.out.println("后置增强..........");
25     }
26 
27     //环绕增强方法
28 //    @Around("execution(* com.haifei.anno.*.*(..))")
29     @Around("pointcut()") //或者 "MyAspect.pointcut()"
30     public Object aroundTarget(ProceedingJoinPoint pjp) throws Throwable { //正在执行的连接点==切点
31         System.out.println("环绕前增强");
32         Object proceed = pjp.proceed(); //切点方法
33         System.out.println("环绕后增强");
34         return proceed;
35     }
36 
37     //异常抛出增强方法
38 //    @AfterThrowing("execution(* com.haifei.anno.*.*(..))")
39     public void afterThrowing(){
40         System.out.println("异常抛出增强....");
41     }
42 
43     //最终增强方法(不管抛不抛异常,该增强都会执行)
44 //    @After("execution(* com.haifei.anno.*.*(..))")
45     @After("MyAspect.pointcut()")
46     public void after(){
47         System.out.println("最终增强....");
48     }
49 
50     //定义方法-抽取切点表达式
51     @Pointcut("execution(* com.haifei.anno.*.*(..))")
52     public void pointcut(){}
53 
54 }
 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:aop="http://www.springframework.org/schema/aop"
 5        xmlns:context="http://www.springframework.org/schema/context"
 6        xsi:schemaLocation="
 7             http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
 8             http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
 9             http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
10 ">
11 
12 
13     <!--开启组件扫描-->
14     <context:component-scan base-package="com.haifei.anno" />
15     <!--配置AOP自动代理-->
16     <aop:aspectj-autoproxy />
17 
18 
19 </beans>
 1 package com.haifei.test;
 2 
 3 import com.haifei.anno.TargetInterface;
 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-anno.xml")
12 public class AnnoTest {
13 
14     @Autowired
15     private TargetInterface target;
16 
17     @Test
18     public void test1(){
19         target.save();
20         /*
21         <context:component-scan base-package="com.haifei.anno" />
22         @Before("execution(* com.haifei.anno.*.*(..))")
23         public void beforeTarget(){...}
24             save running...
25 
26         <context:component-scan base-package="com.haifei.anno" />
27         <aop:aspectj-autoproxy />
28         @Before("execution(* com.haifei.anno.*.*(..))")
29         public void beforeTarget(){...}
30             前置增强..........
31             save running...
32          */
33     }
34 
35     @Test
36     public void test2(){
37         target.save();
38         /*
39         <context:component-scan base-package="com.haifei.anno" />
40         <aop:aspectj-autoproxy />
41 
42         @Around("execution(* com.haifei.anno.*.*(..))")
43         public Object aroundTarget(ProceedingJoinPoint pjp) throws Throwable {...}
44         @After("execution(* com.haifei.anno.*.*(..))")
45         public void after(){...}
46 
47         环绕前增强
48         save running...
49         环绕后增强
50         最终增强....
51 
52 
53        定义方法-抽取切点表达式
54        效果同上
55          */
56     }
57 
58 }

注解配置AOP详解

技术分享图片

 

 技术分享图片

 

 技术分享图片

SSM9.3【Spring:基于注解的AOP开发】

原文:https://www.cnblogs.com/yppah/p/15043506.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!