文章来至于http://www.thinksaas.cn/group/topic/341436/
第一:>>在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">
第二:>>配置一下@AspectJ 的bean,这个Bean就是一个普通的java对象bean,按照普通的bean来配置就行了,我的这个处理切面的bean配置如下 (包含了第一步的配置):
<!-- 支持 @AspectJ 标记--> <aop:aspectj-autoproxy /> <bean id="logAspect" class="com.hs.dolphin.sys.LogAspect"> <!-- 配置要记录日志的对象及属性(要监控的方法的第一个参数) 格式: 对象类名(带路径全称).属性GET方法名 --> <property name="obj_method"> <map> <entry key="default"> <map><!-- 默认值,如果没有指定对象就执行这个 --> <entry key="getId"><!-- 属性的get方法名字 --> <value>id</value><!-- 属性名字 --> </entry> </map> </entry> <entry key="com.hs.dolphin.domain.User"> <map> <entry key="getId"><!-- 属性的get方法名字 --> <value>id</value><!-- 属性名字 --> </entry> <entry key="getUserName"><!-- 属性的get方法名字 --> <value>userName</value><!-- 属性名字 --> </entry> </map> </entry> </map> </property> </bean>
第三:>>在这个切点处理bean类里,在class定义的上一行加上切面定义标签,如下:
@Aspect // 切面定义标签 public class LogAspect { private UserSession sessionValue;// 当前操作员基本信息 private String userIpAddress;// 当前操作员IP地址
第四:>>定义切点及处理方法
@Pointcut("execution(* *.*.*.service..*.insert*(..)) || execution(* *.*.*.service..*.update*(..)) " + "|| execution(* *.*.*.service..*.remove*(..))" + "|| execution(* *.*.*.service..*.delete*(..))") public void insertPointcut() { }
第五:>>Advice扩展处理
@AfterReturning("insertPointcut()") public void insertLogInfo(JoinPoint joinPoint) throws Exception{ /*处理代码,略*/ }
好了,完成了,是不是很容易.在第五步的这个方法里有一个参数,这个参数也许就是切 面自己传进来的,所以你就这样写吧,JoinPoint 对象里可以获取切面切点的相关信息,如方法名,参数,类路径,类名等等,这样我们就可以来记录操作者进行的操作的详细信息,比如执行的方法名,其所在的对 象类名,操作方法里带的所有参数信息,等,再加上自己另外获取一些信息,比如,当前操作时间,操作用户,还可以根据以上信息来定义操作类别等等