Welcome 微信登录

首页 / 软件开发 / JAVA / spring aop面向切面编程:如何来做一个强大的日志记录功能

spring aop面向切面编程:如何来做一个强大的日志记录功能2011-01-27巴士飞扬这个东西怎么做:spring aop 面向切面编程 如何来做一个强大的日志记录功能模板;

昨天经理把这个任务交给我,让我为公司现在的项目加上一个详细的日志记录功能模板,对所有的操作,至少是增删改运作进行一个记录,其要记录操作者,以及执行的方法,IP,以及操作的方法的参数.

我以前做过类似的功能,不过是在filter里做的,通过filter来检查action请求,记录请求中的参数及action名字.但是今天公司这个是要求用spring aop来做,这样就可以在spring里对要进行的日志记录方法进行一个配置.而且这样也就可以无缝集成到现有的系统中去了.

不过,很郁闷的是,我还没用这样做过,或者类似的功能.

==========================

哈哈,很爽,居然让我做好了.

其实也很简单,使用AOP的@AspectJ来做就可以了,方法步骤如下:

第一:>>在spring的配置文件里增加以下配置

<!-- 支持 @AspectJ 标记-->
<aop:aspectj-autoproxy />

如果发现插入后,eclipse提示这行有错误,那可能是你的spring配置有问题,你对照一下我的spring里的beans的头:

<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"
default-autowire="byName" default-lazy-init="true">

关于spring和AOP及@aspectj配置可以参考一下我收藏的以下文章

基于@AspectJ配置Spring AOP之一==>http://www.busfly.cn/csdn/post/700.html

Spring 2.0的新特性之@AspectJ==>http://www.busfly.cn/csdn/post/699.html

AspectJ如何实现AOP==>http://www.busfly.cn/csdn/post/698.html

Spring 2.0中的AOP实现-aspectj-Advice==>http://www.busfly.cn/csdn/post/693.html