Spring Aop (16)--Programmatic custom Advisor

Source: Internet
Author: User
Tags aop
custom Advisor with programmatic style Overview

In most cases, our AOP applications can be made through the Spring AOP configuration (either annotation-based or XML-configured). The core of Spring AOP is the Getadvice () method, which is temporarily useful in the Advisor,advisor interface, and the Isperinstance () method is officially not applied yet, The resulting advisor is a singleton or multiple cases not determined by the return result of Isperinstance (), but is controlled by itself when defining the bean.

Public interface Advisor {

    Advice getadvice ();

    Boolean isperinstance ();

}

When we use the advisor, we do not implement the Advisor interface directly, but implement the sub-interface of the Advisor interface, Pointcutadvisor or Introductionadvisor. Introductionadvisor personal feeling is not very useful, we previously introduced the @declareparents and <aop:declare-parents/> belong to introductionadvisor use, They correspond to the declareparentsadvisor. Most of the rest of the application is pointcutadvisor. The Pointcutadvisor interface is defined as follows.

Public interface Pointcutadvisor extends Advisor {

    Pointcut getpointcut ();

}

We can see that it adds a getpointcut () method based on the Advisor interface to specify which pointcut our advisor needs to apply, that is, which method calls. The pointcut definition of programming has been introduced before, it does not belong to the scope of this article, here is no longer mentioned, the reader is not very familiar with the proposal from the beginning, the author of the blog is a series of blog posts, of course, you can temporarily skip the first, directly see the author of the following examples. Implementing a custom advisor

The following is a custom advisor implemented by the author, is the implementation of the Pointcutadvisor interface. The application of the advice is the implementation of the Methodbeforeadvice, and the pointcut of the application simply matches the method call of the method named find for all classes.

public class Myadvisor implements Pointcutadvisor {@Override public Advice Getadvice () {return new Meth Odbeforeadvice () {@Override public void before (method, object[] args, Object target) thro WS Throwable {System.out.println ("Beforeadvice implementation, called before the target method is invoked, is:" + method.getdeclaringclass (). GetName
                        () + "."
            + Method.getname ());
    }
        };
    } @Override public Boolean isperinstance () {return true;
         } @Override Public Pointcut getpointcut () {/** * simple Pointcut definition, matching the Find method call of all classes.
                */return new Pointcut () {@Override public classfilter getclassfilter () {
            return classfilter.true; } @Override Public Methodmatcher Getmethodmatcher () {return new Methodmatcher () {@Override public boolean MatcheS (method, class<?> targetclass) {String methodName = Method.getname ();
                        if ("Find". Equals (MethodName)) {return true;
                    } return false;
                    } @Override public Boolean isruntime () {return false; } @Override public Boolean matches (method, CLASS&LT;?&G T
                    Targetclass, object[] args) {return false;
            }

                };
    }

        }; }

}
Configure the use of a custom advisor

How should we apply it after we have a custom advisor? This distinguishes several situations.
-If you are using proxyfactory by programming, or applying proxycreatorsupport to create proxy objects, we pass Advisedsupport.addadvisor (advisor advisor) To apply our custom advisor. There are proxycreatorsupport in Advisedsupport subclasses.
-If <aop:aspectj-autoproxy/> or <AOP:CONFIG> has been applied to our project, then the advisor Bean that we define in the bean container is automatically applied to the matching bean. This is described in detail in the article "Automatic creation of proxy objects in Spring AOP principles".
-If no <aop:aspectj-autoproxy/> or <AOP:CONFIG> is applied to the project, we need to define Beannameautoproxycreator, Defaultadvisorautoproxycreator and other abstractadvisorautoproxycreator types of beans. Or a bean that defines a annotationawareaspectjautoproxycreator or aspectjawareadvisorautoproxycreator type, in fact <AOP: Aspectj-autoproxy/> is the automatic definition of the Annotationawareaspectjautoproxycreator type bean,<aop:config> is to automatically define a bean of the aspectjawareadvisorautoproxycreator type. This will look for a matching advisor to create the corresponding proxy object after the bean is created. These are detailed in the article "Automatic proxy object creation in Spring AOP principle", details are not mentioned here.

(Note: This article was written based on Spring4.1.0, written on May 16, 2017)

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.