Day13-10.AOP进阶-连接点

Day13-10.AOP进阶-连接点

1.连接点:

1.在Spring中用JoinPoint抽象了连接点,用它可以获得方法执行时的相关信息,如目标类名、方法名、方法参数等

​ 1.对于@Around通知,获取连接点信息只能使用ProceedingPoint

​ 2.对于其他四种通知,获取连接点信息只能使用JointPoint,它是ProceedingJoint的父类型

@Slf4j
@Component
@Aspect
public class MyAspect7 {

    //@Pointcut("@annotation()")
    @Pointcut("execution(* com.itheima.service.DeptService.*(..))")
    public void pt(){}

    @Around("pt()")
    public Object Around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {

        //1.获取 目标对象的类名
        String className = proceedingJoinPoint.getTarget().getClass().getName();
        log.info("目标对象的类名:{}", className);

        //2.获取 目标对象方法名
        String methodName = proceedingJoinPoint.getSignature().getName();
        log.info("目标对象方法名:{}", methodName);
        
        //3.获取目标对象运行时传入的参数
        Object[] args = proceedingJoinPoint.getArgs();
        log.info("目标对象运行时传入的参数:{}", Arrays.toString(args));

        //4.放行 目标方法执行
        Object result = proceedingJoinPoint.proceed();
        log.info("目标方法执行:{}", result);

        //5.获取目标方法运行的返回值
        log.info("目标方法运行的返回值:{}", result);

        log.info("MyAspect around after ...");

        return result;


    }
}

如果是其他四种通知,新参改成JoinPoint就可以了。

