🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
[TOC] # 使用注解 需要改下配置文件 ~~~ <!-- 准备工作:导入aop(约束)命名空间 --> <!-- 1.配置目标对象 --> <bean name="userService" class="service.UserServiceImpl"></bean> <!-- 2.配置通知对象 --> <bean name="myAdvice" class="aspect.MyAdvice"></bean> <!-- 3.开启使用注解完成织入,开启aspectJ注解自动代理功能 --> <aop:aspectj-autoproxy /> </beans> ~~~ # 注解定义切点 使用@Pointcut注解定义切点 在每一个通知中定义切点,工作量大,不方便维护,我们可以使用@Pointcut来声明切点 切点允许逻辑运算例如`mypointcut()||mypointcut1` ~~~ @Pointcut("execution(* *.s*(..))") private void mypointcut(){} @Pointcut("execution(* *.update(..))") private void mypointcut1(){} //前置通知 @Before("mypointcut()||mypointcut1()") public void before() { Systemctl.out.println("前置通知...") } ~~~ # 异常信息的捕获 ~~~ /** * 异常通知 */ @AfterThrowing(pointcut = "MyAdvice.pc()", throwing = "e") public void afterException(Exception e) { System.out.println("出现异常了: " + e.toString()); } ~~~ 加exception需要原方法有抛出异常