当前位置:编程学习 > JAVA >>

注解方式---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.setMsg(msg.toString());  
    // 记录DAO数据库操作日志  
    SysLogDAO.insertSysLogDetail(sysLogDetail);  
   }  
   // 执行数据库操作  
   object = joinPoint.proceed();  
  
   // 数据库查询缓存  
  } else if (Pattern.matches("(query|load|get|select|read)[\\S]*",  
    methodName)) {  
   // DAO层缓存注解  
   MemCacheKey cacheKey = new MemCacheKey();  
   // 获取cache注解属性  
   Cache cache = null;  
   // 获取请求方法  
   Class<?> cls = joinPoint.getTarget().getClass();  
   // 获取class中的所有方法  
   Method[] methods = cls.getMethods();  
   for (Method m : methods) {  
    // 获取执行方法前的注解信息。  
    if (m.getName().equals(methodName)) {  
     cache = m.getAnnotation(Cache.class);  
     break;  
    }  
   }  
  
   if (cache != null) {  
    // 获取memcacheKey,并进行MD5加密  
    cacheKey = memcacheKey(cache, args);  
    // 判断缓存服务器是否存在该可以值  
    if (memcache.exist(cacheKey.getMemcacheKey())) {  
     object = memcache.get(cacheKey.getMemcacheKey());  
    } else {  
     // 执行数据库操作  
     object = joinPoint.proceed();  
     // 将数据存放进缓存  
     if (cacheKey.getMemcacheKey() != null) {  
      memcache.put(cacheKey.getMemcacheKey(),  
        object == null ? "" : object, new Date(cacheKey  
          .getTime()));  
     
补充:软件开发 , Java ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,