使用Aspect切面實現系統日誌並存入資料庫,aspect切面

來源:互聯網
上載者:User

使用Aspect切面實現系統日誌並存入資料庫,aspect切面
使用Aspect切面實現系統日誌並存入資料庫SpringMVC.xml中:開啟切面註解

<aop:aspectj-autoproxy proxy-target-class="true" />
 自訂註解用於切面切入點
import java.lang.annotation.*;/** * 系統日誌註解 *  * @author wangxueqing */@Target(ElementType.METHOD)@Retention(RetentionPolicy.RUNTIME)@Documentedpublic @interface SysLog {   String value() default "";}import com.alibaba.fastjson.JSON;import com.sm.share3d.annotion.SysLog;import com.sm.share3d.bean.SysLogEntity;import com.sm.share3d.utils.HttpContextUtils;import com.sm.share3d.utils.IPUtils;import org.apache.http.HttpEntity;import org.apache.http.HttpResponse;import org.apache.http.client.HttpClient;import org.apache.http.client.entity.UrlEncodedFormEntity;import org.apache.http.client.methods.HttpPost;import org.apache.http.impl.client.DefaultHttpClient;import org.apache.http.message.BasicNameValuePair;import org.apache.http.util.EntityUtils;import org.aspectj.lang.JoinPoint;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Before;import org.aspectj.lang.annotation.Pointcut;import org.aspectj.lang.reflect.MethodSignature;import org.springframework.stereotype.Component;import javax.servlet.http.HttpServletRequest;import java.lang.reflect.Method;import java.net.URLEncoder;import java.text.SimpleDateFormat;import java.util.ArrayList;import java.util.Date;import java.util.List;/** * 系統日誌,切面處理類 *  * @author wangxueqing */@Component@Aspectpublic class SysLogAspect {   //切入點,以方法的形式存在   @Pointcut("@annotation(com.sm.share3d.annotion.SysLog)")   public void sportPoint(){      System.out.println(1111111);   }   @Before("sportPoint()")   public void before(JoinPoint joinPoint){      MethodSignature signature = (MethodSignature) joinPoint.getSignature();      Method method = signature.getMethod();      SysLogEntity logEntity = new SysLogEntity();      SysLog syslog = method.getAnnotation(SysLog.class);      if(syslog != null){         //註解上的描述         logEntity.setOperation(syslog.value());      }      //請求的方法名      String className = joinPoint.getTarget().getClass().getName();      String methodName = signature.getName();      logEntity.setMethod(className + "." + methodName + "()");      //請求的參數      Object[] args = joinPoint.getArgs();      if(args.length>0){         /*String params = JSON.toJSONString(args[0]);         logEntity.setParams(params);*/      }      //擷取request      HttpServletRequest request = HttpContextUtils.getHttpServletRequest();      //設定IP地址      logEntity.setIp(IPUtils.getIpAddr(request));      //使用者名稱//    String username = ShiroUtils.getUserEntity().getUsername();      logEntity.setUsername("超級管理員");      logEntity.setCreateDate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));      //儲存系統日誌//    sysLogService.save(logEntity);      doPost(logEntity);   }   public void doPost(SysLogEntity logEntity){      HttpServletRequest request = HttpContextUtils.getHttpServletRequest();      String url = "http://"+request.getLocalAddr()+":"+request.getLocalPort()+"/ServicePlatform/operation/saveOperation";      try {         //httpClient         HttpClient httpClient = new DefaultHttpClient();         // get method         HttpPost httpPost = new HttpPost(url);         // set header         httpPost.setHeader("Content-Type","application/x-www-form-urlencoded");         //set params         List<BasicNameValuePair> params = new ArrayList<>();         params.add(new BasicNameValuePair("username", URLEncoder.encode(logEntity.getUsername(),"utf-8")));         params.add(new BasicNameValuePair("operation",URLEncoder.encode(logEntity.getOperation(),"utf-8")));         params.add(new BasicNameValuePair("method",logEntity.getMethod()));         params.add(new BasicNameValuePair("params",logEntity.getParams()));         params.add(new BasicNameValuePair("ip",logEntity.getIp()));         params.add(new BasicNameValuePair("createDate",logEntity.getCreateDate()));         try{            httpPost.setEntity(new UrlEncodedFormEntity(params));         }catch (Exception e) {}         //response         HttpResponse response = null;         try{            response = httpClient.execute(httpPost);         }catch (Exception e) {}         //get response into String         String temp="";         try{            HttpEntity entity = response.getEntity();            temp=EntityUtils.toString(entity,"UTF-8");         }catch (Exception e) {}      }catch (Exception e){         e.printStackTrace();      }   }}


查看評論

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.