Spring Unified log Management, facets (@Aspect), annotated log management

Source: Internet
Author: User
Tags aop

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); } }




Spring Unified log Management, facets (@Aspect), annotated log management

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.