<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    176142998

      BlogJava :: 首頁 :: 聯(lián)系 :: 聚合  :: 管理
      116 Posts :: 0 Stories :: 45 Comments :: 0 Trackbacks

    1、主要實(shí)現(xiàn)用戶在進(jìn)行某項(xiàng)操作時,多數(shù)據(jù)庫的更新、插入和刪除詳細(xì)信息。記錄操作時的請求信息。
    2、在進(jìn)入Controller時,生成一個事物ID,在這個Controller中進(jìn)行的所有DAO操作都綁定該事物ID。并進(jìn)行記錄日志信息。


    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攔截器,實(shí)現(xiàn)記錄用戶操作過的所有方法和參數(shù),并實(shí)現(xiàn)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 {
      // 獲取請求事務(wù)ID信息
      ThreadId threadId = new ThreadBean().getThreadId();
      // 調(diào)用方法名稱
      String methodName = joinPoint.getSignature().getName();
      // 調(diào)用參數(shù)
      Object[] args = joinPoint.getArgs();
      Object object = null;

      // 數(shù)據(jù)庫更新操作日志
      if (Pattern.matches("(save|insert|add|delete|remove|del|update)[\\S]*",
        methodName)) {
       if (threadId != null && threadId.getTransactionalId() != null) {
        // 獲取執(zhí)行請求事務(wù)ID
        String transactionalId = threadId.getTransactionalId();
        // 獲取執(zhí)行請求用戶ID
        String userId = threadId.getUserId();
        SysLogDetail sysLogDetail = new SysLogDetail();
        sysLogDetail.setXh(transactionalId);
        sysLogDetail.setUserId(userId);
        sysLogDetail.setMethod(methodName);
        JSONObject msg = new JSONObject();
        // 處理參數(shù)
        for (Object temp : args) {
         // 獲取參數(shù)類型,不同參數(shù)類型數(shù)據(jù)處理不一樣
         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數(shù)據(jù)庫操作日志
        SysLogDAO.insertSysLogDetail(sysLogDetail);
       }
       // 執(zhí)行數(shù)據(jù)庫操作
       object = joinPoint.proceed();

       // 數(shù)據(jù)庫查詢緩存
      } 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) {
        // 獲取執(zhí)行方法前的注解信息。
        if (m.getName().equals(methodName)) {
         cache = m.getAnnotation(Cache.class);
         break;
        }
       }

       if (cache != null) {
        // 獲取memcacheKey,并進(jìn)行MD5加密
        cacheKey = memcacheKey(cache, args);
        // 判斷緩存服務(wù)器是否存在該可以值
        if (memcache.exist(cacheKey.getMemcacheKey())) {
         object = memcache.get(cacheKey.getMemcacheKey());
        } else {
         // 執(zhí)行數(shù)據(jù)庫操作
         object = joinPoint.proceed();
         // 將數(shù)據(jù)存放進(jìn)緩存
         if (cacheKey.getMemcacheKey() != null) {
          memcache.put(cacheKey.getMemcacheKey(),
            object == null ? "" : object, new Date(cacheKey
              .getTime()));
         }
        }
       } else {
        // 執(zhí)行數(shù)據(jù)庫操作
        object = joinPoint.proceed();
       }
      } else {
       // 執(zhí)行數(shù)據(jù)庫操作
       object = joinPoint.proceed();
      }

      return object;

     }

     /**
      * 獲取根據(jù)注解中的key獲取memcache的含參數(shù)key值
      *
      * @param cache
      * @param parameterObject
      * @return
      * @author fei.zhao 2011-10-10
      */
     @SuppressWarnings("unchecked")
     private static MemCacheKey memcacheKey(Cache cache, Object[] args) {
      MemCacheKey tempKey = new MemCacheKey();
      String key = "";
      boolean flag = true;
      StringBuilder keyBuilder = new StringBuilder(32);
      // 獲取注解中的key值
      String cacheKey = cache.key();
      Object[] cacheArgs = cacheKey.split("\\.");

      // 設(shè)置請求參數(shù)在args[]中的序號
      // key參數(shù)進(jìn)行循環(huán)遍歷
      for (Object s : cacheArgs) {
       // 判斷是否是格式$,$...
       if (s.toString().startsWith("$")) {
        // 獲取參數(shù)名稱
        String type = s.toString().substring(1);
        // 獲取參數(shù)值
        Object temp = args[0];
        // 獲取參數(shù)類型,不同參數(shù)類型數(shù)據(jù)處理不一樣
        Class<? extends Object> paramClazz = temp.getClass();
        String classType = paramClazz.getName();
        if (classType.equals("java.lang.String")) {
         keyBuilder.append(temp);
        } else if (classType.equals("java.util.HashMap")) {
         keyBuilder.append(((HashMap) temp).get(type));
        } else if (classType.startsWith("com.")) {
         try {
          Field f = paramClazz.getDeclaredField(type);// 實(shí)體中字段
          f.setAccessible(true);// 允許訪問私有字段
          keyBuilder.append(f.get(temp));
         } catch (SecurityException e) {
          flag = false;
          e.printStackTrace();
         } catch (NoSuchFieldException e) {
          flag = false;
          e.printStackTrace();
         } catch (IllegalArgumentException e) {
          flag = false;
          e.printStackTrace();
         } catch (IllegalAccessException e) {
          flag = false;
          e.printStackTrace();
         }
        }
       } else {
        keyBuilder.append(s);
       }
       // 每個參數(shù)后面添加 “.”號分隔
       keyBuilder.append(".");
      }
      if (args.length == 3) {
       keyBuilder.append(args[1] + ".").append(args[2]);
      }
      if (flag == true) {
       key = keyBuilder.toString();
       tempKey.setMemcacheKey(DateSHA.shaEncrypt(key));
       tempKey.setTime(cache.time());
      }
      return tempKey;
     }
    }

    posted on 2011-11-07 19:48 飛飛 閱讀(7750) 評論(0)  編輯  收藏

    只有注冊用戶登錄后才能發(fā)表評論。


    網(wǎng)站導(dǎo)航:
     
    主站蜘蛛池模板: 在线jyzzjyzz免费视频| 国产国产人免费视频成69堂| 国产男女猛烈无遮挡免费网站 | 亚洲AV一区二区三区四区| 无码乱肉视频免费大全合集 | 亚洲国产成人久久综合一区77| 亚洲国产AV一区二区三区四区| 成人免费看片又大又黄| 亚洲一卡2卡三卡4卡无卡下载| 四虎国产精品免费久久| 亚洲第一综合天堂另类专| 免费人成视频在线观看视频| 特级毛片全部免费播放| 国产性爱在线观看亚洲黄色一级片| 国产成人精品免费视频大全| 久久亚洲综合色一区二区三区| 野花香在线视频免费观看大全 | 亚洲精品视频在线免费| 免费人成又黄又爽的视频在线电影| 国产精品成人四虎免费视频| 无遮挡免费一区二区三区 | 亚洲韩国—中文字幕| 91九色老熟女免费资源站| 美女视频黄免费亚洲| 亚洲国产一级在线观看 | 性xxxx黑人与亚洲| 国产成人免费永久播放视频平台| 成人a毛片免费视频观看| 国产成A人亚洲精V品无码| 18级成人毛片免费观看| 亚洲日本成本人观看| 国产亚洲精午夜久久久久久| 在线美女免费观看网站h| 最新亚洲精品国偷自产在线| 亚洲第一区在线观看| 最近2018中文字幕免费视频| 久久人午夜亚洲精品无码区| 亚洲AV成人无码久久精品老人| 毛片网站免费在线观看| 中文字幕免费观看视频| 色偷偷亚洲女人天堂观看欧|