Spring之AOP编程
注意添加:
com.springsource.org.aopalliance-1.0.0.jar
com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
两个jar包
方法调用拦截实现

package com.studio.advice; import org.aopalliance.intercept.MethodInterceptor; import org.aopalliance.intercept.MethodInvocation; import org.springframework.stereotype.Component; @Component public class MyAdvice implements MethodInterceptor { @Override public Object invoke(MethodInvocation invocation) throws Throwable { try{ System.out.println("interceptor-Befor:"); Object object= invocation.proceed(); System.out.println("interceptor-End:"); return object; }catch(Exception ex){ System.out.println("interceptor-ex:" + ex.getMessage()); throw ex; } } }
beens.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" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"> <context:component-scan base-package="com.studio.*"/> <aop:config> <aop:pointcut id="mypointcut" expression="execution(* com..service..*(..))"/> <aop:advisor advice-ref="myAdvice" pointcut-ref="mypointcut" order="1" /> <aop:advisor advice-ref="myAdvice2" pointcut="execution(* com..service..*(..))" order="2" /> </aop:config> </beans>
说明
pointcut的execution中“*”表示一个单词(字母组成中间没空格)而“..”表示0或任意多个
定义注解:

package com.studio; import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Target({ElementType.METHOD, ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface CheckLogin { }
注解切入点表达式

<aop:config> <aop:pointcut id="mypointcut" expression="execution(* com..service..*(..))"/> <aop:advisor advice-ref="myAdvice" pointcut-ref="mypointcut" order="1" /> <aop:advisor advice-ref="myAdvice2" pointcut="@annotation(com.studio.CheckLogin) or @target(com.studio.CheckLogin)" order="2" /> </aop:config>
说明:
允许在列或方法上放置CheckUser 以实现拦截
代码在参考华为网盘"软件测试与任务/spring"
参考:
http://blog.csdn.net/topwqp/article/details/8696897
http://blog.sina.com.cn/s/blog_5198c7370100hw1p.html
http://liusg123.iteye.com/blog/941546
http://lavasoft.blog.51cto.com/62575/172292/ 切入点表达式
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· [AI/GPT/综述] AI Agent的设计模式综述