这是一个非常简单的spring aop切面添加日志的程序,下面来看一下这个程序

1、程序使用jar包

2、切面类LoggingAspect.java

package com.cailei.aop.aspect;import java.util.Arrays;import org.aspectj.lang.JoinPoint;public class LoggingAspect {public void beforeMethod(JoinPoint joinPoint) {String methodName = joinPoint.getSignature().getName();Object[] args = joinPoint.getArgs();long times = System.currentTimeMillis();System.out.println("start execute time:" + times + "," + methodName + " start execute,args:" + Arrays.toString(args));}public void afterMethod(JoinPoint joinPoint) {String methodName = joinPoint.getSignature().getName();long times = System.currentTimeMillis();System.out.println("after execute time:" + times + "," + methodName + " execute end");}public void afterReturning(JoinPoint joinPoint, Object result) {String methodName = joinPoint.getSignature().getName();System.out.println(methodName + " execute result:" + result);}public void afterThrowing(JoinPoint joinPoint, Exception e) {String methodName = joinPoint.getSignature().getName();System.out.println(methodName + " execute exception:" + e);}}

3、业务类LoginService.java

package com.cailei.aop;import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;import com.cailei.aop.service.LoginService;public class Test {public static void main(String[] args) throws Exception {ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");LoginService ls = (LoginService)ac.getBean("loginService");ls.login("lucy", "123456");ls.login("lucy", "111111");}
}

4、spring配置文件applicationContext.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:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.0.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-3.0.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-3.0.xsd"><bean id="loggingAspect" class="com.cailei.aop.aspect.LoggingAspect"></bean><bean id="loginService" class="com.cailei.aop.service.LoginService"></bean><aop:config><aop:pointcut expression="execution(* com.cailei.aop.service.*.*(..))" id="logPointcut"/><aop:aspect ref="loggingAspect" order="1"><aop:before method="beforeMethod" pointcut-ref="logPointcut"/><aop:after method="afterMethod" pointcut-ref="logPointcut"/><aop:after-returning method="afterReturning" pointcut-ref="logPointcut" returning="result"/><aop:after-throwing method="afterThrowing" pointcut-ref="logPointcut" throwing="e"/></aop:aspect></aop:config></beans>

运行结果

spring aop 切面添加日志相关推荐

  1. 【经典】Spring aop切面实现异步添加日志—完整版

    系统开发中我们常遇到要处理系统日志等信息的,在此我分享一篇 利用spring aop切面来异步添加日志的操作,其中用到了 队列和多线程,前面的博客有写. 第一步:创建log实体,根据自己业务而定, p ...

  2. spring AOP切面日志

    spring AOP切面日志 导入依赖 <properties><fastjson.version>1.2.49</fastjson.version> </p ...

  3. Spring AOP 切面@Around注解的具体使用

    @Around注解可以用来在调用一个具体方法前和调用后来完成一些具体的任务. 比如我们想在执行controller中方法前打印出请求参数,并在方法执行结束后来打印出响应值,这个时候,我们就可以借助于@ ...

  4. Spring AOP切面的时候参数的传递

    Spring AOP切面的时候参数的传递 Xml: <?xml version="1.0" encoding="UTF-8"?> <beans ...

  5. 日志管理(spring AOP切面拦截)

    **最近,在写日志管理的东西呢,用了几种方法试,还是拦截比较好用,直接截下来传入到数据库中存储. 写的时候,真是头疼啊. 哦~对了,忘了说了,不需要用到Dao层哦. 首先,来一个POJO实体类吧,我看 ...

  6. 利用spring AOP注解实现日志管理

    最近刚接手一个项目,在项目的开始阶段,我们的架构师分配了个任务给我,让我利用spring的切面技术做一个日志管理的案例.要求很简单,就是需要记录:谁在什么时候对谁做了什么操作,同时在日志的描述中还要有 ...

  7. Spring AOP切面使用详细解析

    相关文章: SpringBoot AOP切面的使用 一步一步手绘Spring AOP运行时序图(Spring AOP 源码分析) 架构师系列内容:架构师学习笔记(持续更新)) Spring AOP 应 ...

  8. Spring AOP 切面(Aspect)应用详解

    1. AOP 切面应用 下面是一个AOP切面的一个简单的应用实例 引入AOP依赖 <dependency><groupId>org.springframework.boot&l ...

  9. Spring aop切面插入事物回滚

    <!-- tx标签配置 事物--> <tx:advice id="txadvice" transaction-manager="transactionM ...

最新文章

  1. Linux基础教程之linux文件权限深度解读
  2. Maple Warrior 个人简历
  3. Android Crash战斗日记(一、原理篇)
  4. 【链表】逆序打印链表
  5. matlab中的控制语句,MATLAB控制语句
  6. find_cmd函数分析
  7. mysql字符集解释_MySQL字符集详解
  8. CentOS7与CentOS6区别及特点
  9. Java 程序设计基础知识
  10. java 渲染器_用Java实现一个光线追踪渲染器(下)
  11. Java实现 LeetCode 506 相对名次
  12. Lect2 BFS总结
  13. 欧文分校计算机新sat多少分录取,加州大学欧文分校留学托福成绩要求95分 SAT1800分...
  14. NBA赛事直播超清画质背后:阿里云视频云「窄带高清2.0」技术深度解读
  15. 【html】关于doctype
  16. BGP MPLS VPN(OPTION A)实验笔记
  17. 智能驾驶 车牌检测和识别(一)《CCPD车牌数据集》
  18. 天气预报--桌面插件功能
  19. 初识Anaconda------潇哥带你入坑之python学习(一)
  20. 如何查看建筑设计图纸呢?CAD小白如何实现CAD快速看图?

热门文章

  1. latex如何绘制标准三线表 lllegal charactor in array arg 如何合并单元格?
  2. 螺旋探索与自适应混合变异的麻雀搜索-附代码
  3. 想要改变世界的 Rust 语言
  4. easyrecovery2023最新版本电脑数据恢复软件特点介绍
  5. HZNUOJ 2689 阅兵
  6. 庸殖状恐剐雷善菊囟蕾
  7. Unity的UGUI使用Text和Image实现文字下划线
  8. Windows服务器如何防止黑客入侵的安全设置
  9. 浅析大规模DDOS防御架构-应对T级攻防
  10. 解析button和input type=button 的区别