使用Spring AOP 实现日志管理(简单教程)
有时候,我们在做项目时会遇到这样的需求:
给XXX.java中的所有方法加上指定格式的日志输出。
针对这种指定类、或者指定方法进行共性操作的功能,我们完全可以使用Spring AOP来实现。
本文使用注解方式:
步骤如下:
package com.longti.ydgj.util; import java.util.LinkedHashMap; import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory; 13
14 @Aspect // 指定当前类为切面类
public class MatchLogAspect { private static final Logger logger = LoggerFactory.getLogger(MatchLogAspect. class); // 指定切入点表单式: 拦截哪些方法; 即为哪些类生成代理对象
@Pointcut("execution(* com.longti.ydgj.webservice.impl.MatchWebServiceImpl.*(..))")
public void pointCut_1(){
} // 前置通知 : 在执行目标方法之前执行
@Before("pointCut_1()")
public void printLog1(JoinPoint joinPoint){
try{
String methodName=joinPoint.getTarget().getClass()+"."+joinPoint.getSignature().getName() + "()";
logger.info(methodName+"--请求参数开始--->");
if(joinPoint.getArgs().length>0){
for (int i = 0; i < joinPoint.getArgs().length; i++) {
LinkedHashMap<String, Object> jsonParam=(LinkedHashMap<String, Object>)joinPoint.getArgs()[i];
if(jsonParam.size()>0){
for(String key:jsonParam.keySet()){
StringBuffer sb=new StringBuffer();
sb.append(key+"--=>");
sb.append(jsonParam.get(key)==null?"":jsonParam.get(key).toString());
logger.info(sb.toString());
}
}
}
}
logger.info(methodName+"--请求参数结束--->");
}catch(Exception e){
e.printStackTrace();
}
} }
在切面类中定义切入点,以及拦截方法后进行的操作。
设置好了切面类之后,需要在spring配置文件中进行相关aop配置:
1.开启注解扫描:
<context:component-scan base-package="com.longti.ydgj.util" />
2.
<!-- 开启aop注解方式 -->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
3.
如果切面类没有使用@Component来创建实例的话,则需要在spring配置文件中声明bean
<!--切面-->
<bean id="matchLogAspect" class="com.longti.ydgj.util.MatchLogAspect"></bean>
综上,搞定。
切入点表达式讲解(以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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- dao 实例 -->
<bean id="userDao" class="cn.itcast.g_pointcut.UserDao"></bean>
<bean id="orderDao" class="cn.itcast.g_pointcut.OrderDao"></bean> <!-- 切面类 -->
<bean id="aop" class="cn.itcast.g_pointcut.Aop"></bean> <!-- Aop配置 -->
<aop:config> <!-- 定义一个切入点表达式: 拦截哪些方法 -->
<!--<aop:pointcut expression="execution(* cn.itcast.g_pointcut.*.*(..))" id="pt"/>--> <!-- 【拦截所有public方法】 -->
<!--<aop:pointcut expression="execution(public * *(..))" id="pt"/>--> <!-- 【拦截所有save开头的方法 】 -->
<!--<aop:pointcut expression="execution(* save*(..))" id="pt"/>--> <!-- 【拦截指定类的指定方法, 拦截时候一定要定位到方法】 -->
<!--<aop:pointcut expression="execution(public * cn.itcast.g_pointcut.OrderDao.save(..))" id="pt"/>--> <!-- 【拦截指定类的所有方法】 -->
<!--<aop:pointcut expression="execution(* cn.itcast.g_pointcut.UserDao.*(..))" id="pt"/>--> <!-- 【拦截指定包,以及其自包下所有类的所有方法】 -->
<!--<aop:pointcut expression="execution(* cn..*.*(..))" id="pt"/>--> <!-- 【多个表达式】 -->
<!--<aop:pointcut expression="execution(* cn.itcast.g_pointcut.UserDao.save()) || execution(* cn.itcast.g_pointcut.OrderDao.save())" id="pt"/>-->
<!--<aop:pointcut expression="execution(* cn.itcast.g_pointcut.UserDao.save()) or execution(* cn.itcast.g_pointcut.OrderDao.save())" id="pt"/>--> <!-- 【取非值】 -->
<aop:pointcut expression=" not execution(* cn.itcast.g_pointcut.OrderDao.save())" id="pt"/> <!-- 切面 -->
<aop:aspect ref="aop">
<!-- 环绕通知 -->
<aop:around method="around" pointcut-ref="pt"/>
</aop:aspect>
</aop:config>
</beans>
欢迎讨论。。。
使用Spring AOP 实现日志管理(简单教程)的更多相关文章
- Spring Boot 入门(五):集成 AOP 进行日志管理
本篇文章是接着 Spring boot 入门(四):集成 Shiro 实现登陆认证和权限管理写的,按照前面几篇博客的教程,可以搭建一个简单的项目,主要包含了 Pagehelper+MyBatis 分页 ...
- Spring AOP进行日志记录,管理
在java开发中日志的管理有很多种.我一般会使用过滤器,或者是Spring的拦截器进行日志的处理.如果是用过滤器比较简单,只要对所有的.do提交进行拦截,然后获取action的提交路径就可以获取对每个 ...
- Spring AOP进行日志记录
在java开发中日志的管理有很多种.我一般会使用过滤器,或者是Spring的拦截器进行日志的处理.如果是用过滤器比较简单,只要对所有的.do提交进行拦截,然后获取action的提交路径就可以获取对每个 ...
- [置顶] 使用sping AOP 操作日志管理
记录后台操作人员的登陆.退出.进入了哪个界面.增加.删除.修改等操作 在数据库中建立一张SYSLOG表,使用Sping 的AOP实现日志管理,在Sping.xml中配置 <!-- Spring ...
- 【Java分享客栈】超简洁SpringBoot使用AOP统一日志管理-纯干货干到便秘
前言 请问今天您便秘了吗?程序员坐久了真的会便秘哦,如果偶然点进了这篇小干货,就麻烦您喝杯水然后去趟厕所一边用左手托起对准嘘嘘,一边用右手滑动手机看完本篇吧. 实现 本篇AOP统一日志管理写法来源于国 ...
- Spring AOP 完成日志记录
Spring AOP 完成日志记录 http://hotstrong.iteye.com/blog/1330046
- Spring AOP初级——入门及简单应用
在上一篇<关于日志打印的几点建议以及非最佳实践>的末尾提到了日志打印更为高级的一种方式——利用Spring AOP.在打印日志时,通常都会在业务逻辑代码中插入日志打印的语句,这实际上是 ...
- Spring AOP的日志记录
现在的项目是Spring+MyBatis,前段时间项目经理让我干了一个活,就是给所有的controller里的所有方法加上日志记录的代码,其实没有多少,也就300来个方法,也没有抱怨什么,一边打着瞌睡 ...
- Spring AOP详解及简单应用
Spring AOP详解 一.前言 在以前的项目中,很少去关注spring aop的具体实现与理论,只是简单了解了一下什么是aop具体怎么用,看到了一篇博文写得还不错,就转载来学习一下,博文地址: ...
随机推荐
- I420转RGB
http://blog.csdn.net/huiguixian/article/details/17288909 public class YuvToRGB { private static int ...
- English Grammar - Subject Clause
that引导主语从句 一般置于句末,偶尔也置于句首 that引导的主语从句置于句首 That the seas are being overfished has been known for year ...
- python编程:从入门到实践----第五章:if语句>练习
5-1 条件测试 :编写一系列条件测试:将每个测试以及你对其结果的预测和实际结果都打印出来. a. 详细研究实际结果,直到你明白了它为何为True 或False b. 创建至少2个测试,且其中结果分别 ...
- linux mint使用中的问题解决记录
软件升级失败 换用命令行 sudo apt update sudo apt list --upgradable sudo apt upgrade sudo apt autoremove Enter p ...
- mysql字段修改脚本
-- help_text:帮助说明 -- help_content -- raw USE pro_seal_chip_sell_portal_v1; -- 表修改ALTER TABLE `help_t ...
- 对于 C语言的扩展和JAVA的重载理解
哎,又被学长看成笨蛋了 ,先前学习java,自己真是什么都要忘了,弄得自己连java最重要的概念--重载,都不知道是啥,还厚着脸皮和学长说 是函数名字一样 ,但是就是函数里面的参数和参数类型不一 ...
- JS 2019-12-03T15:53:23.000+08:00 转化为 YYYY MM DD
js时间格式转化 2019-12-03T15:53:23.000+08:00 转化为 YYYY MM DD var dateee = new Date(createTime).toJSON();var ...
- 拾起HTML+CSS的一天
今天在w3school看了下HTML5和CSS3的新特性. 本来觉得自己对CSS方面基础还挺有把握的,就之前自学都是跳过这个模块的,但经过昨天会友的面试,感觉自己好像太忽视基础了,很多基本的东西很模糊 ...
- is,==区别,编码转换
一.is 比较的是内存地址 == 比较的是两边的值 二.编码扩展: 1字节(bytes) = 8 位(bit) 1024byte = 1kb 1024kb = 1MB 1024MB = 1GB 102 ...
- iOS 10 新增plist文件属性
大概统计了一下需要加的一些字段列在下面: NSContactsUsageDescription -> 通讯录 NSMicrophoneUsageDescription -> 麦克风 NSP ...