SSM9.2【Spring:基于XML的AOP开发】
快速入门
代码实现
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 5 <modelVersion>4.0.0</modelVersion> 6 7 <groupId>com.haifei</groupId> 8 <artifactId>SSM9_spring_aop</artifactId> 9 <version>1.0-SNAPSHOT</version> 10 <packaging>war</packaging> 11 12 13 <dependencies> 14 <!--导入spring的context坐标,context依赖aop--> 15 <dependency> 16 <groupId>org.springframework</groupId> 17 <artifactId>spring-context</artifactId> 18 <version>5.0.5.RELEASE</version> 19 </dependency> 20 <!-- aspectj的织入 --> 21 <dependency> 22 <groupId>org.aspectj</groupId> 23 <artifactId>aspectjweaver</artifactId> 24 <version>1.8.4</version> 25 </dependency> 26 27 <dependency> 28 <groupId>org.springframework</groupId> 29 <artifactId>spring-test</artifactId> 30 <version>5.0.5.RELEASE</version> 31 </dependency> 32 <dependency> 33 <groupId>junit</groupId> 34 <artifactId>junit</artifactId> 35 <version>4.12</version> 36 </dependency> 37 </dependencies> 38 39 40 <build> 41 <plugins> 42 <!--jdk编译插件--> 43 <plugin> 44 <groupId>org.apache.maven.plugins</groupId> 45 <artifactId>maven-compiler-plugin</artifactId> 46 <version>3.1</version> 47 <configuration> 48 <target>1.8</target> 49 <source>1.8</source> 50 <encoding>UTF-8</encoding> <!--防止sout内部输出中文乱码--> 51 </configuration> 52 </plugin> 53 <!--tomcat7插件--> 54 <plugin> 55 <groupId>org.apache.tomcat.maven</groupId> 56 <artifactId>tomcat7-maven-plugin</artifactId> 57 <version>2.1</version> 58 <configuration> 59 <port>8080</port> 60 <path>/ssm9</path> 61 <uriEncoding>UTF-8</uriEncoding> <!--防止get请求url中中文参数乱码--> 62 </configuration> 63 </plugin> 64 </plugins> 65 </build> 66 67 68 </project>
1 package com.haifei.aop; 2 3 /** 4 * 目标接口 5 */ 6 public interface TargetInterface { 7 8 public void save(); 9 10 }
1 package com.haifei.aop; 2 3 /** 4 * 目标类 5 */ 6 public class Target implements TargetInterface { 7 8 //切点方法 9 @Override 10 public void save() { 11 System.out.println("save running..."); 12 // int i = 1/0; 13 } 14 15 }
1 package com.haifei.aop; 2 3 import org.aspectj.lang.ProceedingJoinPoint; 4 5 /** 6 * 切面类 7 */ 8 public class MyAspect { 9 10 //前置增强方法 11 public void beforeTarget(){ 12 System.out.println("前置增强.........."); 13 } 14 15 //后置增强方法 16 public void afterReturning(){ 17 System.out.println("后置增强.........."); 18 } 19 20 //环绕增强方法 21 public Object aroundTarget(ProceedingJoinPoint pjp) throws Throwable { //正在执行的连接点==切点 22 System.out.println("环绕前增强"); 23 Object proceed = pjp.proceed(); //切点方法 24 System.out.println("环绕后增强"); 25 return proceed; 26 } 27 28 //异常抛出增强方法 29 public void afterThrowing(){ 30 System.out.println("异常抛出增强...."); 31 } 32 33 //最终增强方法(不管抛不抛异常,该增强都会执行) 34 public void after(){ 35 System.out.println("最终增强...."); 36 } 37 38 }
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 xsi:schemaLocation=" 6 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 7 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> 8 9 <!--目标对象--> 10 <bean id="target" class="com.haifei.aop.Target"></bean> 11 12 <!--切面对象--> 13 <bean id="myAspect" class="com.haifei.aop.MyAspect"></bean> 14 15 <!--配置织入:告诉spring框架 哪些方法(切点)需要进行哪些增强(前置、后置...)--> 16 <aop:config> 17 <!--声明切面--> 18 <aop:aspect ref="myAspect"> 19 <!--切面:切点+通知--> 20 <!--<aop:before method="before" pointcut="execution(public void com.haifei.aop.Target.save())" />--> 21 22 <!--<aop:before method="beforeTarget" pointcut="execution(* com.haifei.aop.*.*(..))" /> 23 <aop:after-returning method="afterReturning" pointcut="execution(* com.haifei.aop.*.*(..))" />--> 24 25 <!--<aop:around method="aroundTarget" pointcut="execution(* com.haifei.aop.*.*(..))" /> 26 27 <aop:after-throwing method="afterThrowing" pointcut="execution(* com.haifei.aop.*.*(..))" /> 28 29 <aop:after method="after" pointcut="execution(* com.haifei.aop.*.*(..))" />--> 30 31 <!--配置-抽取切点表达式--> 32 <aop:pointcut id="myPointcut" expression="execution(* com.haifei.aop.*.*(..))"></aop:pointcut> 33 <aop:around method="aroundTarget" pointcut-ref="myPointcut" /> 34 <aop:after method="after" pointcut-ref="myPointcut" /> 35 36 </aop:aspect> 37 </aop:config> 38 39 </beans>
1 package com.haifei.test; 2 3 import com.haifei.aop.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.xml") 12 public class AopTest { 13 14 @Autowired 15 private TargetInterface target; 16 17 @Test 18 public void test1(){ 19 target.save(); 20 /* 21 applicationContext.xml 22 <aop:before method="before" pointcut="execution(public void com.haifei.aop.Target.save())" /> 23 配置织入前 24 save running... 25 26 配置织入后 27 前置增强.......... 28 save running... 29 */ 30 } 31 32 @Test 33 public void test2(){ 34 target.save(); 35 /* 36 <aop:before method="beforeTarget" pointcut="execution(* com.haifei.aop.*.*(..))" /> 37 38 效果同上test1 39 */ 40 } 41 42 @Test 43 public void test3(){ 44 target.save(); 45 /* 46 <aop:before method="beforeTarget" pointcut="execution(* com.haifei.aop.*.*(..))" /> 47 <aop:after-returning method="afterReturning" pointcut="execution(* com.haifei.aop.*.*(..))" /> 48 49 前置增强.......... 50 save running... 51 后置增强.......... 52 */ 53 } 54 55 @Test 56 public void test4(){ 57 target.save(); 58 /* 59 <aop:around method="aroundTarget" pointcut="execution(* com.haifei.aop.*.*(..))" /> 60 61 环绕前增强 62 save running... 63 环绕后增强 64 */ 65 } 66 67 @Test 68 public void test5(){ 69 target.save(); 70 /* 71 <aop:around method="aroundTarget" pointcut="execution(* com.haifei.aop.*.*(..))" /> 72 <aop:after-throwing method="afterThrowing" pointcut="execution(* com.haifei.aop.*.*(..))" /> 73 74 public void save() { 75 int i = 1/0; 76 System.out.println("save running..."); 77 } 78 环绕前增强 79 异常抛出增强.... 80 ArithmeticException: / by zero 81 82 public void save() { 83 System.out.println("save running..."); 84 int i = 1/0; 85 } 86 环绕前增强 87 save running... 88 异常抛出增强.... 89 ArithmeticException: / by zero 90 */ 91 } 92 93 @Test 94 public void test6(){ 95 target.save(); 96 /* 97 <aop:around method="aroundTarget" pointcut="execution(* com.haifei.aop.*.*(..))" /> 98 <aop:after-throwing method="afterThrowing" pointcut="execution(* com.haifei.aop.*.*(..))" /> 99 <aop:after method="after" pointcut="execution(* com.haifei.aop.*.*(..))" /> 100 101 public void save() { 102 System.out.println("save running..."); 103 int i = 1/0; 104 } 105 环绕前增强 106 save running... 107 异常抛出增强.... 108 最终增强.... 109 ArithmeticException: / by zero 110 111 public void save() { 112 System.out.println("save running..."); 113 } 114 环绕前增强 115 save running... 116 环绕后增强 117 最终增强.... 118 */ 119 } 120 121 /** 122 * 测试 抽取切点表达式 的抽取配置及使用 123 */ 124 @Test 125 public void test7(){ 126 target.save(); 127 /* 128 <aop:pointcut id="myPointcut" expression="execution(* com.haifei.aop.*.*(..))"></aop:pointcut> 129 <aop:around method="aroundTarget" pointcut-ref="myPointcut" /> 130 <aop:after method="after" pointcut-ref="myPointcut" /> 131 132 环绕前增强 133 save running... 134 环绕后增强 135 最终增强.... 136 */ 137 } 138 139 }
XML配置AOP详解
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 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 让容器管理更轻松!