当前位置: 首页 > news >正文

Spring教程-AOP

需要新增的包 jdk21兼容spring版本

<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.nhooo</groupId> <artifactId>spring-demo</artifactId> <version>1.0</version> <properties> <maven.compiler.source>21</maven.compiler.source> <maven.compiler.target>21</maven.compiler.target> </properties> <dependencies> <!-- Spring 5.x(支持 JDK 21) --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>5.3.39</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>5.3.39</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.3.39</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>5.3.39</version> </dependency> <!-- AspectJ 高版本 --> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjrt</artifactId> <version>1.9.19</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.9.19</version> </dependency> <!-- 日志 --> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.2</version> </dependency> </dependencies> </project>
1.注解
<bean class="org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator"></bean> 开启
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> <bean id="aAOP" class="com.smr.demoAop.A"> </bean> <bean id="Alog" class="com.smr.demoAop.Alog"></bean> <bean class="org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator"></bean> </beans>
package com.smr.demoAop; public class A { public void msg(){ System.out.println("msg method..."); } public int m(){ System.out.println("m method..."); return 2; } public int k(){ System.out.println("k method..."); return 3; } public void num(int age) throws Exception{ if(age<18){ throw new ArithmeticException("Not valid age"); }else{ System.out.println("正常1"); } } }
package com.smr.demoAop; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.*; @Aspect public class Alog { // @Pointcut("execution(* A.*(..))")//不管返回类型如何,它将应用于所有A类的所有方法 @Pointcut("execution(* A.m*(..))")//不管返回类型如何,它将应用于所有A类的m开头方法 public void advice(){ //System.out.println("Pointcut...."); } // @Before("advice()") //执行方法之前的操作 /** 结果 * k-satrt * myadvice-k-log.... * k method... */ // @After("advice()") /** * k-satrt * m method... * myadvice-k-log.... */ public void myadvice(JoinPoint jp) { System.out.println("myadvice-k-log...."); } //@AfterReturning(pointcut = "execution(* A.*(..))",returning = "result") /** * k-satrt * m method... * myadvice-k-log....start * int com.smr.demoAop.A.m() * 2 * myadvice-k-log....end */ public void myadvice(JoinPoint jp,Object result){ System.out.println("myadvice-k-log....start"); System.out.println(jp.getSignature()); System.out.println(result); //return 的结果 System.out.println("myadvice-k-log....end"); } //@Around("advice()") //必须要有返回值 方法前后执行 /** * k-satrt * myadvice-k-log....start * m method... * 2 * myadvice-k-log....end */ public Object myadvice(ProceedingJoinPoint jp) throws Throwable { System.out.println("myadvice-k-log....start"); Object ob= jp.proceed(); System.out.println(ob); System.out.println("myadvice-k-log....end"); return ob; } /** * 19 * k-satrt * 正常1 */ /** * 11 * k-satrt * myadvice-k-log....start * java.lang.ArithmeticException: Not valid age * myadvice-k-log....end */ @AfterThrowing(pointcut = "execution(* A.*(..))",throwing = "throwable") public void num(JoinPoint jp,Throwable throwable){ System.out.println("myadvice-k-log....start"); System.out.println(throwable); System.out.println("myadvice-k-log....end"); } }
package com.smr.demoAop; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class TestAOP { public static void main(String[] args) throws Exception { ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml"); A a=applicationContext.getBean("aAOP",A.class); System.out.println("k-satrt"); /** * k-satrt * myadvice-k-log....start * java.lang.ArithmeticException: Not valid age * myadvice-k-log....end */ // a.num(11); /** * k-satrt * 正常1 */ a.num(19); } }
2.xml文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> <!--xml配置--> <bean id="b" class="com.smr.demoAop.B"></bean> <bean id="blog" class="com.smr.demoAop.Blog"></bean> <!--start myadvice-log.... m method... --> <!--Blog注入切面--> <aop:config> <aop:aspect id="basp" ref="blog"> <aop:pointcut id="aspbefore" expression="execution(* com.smr.demoAop.B.*(..))"/> <aop:before method="myadvice" pointcut-ref="aspbefore"></aop:before> </aop:aspect> </aop:config> <!-- start m method... myadvice-log.... --> <!-- <aop:config> <aop:aspect id="basp" ref="blog"> <aop:pointcut id="aspbefore" expression="execution(* com.smr.demoAop.B.*(..))"/> <aop:after method="myadvice" pointcut-ref="aspbefore"></aop:after> </aop:aspect> </aop:config>--> </beans>
package com.smr.demoAop; public class B { public void msg(){ System.out.println("msg method..."); } public int m(){ System.out.println("m method..."); return 2; } public int k(){ System.out.println("k method..."); return 3; } }
package com.smr.demoAop; import org.aspectj.lang.JoinPoint; public class Blog { public void myadvice(JoinPoint jp){ System.out.println("myadvice-log...."); } }
package com.smr.demoAop; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class TestB { public static void main(String[] args) { ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml"); B b=applicationContext.getBean("b",B.class); System.out.println("start"); b.m(); } }
http://www.jsqmd.com/news/625620/

相关文章:

  • 软件行为驱动开发管理化的协作定义
  • SunnyUI中Pipe控件的动态数据可视化应用
  • 高性能FMC接口扩展卡详解:高速ADC/DAC设计、工程应用与参数对比
  • 云端隔断智慧工厂联系电话多少?2026年四川办公隔断源头工厂直供指南 - 精选优质企业推荐榜
  • Royal cove的实现(个人想法)
  • x86 - 64 架构下拆分锁性能测试:现状、挑战与未来
  • Nginx日志分割实战:如何用map指令按日期自动生成日志文件(附完整配置)
  • XUnity.AutoTranslator:如何为Unity游戏打造智能实时翻译系统
  • Java项目Loom升级实战:3步完成Spring WebFlux与虚拟线程深度整合(附压测对比数据)
  • 配电网电压与无功协调优化策略:降低运行成本、优化设备性能与场景对比分析
  • Qt 自定义控件动画深度解析:从 QPropertyAnimation 到源码内幕
  • 2026年四川成都办公玻璃隔断智能化方案深度横评:源头工厂直供与隐私保护的平衡之道 - 精选优质企业推荐榜
  • 音视频框架云原生应用
  • 2026年如何选择靠谱的6063铝型材厂家?从国耀铝业的实践看行业升级路径 - 企师傅推荐官
  • 【零信任AI服务网格架构】:基于eBPF+WebAssembly构建毫秒级策略引擎的9个关键决策点
  • 如何实现一个支持「撤回」功能的即时通讯系统?
  • 3分钟掌握JiYuTrainer:极域电子教室破解终极指南,告别课堂操作限制
  • [AI/应用/MCP] MCP Server/Tool 开发指南腋
  • 前端内存泄漏排查指南:Chrome DevTools高级用法
  • 2026年4月,联系别墅花园设计施工团队的实用办法,花园设计/规划设计/民宿规划设计,花园设计施工团队怎么联系 - 品牌推荐师
  • Finalshell连不上Linux?别慌!手把手教你排查并修复Connection timed out
  • 模型即服务(MaaS)架构已过时?SITS2026 2026新版标准强制要求的3类实时反馈闭环设计
  • 大模型如何在200ms内完成端侧推理?SITS2026权威披露4项轻量化部署硬核指标
  • 卡希诺水溶肥怎么样好用吗?深度实测与农户口碑
  • Python 网络编程从入门到精通:TCP/UDP/Socket 实战详解
  • Steam成就管理器完全指南:如何安全修复游戏成就问题
  • Ubuntu 24.04 + Wine 9.0 完美运行《文明5》中文版:DXVK配置全攻略
  • 特斯拉 FSD 芯片架构揭秘:如何通过专用化设计超越英伟达 Xavier?
  • 2026年四川智能办公隔断系统深度横评:源头工厂直供与空间通透革命 - 精选优质企业推荐榜
  • 网易云音乐自动听歌打卡完整指南:快速升级到LV10的终极方法