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