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

快速入门

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 代码实现

复制代码
 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详解

 

 

 

 

posted @   yub4by  阅读(45)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
点击右上角即可分享
微信分享提示