AspectJ section to get method parameters and get specific values

Source: Internet
Author: User
Tags object model reflection

Project requirements: To implement slices through aspectj and to obtain specific parameter values and types

Objective: To obtain the values shared by different parameter objects in different methods in the background

Implementation principle: Use ASPECTJ to intercept and then get the object parameter and then take the value by reflection


Here is the implementation method:

The first is to define the AspectJ file, which is the. aj file

Public aspect UPDATESERVICEASPECTJ {public
    pointcut updateServices1 (Object model)://This model is the same
    as the parameter name Execution (Public * *.*.*.*controller.* (..)) && @annotation (customupdateservices) && args (model); Define use this parameter model after

    (Object O) returning (): UpdateServices1 (o) {//Post notification
        reflectclass.reflect (o); The parameter
	System.out.println (REFLECTCLASS.XQID) is obtained by reflection method.	Parameters common to OBJ
    }
@CustomUpdateServices//This is my custom. No internal parameters are used to intercept and find a way to do this.

The simple thing is to use the slice to get the argument of the worthy words, need to use the cache, but the cache configuration is cumbersome and generally not used

And the simple annotations reflect that we're not getting the parameters, so we need to use the slice and then the proxy gets the parameters and then the values of the parameters are obtained by reflection.

We can make a slice by ASPECTJ.

What we're going to notice here is that the configuration interceptor is after our parameter name to be sent with the exact same parameter name as the Intercept.

Agent parameters as long as the types are the same

And then it's our reflection that gets the values common to the object.

Reflecclass

Package com.hnmzhc.web.commons.updateService;
Import Org.slf4j.Logger;

Import Org.slf4j.LoggerFactory;

Import Java.lang.reflect.Field;
 /** * Created by DAVIDG on 2016/11/8. * @XQID Cell ID * @USERID User ID * * Public class Reflectclass {public static final Logger Logger = Loggerfactory.getlo

    Gger (Reflectclass.class);
    public static Integer xqid = 0;
    public static Integer Wyid = 0;


    public static Integer USERID = 0; /** Incoming parameter * Gets all the properties of the current class by reflection * to determine if there are xqid and UserID * If there is, deposit in the static properties of the current class * @param obj/public Stati
        c void reflect (Object obj) {if (obj = null) return;
        field[] fields = Obj.getclass (). Getdeclaredfields ();
            for (int j = 0; J < Fields.length; J + +) {fields[j].setaccessible (true); try {//Field name if (Fields[j].getname (). Equals ("Xqid")) {if (Fields[j].get) ( OBJ) {//!=null xqid = (Integer) fields[j].get (obj);//Assignment
                    }}else if (Fields[j].getname (). Equals ("UserId")) {if (fields[j].ge
                    T (obj)!=null) {USERID = (Integer) fields[j].get (obj);
                        }else if (Fields[j].getname (). Equals ("Wyid")) {if (Fields[j].get (obj)!=null) {
                    Wyid = (Integer) fields[j].get (obj); } Logger.warn (String.Format ("xqid =%n, Wyid =%n, USERID =%n", xqid, Wyid,userid));
            Print log} catch (Exception e) {e.printstacktrace ();


 }

        }
    }
}
So we can do a slice to get the function of the method parameter.

Of course, if your parameter types are the same here, it'll be easier.

Just replace the type of object in the ASPECTJ with your own pojo/bean.

Be sure to note the use of names and args ()

Then the choice of the specific implementation method





Thanks to the knowledge shared by the older Daniel on the Internet, thank you


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.