有时候,我们在做项目时会遇到这样的需求:

给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 实现日志管理(简单教程)的更多相关文章

  1. Spring Boot 入门(五):集成 AOP 进行日志管理

    本篇文章是接着 Spring boot 入门(四):集成 Shiro 实现登陆认证和权限管理写的,按照前面几篇博客的教程,可以搭建一个简单的项目,主要包含了 Pagehelper+MyBatis 分页 ...

  2. Spring AOP进行日志记录,管理

    在java开发中日志的管理有很多种.我一般会使用过滤器,或者是Spring的拦截器进行日志的处理.如果是用过滤器比较简单,只要对所有的.do提交进行拦截,然后获取action的提交路径就可以获取对每个 ...

  3. Spring AOP进行日志记录

    在java开发中日志的管理有很多种.我一般会使用过滤器,或者是Spring的拦截器进行日志的处理.如果是用过滤器比较简单,只要对所有的.do提交进行拦截,然后获取action的提交路径就可以获取对每个 ...

  4. [置顶] 使用sping AOP 操作日志管理

    记录后台操作人员的登陆.退出.进入了哪个界面.增加.删除.修改等操作 在数据库中建立一张SYSLOG表,使用Sping 的AOP实现日志管理,在Sping.xml中配置 <!-- Spring ...

  5. 【Java分享客栈】超简洁SpringBoot使用AOP统一日志管理-纯干货干到便秘

    前言 请问今天您便秘了吗?程序员坐久了真的会便秘哦,如果偶然点进了这篇小干货,就麻烦您喝杯水然后去趟厕所一边用左手托起对准嘘嘘,一边用右手滑动手机看完本篇吧. 实现 本篇AOP统一日志管理写法来源于国 ...

  6. Spring AOP 完成日志记录

    Spring AOP 完成日志记录 http://hotstrong.iteye.com/blog/1330046

  7. Spring AOP初级——入门及简单应用

      在上一篇<关于日志打印的几点建议以及非最佳实践>的末尾提到了日志打印更为高级的一种方式——利用Spring AOP.在打印日志时,通常都会在业务逻辑代码中插入日志打印的语句,这实际上是 ...

  8. Spring AOP的日志记录

    现在的项目是Spring+MyBatis,前段时间项目经理让我干了一个活,就是给所有的controller里的所有方法加上日志记录的代码,其实没有多少,也就300来个方法,也没有抱怨什么,一边打着瞌睡 ...

  9. Spring AOP详解及简单应用

    Spring AOP详解   一.前言 在以前的项目中,很少去关注spring aop的具体实现与理论,只是简单了解了一下什么是aop具体怎么用,看到了一篇博文写得还不错,就转载来学习一下,博文地址: ...

随机推荐

  1. 系统学习python第六天学习笔记

    1.补充 1.列表方法补充 reverse,反转. v1 = [1,2,3111,32,13] print(v1) v1.reverse() print(v1) sort,排序 v1 = [11,22 ...

  2. static_cast 与 dynamic_caste, reinterpreter 的区别

    static_cast 强制转换 dynamic_caste 在运行时做检查,区别常见与子类转换为派生类 reinterpertor 意思时重解释,例如将void* 转换成其它类型

  3. 关于redis的使用总结

    简介 redis是一个开源的用c语言编写的数据库,但并不像常规的关系型数据库那样把数据存储在表中 , 而是像Java中的Map集合一样,存储的是一系列key=value键值对,redis属于NoSQL ...

  4. aop 实现原理

    aop 底层采用代理机制实现 接口 + 实现类 :spring 采用 jdk 的 动态代理 只有实现类:spring 采用 cglib 字节码增强 aop专业术语 1.target(目标) 需要被代理 ...

  5. 7.学完linux系统运维到底可以做什么?

    linux运维到底可以做什么?(略有改动原文.排版) 运维,很容易从字面理解为运营.维护. 很多朋友认为,在互联网公司中linux系统运维的工作就是安装系统,部署服务.处理紧急故障,为公司里的开发人员 ...

  6. D - Daydreaming Stockbroker Gym - 101550D

    题目链接:http://codeforces.com/gym/101550/attachments 总的来说就是要: 极大值卖出,极小值买入, 再加上端点时的特判. 还有就是会有连续几天股票价格相同的 ...

  7. PAT甲级——1140.Look-and-say Sequence (20分)

    Look-and-say sequence is a sequence of integers as the following: D, D1, D111, D113, D11231, D112213 ...

  8. Review For Exam

    Review For Exam [2019 福建省赛] 一个很简单的状态压缩DP,结果集体走偏 如何解决连续几日的限制问题?这种东西普通的DP很难写 #include <bits/stdc++. ...

  9. \_\_str\_\_和\_\_repr\_\_

    目录 __str__和__repr__ 一.__str__ 二.repr __str__和__repr__ 一.__str__ 打印时触发 class Foo: pass obj = Foo() pr ...

  10. MySQL主从及读写分离配置

    <<MySQL主从又叫做Replication.AB复制.简单讲就是A和B两台机器做主从后,在A上写数据,B也会跟着写数据,两者数据实时同步>> MySQL主从是基于binlo ...