<dependency> <groupId>javassist</groupId> <artifactId>javassist</artifactId> <ve Rsion>3.12.1.ga</version></dependency><dependency><groupid>org.aspectj</groupid ><artifactId>aspectjweaver</artifactId><version>1.8.8</version></dependency>
package com.try2better.daily.component;import java.lang.annotation.documented;import java.lang.annotation.elementtype;import java.lang.annotation.retention;import java.lang.annotation.retentionpolicy;import java.lang.annotation.target;import org.springframework.core.ordered;import Org.springframework.core.annotation.Order, @Retention (retentionpolicy.runtime) @Target ({ Elementtype.method,elementtype.type}) @Documented//Highest priority @Order (ordered.highest_ Precedence) public @interface try2betteraspectlog {string name () default ""; }
package com.try2better.daily.component;import java.lang.reflect.modifier;import Java.util.hashmap;import java.util.map;import javassist. Classclasspath;import javassist. Classpool;import javassist. Ctclass;import javassist. ctmethod;import javassist.bytecode.codeattribute;import javassist.bytecode.localvariableattribute; import javax.servlet.http.httpservletrequest;import org.aspectj.lang.joinpoint;import org.aspectj.lang.annotation.afterreturning;import org.aspectj.lang.annotation.aspect;import org.springframework.stereotype.component;import org.springframework.web.context.request.requestcontextholder;import org.springframework.web.context.request.servletrequestattributes;import com.try2better.daily.entity.logrecordentity;import com.try2better.daily.util.loggerutil;@ aspect@componentpublic class logaspect { @AfterReturning (returning= " Result ", pointcut=" @aNnotation (Com.try2better.daily.component.Try2betterAspectLog) ") public void After (Joinpoint joinpoint,object result) throws ClassNotFoundException{ object target = joinpoint.gettarget (); if (target == null | | ! (Target instanceof basecontroller)) { throw new runtimeexception ("This controller does not inherit basecontroller!"); } BaseController controller = (Basecontroller) Joinpoint.gettarget (); httpservletrequest request = ( servletrequestattributes) Requestcontextholder.getrequestattributes ()). Getrequest (); String uri = request.getrequesturi (); string url = Request.getrequesturl (). toString (); string querystring = request.geTquerystring (); string classtype = joinpoint.gettarget (). GetClass (). GetName (); class<?> clazz = class.forname (ClassType); String classname = clazz.getname (); string methodname = joinpoint.getsignature (). GetName (); //Get method name //gets the parameter name and value map <string,object> parametermap = getfieldsnamevaluemap (Joinpoint); LogRecordEntity Logrecordentity = new logrecordentity (); Logrecordentity.seturl (URL);; &nbsP; logrecordentity.seturi (URI);; logrecordentity.setquerystring (queryString); logrecordentity.setuserid ("admin");; logrecordentity.setrequestdate (New java.util.Date ());; logrecordentity.setclassname (className );; logrecordentity.setmethodname (MethodName); logrecordentity.setparametermap (PARAMETERMAP); logrecordentity.setresult (result);; loggerutil.info (Controller.getlogger (), logrecordentity); } private map< String,object> getfiEldsnamevaluemap (Joinpoint joinpoint) { map<string,object > map=new HashMap<String,Object> (); try { object[] args = joinpoint.getargs (); string classtype = joinpoint.gettarget (). GetClass (). GetName (); Class<?> clazz = Class.forName (ClassType); string clazzname = clazz.getname (); string methodname = joinpoint.getsignature (). GetName (); //Get method name &nBsp; classpool pool = classpool.getdefault (); ClassClassPath Classpath = new classclasspath (This.getclass ()); pool.insertclasspath (ClassPath); ctclass cc = pool.get ( Clazzname); Ctmethod cm = cc.getdeclaredmethod (MethodName); javassist.bytecode.MethodInfo methodInfo = Cm.getmethodinfo (); codeattribute codeattribute&Nbsp;= methodinfo.getcodeattribute (); LocalVariableAttribute attr = (Localvariableattribute) Codeattribute.getattribute (Localvariableattribute.tag); if (attr == null) { throw new runtimeexception (); } int pos = Modifier.isstatic (Cm.getmodifiers ()) ? 0 : 1; for (int i = 0; i < cm.getparametertypes (). Length; i++) { map.put ( attr.variablename (I + pos), args[i]);//paramnames that is, the name of the parameter } } catch (exception e) {e.printstacktrace ();} return map; } }
package com.try2better.daily.entity;import java.util.date;import java.util.map;public Class logrecordentity {private string url;private string uri;private string queryString;private String userId;private Date requestDate;private String Classname;private string methodname;private map<string,object> parametermap;private object result;public string geturl () {return url;} Public void seturl (String url) {this.url = url;} Public string geturi () {return uri;} Public void seturi (String uri) {this.uri = uri;} Public string getquerystring () {return querystring;} Public void setquerystring (string querystring) {this.querystring = querystring;} Public string getuserid () {return userid;} Public void setuseRId (String userid) {this.userid = userid;} Public date getrequestdate () {return requestdate;} Public void setrequestdate (Date requestdate) {this.requestdate = requestdate;} Public string getclassname () {return classname;} Public void setclassname (String classname) {this.classname = classname;} Public string getmethodname () {return methodname;} Public void setmethodname (String methodname) {this.methodname = methodname;} Public map<string, object> getparametermap () {return parametermap;} Public void setparametermap (Map<string, object> parametermap) { This.parametermap = parametermap;} Public object getresult () {return result;} Public void setresult (Object result) {this.result = result;}}
package com.try2better.daily.util;import org.slf4j.logger; Import com.alibaba.fastjson.jsonobject;import com.try2better.daily.entity.logrecordentity;public class loggerutil {public static void info (logger logger,logrecordentity logrecordentity) {logger.info (jsonobject.tojsonstring (logrecordentity))}}
package com.try2better.daily.controller;import java.io.bufferedinputstream;import java.io.file; Import java.io.fileinputstream;import java.io.ioexception;import java.io.outputstream;import java.net.URLEncoder;import java.util.HashMap;import java.util.List;import java.util.Map; Import javax.servlet.http.httpservletrequest;import javax.servlet.http.httpservletresponse;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.requestmapping;import Org.springframework.web.bind.annotation.responsebody;import com.try2better.daily.component.basecontroller ;import com.try2better.daily.component.try2betteraspectlog;import com.try2better.daily.entity.task; import com.try2better.daily.util.qrcode.encoderequestentity;import com.try2better.daily.util.qrcode.encoderesultentity;import com.try2better.daily.util.qrcode.qrcodehelper; @Controller @requestmapping ("/") public class qrcodecontroller extends basecontroller{@ Try2betteraspectlog@responsebody@requestmapping ("/method") public map<string,object> method1 ( STRING&NBSP;A,INTEGER&NBSP;B) {map<string,object> map = new hashmap<string, Object> (); Map.put ("1", "1"); return map;}}
Package Com.try2better.daily.component;import Org.slf4j.logger;import Org.slf4j.loggerfactory;public class Basecontroller {protected final Logger Logger = Loggerfactory.getlogger (GetClass ());p ublic Logger GetLogger () {return Logger;}}
Spring AOP Logging