使用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具体怎么用,看到了一篇博文写得还不错,就转载来学习一下,博文地址: ...
随机推荐
- Hadoop的FlieSystem类的使用
1.使用FileSystem类需要导入jar包 解压hadoop-2.7.7.tar.gz 复制如下三个jar包和lib下所有jar包到项目文件下的lib文件 2.查看文件信息 @Test publi ...
- uni-app真机调试报错request:fail abort解决方法
Android端真机调试访问本地接口数据时报错:request:fail abort 报错代码 onLoad: function(e) { uni.request({ url: 'http://loc ...
- h5-语义化标签的兼容性问题
1.html代码 <header>头</header> <nav>导航栏</nav> <main> <article>左< ...
- Vue2.0权限树组件
项目使用的饿了么的Element-Ui,权限树使用其树形控件: <el-tree :data="data" ></el-tree> 刚开始没有特殊需求,三级 ...
- Java中static变量作用和用法详解
static表示"全局"或者"静态"的意思,用来修饰成员变量和成员方法,也可以形成静态static代码块,但是Java语言中没有全局变量的概念. 被static ...
- 使用itop4412开发板单独编译驱动模块
上面我们的驱动是放在了内核源码的目录下来实现的编译成驱动模块,很多时候我们都是拿到一个驱动源码,不需要把他放到内核源码里面,而是直接把他编译成驱动模块,下面我们来讲下实现方法,我们还是以蜂鸣器的驱动为 ...
- 17.3.12---socket
1----如果要用python做一个服务器和客户端的通信程序,那么就一定得选择标准库中的scoket套接字模块,它支持多种网络协议:TCP/IP ,ICMP/IP, UDP ...
- ES6 find()
Array.prototype.find() 返回数组中满足提供测试函数的第一个元素的值,否则返回undefined let b = blogs.find(function(e) => { re ...
- 1016D.Vasya And The Matrix#矩阵存在
题目出处:http://codeforces.com/contest/1016/problem/D #include<iostream> #define ll long long int ...
- Django框架(五):模型(一) 定义属性
1. 定义属性 Django根据属性的类型确定以下信息: 当前选择的数据库支持字段的类型 渲染管理表单时使用的默认html控件 在管理站点最低限度的验证 django会为表创建自动增长的主键列,每个模 ...