欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 汽车 > 新车 > AOP进阶-05.连接点

AOP进阶-05.连接点

2025/2/27 10:24:15 来源:https://blog.csdn.net/qq_45055856/article/details/145890553  浏览:    关键词:AOP进阶-05.连接点

一.连接点 

JoinPoint有两个,要使用org,aspectj.lang

package com.gjw.aop;import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;import java.util.Arrays;/*** 目标:掌握AOP中的连接点技术*      所谓连接点,就是可以被AOP控制的方法,而在SpringAOP中,这个链接点又特指方法的执行* 在Spring中用JoinPoint抽象了连接点,用它可以获得方法执行时的相关信息,如目标类名、方法名、方法参数等*      对于@Around通知,获取连接点信息只能使用 ProceedingJoinPoint*      对于其他四种通知,获取连接点信息只能使用JoinPoint,它是ProceedingJoinPoint的父类型*/
@Slf4j
@Component
@Aspect
public class MyAspect8 {@Pointcut("execution(* com.gjw.service.impl.DeptServiceImpl.*(..))")public void pt(){}@Around("pt()")public Object around(ProceedingJoinPoint joinPoint) throws Throwable {log.info("MyAspect8 around before......");// 1.   获取 目标对象的类名String className = joinPoint.getTarget().getClass().getName();log.info("目标对象的类名:{}",className);// 2.   获取  目标对象的方法名String methodName = joinPoint.getSignature().getName();log.info("目标对象的方法名:{}",methodName);// 3.   获取  目标方法运行时传入的参数Object[] args = joinPoint.getArgs();log.info("目标方法运行时传入的参数:{}", Arrays.toString(args));// 4.   放行  目标方法执行Object result = joinPoint.proceed();// 5.   获取  目标方法运行后的返回值log.info("目标方法运行后的返回值:{}", result);log.info("MyAspect8 around after");return result;}
}

Test中的测试代码

package com.gjw.springbootaopquickstart;import com.gjw.pojo.Dept;
import com.gjw.service.DeptService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;import java.util.List;@SpringBootTest
class SpringbootAopQuickstartApplicationTests {@Autowiredprivate DeptService deptService;@Testvoid contextLoads() {}@Testpublic void testAopDelete() {deptService.deleteById(10);}@Testpublic void testAopList() {List<Dept> list = deptService.list();System.out.println(list);}@Testpublic void testAopGetById() {Dept dept = deptService.getById(1);System.out.println(dept);}}

执行testAopList()方法,执行结果如下:

如果我们将result返回的结果改为null,则list方法(原始方法)就拿不到返回值了。返回null,就相当于原始方法执行完后,结果就丢了。因此要将result返回回去,设置通知方法的返回值类型为Object。 

 执行testAopDelete()方法,执行结果如下:

delete方法没有返回值。 

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词