使用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(); } }}
查看評論