今天看啥
热点:

使用Aspect切面实现系统日志并存入数据库,aspect切面


使用Aspect切面实现系统日志并存入数据库

SpringMVC.xml中:开启切面注解

<aop:aspectj-autoproxy proxy-target-class="true" />

 自定义注解用于切面切入点

import java.lang.annotation.*;

/**
 * 系统日志注解
 * 
 * @author wangxueqing
 */
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @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
@Aspect
public 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 = "https://"+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();
      }
   }

}


查看评论

www.bkjia.comtruehttps://www.bkjia.com/ASPjc/1312444.htmlTechArticle使用Aspect切面实现系统日志并存入数据库,aspect切面 使用Aspect切面实现系统日志并存入数据库 SpringMVC.xml中:开启切面注解 aop:aspectj-autopro...

相关文章

相关搜索: 切面 注解 日志 log4j

帮客评论

视觉看点