Step1 Open Section Programming
<!-- Open section programming (weaving @aspectj facets by configuration)- <aop:aspectj-autoproxy/>
<aop:aspectj-autoproxy/> has a Proxy-target-class property, which defaults to false, which means that the use of JDK dynamic agent weaving enhancements, when fitted as <aop:aspectj-autoproxy Poxy-target-class= "true"/> Represents the use of Cglib dynamic Agent technology to weave in the enhancement. However, even if the Proxy-target-class is set to False, if the target class does not declare an interface, spring will automatically use the Cglib dynamic proxy.
Step2 writing a log annotation class
@Target ({elementtype.parameter, elementtype.method}) @Retention (retentionpolicy.runtime) @Documented Public @interface Systemlog { default "";}
@Aspect @componentPublicClassSystemlogaspect {@Pointcut ("@annotation (Com.tj.common.log.system.SystemLog)")PublicvoidControlleraspect () {} @Pointcut ("@annotation (Com.tj.common.log.system.SystemLog)")PublicvoidServiceaspect () {} @Pointcut ("@annotation (Com.tj.common.log.system.SystemLog)")PublicvoidRepositoryaspect () {} @After ("Controlleraspect ()")PublicvoidDobefore (Joinpoint joinpoint) {Try{HttpServletRequest request =((servletrequestattributes) requestcontextholder.getrequestattributes ()). Getrequest (); String IP =Request.getremoteaddr (); String Description =Getcontrollermethoddescription (Joinpoint); Object obj = Request.getsession (). getattribute ("Loginuser"); LogUser user =New LogUser (NullNull);/*Object obj must have the attribute account, UserName*/Beanutils.copyproperties (user, obj);If(Stringutils.isblank (User.getaccount ())) {user =New LogUser ("Anonymous", "Anonymous user"); } }Catch(Exception e) {}} @SuppressWarnings ("Rawtypes")PrivateStatic String getcontrollermethoddescription (Joinpoint joinpoint)ThrowsException {String TargetName =Joinpoint.gettarget (). GetClass (). GetName (); String MethodName =Joinpoint.getsignature (). GetName (); object[] arguments =Joinpoint.getargs (); Class Targetclass =Class.forName (targetName); Method[] methods = targetclass.getmethods (); String Description = ""; for (Method method:methods) { if (Method.getname (). Equals (MethodName)) {class[] Clazzs = method.get Parametertypes (); if (clazzs.length = = arguments.length) {description = Method.getannotation (systemlog. Class). Description (); break;}} } return description;}}
Step2 Log Records
@CrossOrigin (maxAge = 3600) @RestController @requestmapping (value = "/cxorders")class cxorderresources { @SystemLog (description= "Query order List operation") @RequestMapping (value= "/showdata", method = Public responseentity<string> ShowData ()throwsnew Responseentity<string> ("", Httpstatus.ok); } }
Reference:
http://kld208.iteye.com/blog/1632935
http://www.oschina.net/code/snippet_201779_53788
Spring Unified log Management, facets (@Aspect), annotated log management