Spring AOP Gets the parameter name and parameter value of the Intercept method

Source: Internet
Author: User
Tags aop throwable

Spring AOP Gets the parameter name and parameter value of the Intercept method

Note: This approach requires JDK1.8 version support

Begin:

1.AOP configuration:<aop:aspectj-autoproxy expose-proxy="true" />

  • Note This configuration needs to be configured in the Spring MVC configuration file because the controller layer method needs to be intercepted
  • Or, in the case of a spring configuration file that must be configured, and the method of intercepting the controller layer, a packet scan of the controller layer can be added to the spring configuration file

2. Specific code:

Import Org.apache.commons.lang3.arrayutils;import Org.aspectj.lang.proceedingjoinpoint;import Org.aspectj.lang.signature;import Org.aspectj.lang.annotation.around;import Org.aspectj.lang.annotation.Aspect; Import Org.aspectj.lang.reflect.methodsignature;import org.slf4j.logger;import org.slf4j.loggerfactory;/** * Request parameter Intercept Check */@Aspectpublic class Aopparamverify {private static Logger log = Loggerfactory.getlogger (Aopparamverify.clas    s); @Around (value = "Execution (* Com.website.controller):    *.*(..))")        Public Object Invoke (Proceedingjoinpoint joinpoint) {/** * timestamp check, more than one minute, intercept *///return result encapsulation class        Hzlqswreqresult rst = new Hzlqswreqresult ();        1. Get the array of all parameter values here object[] args = Joinpoint.getargs ();        Signature Signature = Joinpoint.getsignature ();        Methodsignature methodsignature = (methodsignature) signature;        2. The most critical step: through this gets the string array of all parameter names to the method string[] Parameternames = Methodsignature.getparameternames ();  try {          3. Obtain the corresponding value by the subscript of the parameter name you need to get int timestampindex = Arrayutils.indexof (Parameternames, "TimeStamp");                if (timestampindex! =-1) {Long TimeStamp = (long) args[timestampindex]; if (System.currenttimemillis ()-timeStamp > 60000) {rst.setresultcode (rst.resultcode_hashcode_over                    Due);                    Rst.setreturnmsg ("Time stamp expires");                    Rst.setreturnobject ("Time stamp expires");                return rst;        }} return Joinpoint.proceed ();            } catch (Throwable throwable) {log.error ("timestamp check exception");            Throwable.printstacktrace ();            Rst.setresultcode (Rst.resultcode_error);            Rst.setreturnmsg ("AOP check Exception");            Rst.setreturnobject ("AOP check Exception");        return rst; }    }}
3. Effect:

Spring AOP Gets the parameter name and parameter value of the Intercept method

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.