设为首页 加入收藏

TOP

注解方式---spring的AOP拦截用户操作(一)
2014-11-24 12:02:18 来源: 作者: 【 】 浏览:101
Tags:注解 方式 ---spring AOP 拦截 用户 操作
1、主要实现用户在进行某项操作时,多 数据库的更新、插入和删除详细信息。记录操作时的请求信息。
2、在进入Controller时,生成一个事物ID,在这个Controller中进行的所有DAO操作都绑定该事物ID。并进行记录日志信息。
Java代码
package com.centralsoft.filter;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Date;
import java.util.HashMap;
import java.util.regex.Pattern;
import net.sf.json.JSONObject;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import com.centralsoft.cache.CacheService;
import com.centralsoft.cache.annotations.Cache;
import com.centralsoft.cache.entity.MemCacheKey;
import com.centralsoft.entity.SysLogDetail;
import com.centralsoft.manager.pub.ThreadBean;
import com.centralsoft.manager.pub.ThreadId;
import com.centralsoft.pub.dao.SysLogDAO;
import com.centralsoft.webservice.pub.DateSHA;
/**
* DAO层AOP拦截器,实现记录用户操作过的所有方法和参数,并实现DAO层缓存
*
* @author Administrator
*
*/
@Aspect
@Component
public class AspectAutoDAOBean {
@Autowired
@Qualifier("CacheService")
private CacheService memcache;
@Autowired
@Qualifier("SysLogDAO")
private SysLogDAO SysLogDAO;
@Around("execution(* com.centralsoft.*.dao.Zr*DAO.*(..))")
public Object before(ProceedingJoinPoint joinPoint) throws Throwable {
// 获取请求事务ID信息
ThreadId threadId = new ThreadBean().getThreadId();
// 调用方法名称
String methodName = joinPoint.getSignature().getName();
// 调用参数
Object[] args = joinPoint.getArgs();
Object object = null;
// 数据库更新操作日志
if (Pattern.matches("(save|insert|add|delete|remove|del|update)[\\S]*",
methodName)) {
if (threadId != null && threadId.getTransactionalId() != null) {
// 获取执行请求事务ID
String transactionalId = threadId.getTransactionalId();
// 获取执行请求用户ID
String userId = threadId.getUserId();
SysLogDetail sysLogDetail = new SysLogDetail();
sysLogDetail.setXh(transactionalId);
sysLogDetail.setUserId(userId);
sysLogDetail.setMethod(methodName);
JSONObject msg = new JSONObject();
// 处理参数
for (Object temp : args) {
// 获取参数类型,不同参数类型数据处理不一样
Class< extends Object> paramClazz = temp.getClass();
String classType = paramClazz.getName();
if (classType.equals("java.lang.String")) {
msg.put("key", temp);
} else if (classType.equals("java.util.HashMap")) {
msg.putAll((HashMap< , >) temp);
} else if (classType.startsWith("com.")) {
try {
Field[] f = paramClazz.getDeclaredFields();
for (Field field : f) {
String fieldName = field.getName();
field.setAccessible(true);
msg.put(fieldName, field.get(temp));
}
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
}
}
}
sysLogDetail
首页 上一页 1 2 3 下一页 尾页 1/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Spring AOP记录系统日志 下一篇freemaker的初步使用

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: