springAOP proxy implementation

Continue to the previous article

 

I traced the code and found the proxy class registered when springAOP started

The class AnnotationAwareAspectJAutoProxyCreator is a container for processing aspect xml configuration spingaop or annotation creation proxy class.

Its parent class AbstractAutoProxyCreator executes according to the configured notification type

postProcessBeforeInstantiation postProcessAfterInitialization 方法

 

Then call the wrapIfNecessary method to store the method that needs the aspect in map  advisedBeans

 

 

/**
* Wrap the given bean if necessary, i.e. if it is eligible for being proxied.
* @param bean the raw bean instance
* @param beanName the name of the bean
* @param cacheKey the cache key for metadata access
* @return a proxy wrapping the bean, or the raw bean instance as-is
*/
protected Object wrapIfNecessary(Object bean, String beanName, Object cacheKey) {
if (beanName != null && this.targetSourcedBeans.containsKey(beanName)) {
returnbean;
}
if (Boolean.FALSE.equals(this.advisedBeans.get(cacheKey))) {
returnbean;
}
if (isInfrastructureClass(bean.getClass()) || shouldSkip(bean.getClass(), beanName)) {
this.advisedBeans.put(cacheKey, Boolean.FALSE);
returnbean;
}
 
// Create proxy if we have advice.
Object[] specificInterceptors = getAdvicesAndAdvisorsForBean(bean.getClass(), beanName, null);
if (specificInterceptors != DO_NOT_PROXY) {
this.advisedBeans.put(cacheKey, Boolean.TRUE);
Object proxy = createProxy(bean.getClass(), beanName, specificInterceptors, new SingletonTargetSource(bean));
this.proxyTypes.put(cacheKey, proxy.getClass());
returnproxy;
}
 
this.advisedBeans.put(cacheKey, Boolean.FALSE);
returnbean;
 
}
 

 

 

 

in

Object proxy = createProxy(bean.getClass(), beanNamespecificInterceptorsnew SingletonTargetSource(bean));

Proxy the object to be handled by the aspect by proxy

The proxy method is divided into the proxy of the JDK original sound, and the cglib

JDK: the object being proxied implements the interface

cglib: proxied object has no interface implementation

 

So far, the springaop proxy object has been successful,

When the method of the pointcut is called, the method of the proxy object is reflected,

Then perform the front and rear operations of the proxy object according to the type of notification

Call the JdkDynamicAopProxy class in

public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

。。。

method

 

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327056781&siteId=291194637