2023-10-10 21:17:22.095  INFO 21448 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
2023-10-10 21:17:22.096  INFO 21448 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2023-10-10 21:17:22.096  INFO 21448 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 0 ms
2023-10-10 21:17:22.110  INFO 21448 --- [nio-8080-exec-1] c.i.interceptor.LoginCheckInterceptor    : 请求url为:/depts
2023-10-10 21:17:22.171  INFO 21448 --- [nio-8080-exec-1] c.i.interceptor.LoginCheckInterceptor    : 令牌合法,放行
2023-10-10 21:17:22.178  INFO 21448 --- [nio-8080-exec-1] com.itheima.controller.DeptController    : 查询全部数据
2023-10-10 21:17:22.181  INFO 21448 --- [nio-8080-exec-1] com.itheima.aop.MyAspect7                : 目标对象的类名:com.itheima.service.impl.DeptServiceImpl
2023-10-10 21:17:22.182  INFO 21448 --- [nio-8080-exec-1] com.itheima.aop.MyAspect7                : 目标对象方法名:list
2023-10-10 21:17:22.182  INFO 21448 --- [nio-8080-exec-1] com.itheima.aop.MyAspect7                : 目标对象运行时传入的参数:[]
Creating a new SqlSession
SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@4e1289e7] was not registered for synchronization because synchronization is not active
2023-10-10 21:17:22.203  INFO 21448 --- [nio-8080-exec-1] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
2023-10-10 21:17:22.493  INFO 21448 --- [nio-8080-exec-1] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
JDBC Connection [HikariProxyConnection@375142233 wrapping com.mysql.cj.jdbc.ConnectionImpl@491ce28f] will not be managed by Spring
==>  Preparing: Select * from dept
==> Parameters: 
<==    Columns: id, name, create_time, update_time
<==        Row: 2, 万维网1, 2023-09-27 10:44:27, 2023-09-27 22:47:01
<==        Row: 3, 咨询部, 2023-09-27 10:44:27, 2023-09-27 10:44:27
<==        Row: 6, 就业部, 2023-09-27 20:33:08, 2023-09-27 20:33:08
<==        Row: 21, 瓦打, 2023-09-27 22:54:09, 2023-09-27 22:54:09
<==        Row: 23, qdawdawda, 2023-09-28 13:34:05, 2023-09-28 13:34:05
<==        Row: 24, dsdfs, 2023-09-28 13:34:09, 2023-09-28 13:34:09
<==        Row: 32, aoligei, 2023-10-07 23:08:05, 2023-10-07 23:08:05
<==        Row: 33,, 2023-10-10 19:08:35, 2023-10-10 19:08:35
<==        Row: 34, dada, 2023-10-10 20:42:02, 2023-10-10 20:42:02
<==      Total: 9
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@4e1289e7]
2023-10-10 21:17:22.535  INFO 21448 --- [nio-8080-exec-1] com.itheima.aop.MyAspect7                : 目标方法执行:[Dept(id=2, name=万维网1, createTime=2023-09-27T10:44:27, updateTime=2023-09-27T22:47:01), Dept(id=3, name=咨询部, createTime=2023-09-27T10:44:27, updateTime=2023-09-27T10:44:27), Dept(id=6, name=就业部, createTime=2023-09-27T20:33:08, updateTime=2023-09-27T20:33:08), Dept(id=21, name=瓦打, createTime=2023-09-27T22:54:09, updateTime=2023-09-27T22:54:09), Dept(id=23, name=qdawdawda, createTime=2023-09-28T13:34:05, updateTime=2023-09-28T13:34:05), Dept(id=24, name=dsdfs, createTime=2023-09-28T13:34:09, updateTime=2023-09-28T13:34:09), Dept(id=32, name=aoligei, createTime=2023-10-07T23:08:05, updateTime=2023-10-07T23:08:05), Dept(id=33, name=, createTime=2023-10-10T19:08:35, updateTime=2023-10-10T19:08:35), Dept(id=34, name=dada, createTime=2023-10-10T20:42:02, updateTime=2023-10-10T20:42:02)]
2023-10-10 21:17:22.542  INFO 21448 --- [nio-8080-exec-1] com.itheima.aop.MyAspect7                : 目标方法运行的返回值:[Dept(id=2, name=万维网1, createTime=2023-09-27T10:44:27, updateTime=2023-09-27T22:47:01), Dept(id=3, name=咨询部, createTime=2023-09-27T10:44:27, updateTime=2023-09-27T10:44:27), Dept(id=6, name=就业部, createTime=2023-09-27T20:33:08, updateTime=2023-09-27T20:33:08), Dept(id=21, name=瓦打, createTime=2023-09-27T22:54:09, updateTime=2023-09-27T22:54:09), Dept(id=23, name=qdawdawda, createTime=2023-09-28T13:34:05, updateTime=2023-09-28T13:34:05), Dept(id=24, name=dsdfs, createTime=2023-09-28T13:34:09, updateTime=2023-09-28T13:34:09), Dept(id=32, name=aoligei, createTime=2023-10-07T23:08:05, updateTime=2023-10-07T23:08:05), Dept(id=33, name=, createTime=2023-10-10T19:08:35, updateTime=2023-10-10T19:08:35), Dept(id=34, name=dada, createTime=2023-10-10T20:42:02, updateTime=2023-10-10T20:42:02)]
2023-10-10 21:17:22.542  INFO 21448 --- [nio-8080-exec-1] com.itheima.aop.MyAspect7                : MyAspect around after ...
postHandle...
afterHandle...
2023-10-10 21:17:23.871  INFO 21448 --- [nio-8080-exec-2] c.i.interceptor.LoginCheckInterceptor    : 请求url为:/depts
2023-10-10 21:17:23.872  INFO 21448 --- [nio-8080-exec-2] c.i.interceptor.LoginCheckInterceptor    : 令牌合法,放行
2023-10-10 21:17:23.872  INFO 21448 --- [nio-8080-exec-2] com.itheima.controller.DeptController    : 查询全部数据
2023-10-10 21:17:23.872  INFO 21448 --- [nio-8080-exec-2] com.itheima.aop.MyAspect7                : 目标对象的类名:com.itheima.service.impl.DeptServiceImpl
2023-10-10 21:17:23.872  INFO 21448 --- [nio-8080-exec-2] com.itheima.aop.MyAspect7                : 目标对象方法名:list
2023-10-10 21:17:23.872  INFO 21448 --- [nio-8080-exec-2] com.itheima.aop.MyAspect7                : 目标对象运行时传入的参数:[]
Creating a new SqlSession
SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@6ed0abca] was not registered for synchronization because synchronization is not active
JDBC Connection [HikariProxyConnection@1612142688 wrapping com.mysql.cj.jdbc.ConnectionImpl@491ce28f] will not be managed by Spring
==>  Preparing: Select * from dept
==> Parameters: 
<==    Columns: id, name, create_time, update_time
<==        Row: 2, 万维网1, 2023-09-27 10:44:27, 2023-09-27 22:47:01
<==        Row: 3, 咨询部, 2023-09-27 10:44:27, 2023-09-27 10:44:27
<==        Row: 6, 就业部, 2023-09-27 20:33:08, 2023-09-27 20:33:08
<==        Row: 21, 瓦打, 2023-09-27 22:54:09, 2023-09-27 22:54:09
<==        Row: 23, qdawdawda, 2023-09-28 13:34:05, 2023-09-28 13:34:05
<==        Row: 24, dsdfs, 2023-09-28 13:34:09, 2023-09-28 13:34:09
<==        Row: 32, aoligei, 2023-10-07 23:08:05, 2023-10-07 23:08:05
<==        Row: 33,, 2023-10-10 19:08:35, 2023-10-10 19:08:35
<==        Row: 34, dada, 2023-10-10 20:42:02, 2023-10-10 20:42:02
<==      Total: 9
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@6ed0abca]
2023-10-10 21:17:23.884  INFO 21448 --- [nio-8080-exec-2] com.itheima.aop.MyAspect7                : 目标方法执行:[Dept(id=2, name=万维网1, createTime=2023-09-27T10:44:27, updateTime=2023-09-27T22:47:01), Dept(id=3, name=咨询部, createTime=2023-09-27T10:44:27, updateTime=2023-09-27T10:44:27), Dept(id=6, name=就业部, createTime=2023-09-27T20:33:08, updateTime=2023-09-27T20:33:08), Dept(id=21, name=瓦打, createTime=2023-09-27T22:54:09, updateTime=2023-09-27T22:54:09), Dept(id=23, name=qdawdawda, createTime=2023-09-28T13:34:05, updateTime=2023-09-28T13:34:05), Dept(id=24, name=dsdfs, createTime=2023-09-28T13:34:09, updateTime=2023-09-28T13:34:09), Dept(id=32, name=aoligei, createTime=2023-10-07T23:08:05, updateTime=2023-10-07T23:08:05), Dept(id=33, name=, createTime=2023-10-10T19:08:35, updateTime=2023-10-10T19:08:35), Dept(id=34, name=dada, createTime=2023-10-10T20:42:02, updateTime=2023-10-10T20:42:02)]
2023-10-10 21:17:23.885  INFO 21448 --- [nio-8080-exec-2] com.itheima.aop.MyAspect7                : 目标方法运行的返回值:[Dept(id=2, name=万维网1, createTime=2023-09-27T10:44:27, updateTime=2023-09-27T22:47:01), Dept(id=3, name=咨询部, createTime=2023-09-27T10:44:27, updateTime=2023-09-27T10:44:27), Dept(id=6, name=就业部, createTime=2023-09-27T20:33:08, updateTime=2023-09-27T20:33:08), Dept(id=21, name=瓦打, createTime=2023-09-27T22:54:09, updateTime=2023-09-27T22:54:09), Dept(id=23, name=qdawdawda, createTime=2023-09-28T13:34:05, updateTime=2023-09-28T13:34:05), Dept(id=24, name=dsdfs, createTime=2023-09-28T13:34:09, updateTime=2023-09-28T13:34:09), Dept(id=32, name=aoligei, createTime=2023-10-07T23:08:05, updateTime=2023-10-07T23:08:05), Dept(id=33, name=, createTime=2023-10-10T19:08:35, updateTime=2023-10-10T19:08:35), Dept(id=34, name=dada, createTime=2023-10-10T20:42:02, updateTime=2023-10-10T20:42:02)]
2023-10-10 21:17:23.885  INFO 21448 --- [nio-8080-exec-2] com.itheima.aop.MyAspect7                : MyAspect around after ...
postHandle...
afterHandle...
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值