SPRINGAOP source of the---agent

Source: Internet
Author: User
Tags addall

Overview

1. Enhanced Builds

2. Acquisition of agents

As you can see from the previous chapter, after obtaining the enhancement, you can create an agent through Createproxy, the source code is as follows:

protectedObject createproxy (Class<?>Beanclass, String beanname, object[] specificinterceptors, Targetsource targetsource) {proxyfactory proxyfactory=Newproxyfactory (); Proxyfactory.copyfrom ( This);//Gets the properties of the current classif(!Proxyfactory.isproxytargetclass ()) {//Set Proxytargetclass property to True use Cglib Proxy, false using Jdkdynamicproxy modeif(Shouldproxytargetclass (Beanclass, Beanname)) {Proxyfactory.setproxytargetclass (true); }            Else{evaluateproxyinterfaces (Beanclass, proxyfactory); }} advisor[] Advisors=buildadvisors (Beanname, specificinterceptors);//Create Facets for(Advisor Advisor:advisors) {proxyfactory.addadvisor (advisor);//proxyfactory in which the slice to be created is added} Proxyfactory.settargetsource (Targetsource);        Set the class Customizeproxyfactory (proxyfactory) to be proxied; Proxyfactory.setfrozen ( This. Freezeproxy); if(advisorsprefiltered ()) {proxyfactory.setprefiltered (true); }        returnProxyfactory.getproxy (Getproxyclassloader ());//Get Agent}
protectedadvisor[] Buildadvisors (String beanname, object[] specificinterceptors) {//Handle prototypes correctly ...advisor[] Commoninterceptors =Resolveinterceptornames (); List<Object> allinterceptors =NewArraylist<object>(); if(Specificinterceptors! =NULL) {//Join Interceptor Allinterceptors.addall (Arrays.aslist (specificinterceptors)); if(Commoninterceptors! =NULL) {                if( This. Applycommoninterceptorsfirst) {Allinterceptors.addall (0, Arrays.aslist (commoninterceptors)); }                Else{Allinterceptors.addall (arrays.aslist (commoninterceptors)); }            }        }        if(logger.isdebugenabled ()) {intNrofcommoninterceptors = (commoninterceptors! =NULL? commoninterceptors.length:0); intNrofspecificinterceptors = (specificinterceptors! =NULL? specificinterceptors.length:0); Logger.debug ("Creating implicit proxy for beans '" + beanname + "' with" + nrofcommoninterceptors + "Common Interce Ptors and "+ nrofspecificinterceptors +" specific interceptors "); } advisor[] Advisors=Newadvisor[allinterceptors.size ()];  for(inti = 0; I < allinterceptors.size (); i++) { advisors[i] =   This . Advisoradapterregistry.wrap (Allinterceptors.get (i));//Interceptor for encapsulation conversion to advisor }returnAdvisors; }
 PublicAdvisor Wrap (Object adviceobject)throwsunknownadvicetypeexception {if(AdviceobjectinstanceofAdvisor) {//If the object being encapsulated is itself an advisor, no processing is requiredreturn(Advisor) adviceobject; }        if(! (AdviceobjectinstanceofAdvice)) {//This encapsulation method is only valid for advice and advisorThrow Newunknownadvicetypeexception (Adviceobject); } Advice Advice=(Advice) adviceobject; if(Adviceinstanceofmethodinterceptor) {//If it is a method interceptor, use the Defaultpointcutadvisor package//So well-known it doesn ' t even need an adapter.            return Newdefaultpointcutadvisor (advice); }         for(Advisoradapter adapter: This. Adapters) {//If there is an adapter for advisor, you also need to encapsulate//Check The It is supported.            if(Adapter.supportsadvice (advice)) {return Newdefaultpointcutadvisor (advice); }        }        Throw Newunknownadvicetypeexception (advice); }

Due to the fact that there are too many interceptors, notifier, enhancement methods in Spring to enhance the logic, it is necessary to uniformly encapsulate the Advisor to create the agent and complete the enhanced encapsulation process.

The next step is the process of creating the agent.

SPRINGAOP source of the---agent

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.