近期,一个小伙伴遇到了此需求。要解决的问题就是:
记录用户在系统的操作,通过注解来灵活控制。 注解可以对方法进行修饰,描述。 后面会将注解上描述以及方法被调用时入参记录到数据库。 同时还需要对不同的操作进行分类(插入,修改,查看,下载/上传文件之类的),记录用户,时间以及IP,客户端User-agent . 我在这里将部分实现写了出来,实际在项目中可以直接参照进行修改就可以满足以上功能。
开发环境:W7 + Tomcat7 + jdk1.7 + Mysql5
框架:spring,springmvc,hibernate
于是乎,下班后动手写了个小demo,主要使用注解实现,思路如下:
1.打算在service 层切入,所以在springmvc配置文件中排除对service层的扫描
2.在spring配置文件中扫描没有被springmvc扫描的service层,aop对其增强
3.实现注解,注解要能满足记录【方法描述,参数描述,操作类型】等等
4.对拦截到的方法进行统一处理,持久化日志
代码结构图:
1.springmvc-servlet.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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd"
default-autowire="byName" >
<!-- 扫描的时候过滤掉Service层,aop要在service进行切入! -->
<strong><span style="color:#006600;"><context:component-scan base-package="com.billstudy.springaop">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service"/>
</context:component-scan>
<context:component-scan base-package="com.buyantech.log">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service"/>
</context:component-scan></span></strong>
<bean class="cn.org.rapid_framework.spring.web.servlet.mvc.support.ControllerClassNameHandlerMapping" >
<!-- <property name="caseSensitive" value="true"/> -->
<!-- 前缀可选 -->
<property name="pathPrefix" value="/"></property>
<!-- 拦截器注册 -->
<property name="interceptors">
<bean class="javacommon.springmvc.interceptor.SharedRenderVariableInterceptor"/>
</property>
</bean>
<!-- Default ViewResolver -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/pages"/>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
2.spring配置文件中扫描service层,还有其他几个配置文件相关性不大,可以下载项目后查看。这里不再贴出
<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd" default-autowire="byName" default-lazy-init="false">
<strong><span style="color:#009900;"><context:component-scan base-package="com.**.service" /></span></strong>
</beans>
3.Aop拦截处理,以及注解实现部分
/**
* 文件名:Operation.java
* 版权:Copyright 2014-2015 BuyanTech.All Rights Reserved.
* 描述:
* 修改人:Bill
* 修改时间:2014/11/03
* 修改内容: 无
*/
package com.billstudy.springaop.log.annotation;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import com.billstudy.springaop.log.enums.OperationType;
/**
* @Descrption该注解描述方法的操作类型和方法的参数意义
*/
@Target(value = ElementType.METHOD)
@Retention(value = RetentionPolicy.RUNTIME)
@Documented
public @interface Operation {
/**
* @Description描述操作类型,参见{@linkOperationType ,为必填项
*/
OperationType type();
/**
* @Description描述操作意义,比如申报通过或者不通过等
*/
String desc() default "";
/**
* @Description描述操作方法的参数意义,数组长度需与参数长度一致,否则无效
*/
String[] arguDesc() default {};
}
package com.billstudy.springaop.log.aspect;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import org.apache.log4j.Logger;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.refl