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