The difference between args and arg-names in Spring AOP

Source: Internet
Author: User

These two days in looking at the various syntax of AOP ASPECTJ, found that there are two concepts of args and arg-names is easy to confuse, the Internet is basically not clear, so try it out, find it or try it better understanding

First, the conclusion:

Args is used with execution to filter the method to be proxied, if not with arg-names, then the usage is the args (class name, class name ...). )。 If and arg-names (parameter name 1, parameter name 2 ...) Together, the usage is args (parameter 1, parameter 2 ...), where the type of parameter 1 and parameter 2 is determined by the parameters of the method represented by Arg-names

Arg-names is used in conjunction with the Proxy method (that is, the method you want to add before or after the proxy method) arg-names (parameter name 1, parameter name 2 ...) where the parameter name 1 parameter name 2 and the proxy method's parameter name is one by one corresponding

As for @args, it is used for specifying annotations on parameters, but rather better to distinguish

As an example,

First define a class to be proxied:

public interface someinterface{public void Targetfunction (String St, Integer in); public class target implements someinterface{public    void Targetfunction (String St, Integer in) {
System.out.println ("I am a proxy class");
}}

  

Then you define the proxy class.

public class aspect{public   void Aspectfunction (Integer in, String St) {
System.out.println ("I am the proxy class, there are two parameters:" + st + "" + in);

}

  

Then define the XML file:

<bean id= "Targetclass" class= "target"/><bean id= "Aspectclass" class= "aspect"/><aop:config>        <aop:aspect id = "ASP1" ref= "Aspectclass" >        <aop:after pointcut= "Execution (* *.targetfunction. (..) and Args (String,integer)) "method=" Aspectfunction "/>    </aop:aspect>        <aop:aspect id =" ASP2 "ref=" Aspectclass ">        <aop:after pointcut=" Execution (* *.targetfunction. (..) and args (st,in)) "Method=" Aspectfunction "arg-names=" St,in "/>    </aop:aspect></aop:config>

  

As shown in the XML file, arg is defined in Pointcut, and arg-names is defined in the cut-in position after.

Args is used with execution to filter the method to be proxied, if not with arg-names, then the usage is the args (class name, class name ...). )。 If and arg-names (parameter name 1, parameter name 2 ...) Together, the usage is args (parameter 1, parameter 2 ...), where the type of parameter 1 and parameter 2 is determined by the parameters of the method represented by Arg-names

Arg-names is used in conjunction with the Proxy method (that is, the method you want to add before or after the proxy method) arg-names (parameter name 1, parameter name 2 ...) where the parameter name 1 parameter name 2 and the proxy method's parameter name is one by one corresponding

The difference between args and arg-names in Spring AOP

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.