Creating a proxy object

Following up on the previous article, the Advice notification method in the shouldSkip method has generated and cached the Advisor object

Then call getAdvicesAndAdvisorsForBean method to obtain the current interceptor Bean (i.e., Advice notice), this method first from the cache access to have cached List < Advisor >, This method first fetches all the methods of the current class (including the parent class) with reflection and then calls Matches to retrieve the Pointcut expression from the Advisor. Return true whenever one of the methods meets the criteria, indicating that the proxy object needs to be created

Back in the wrapIfNecessary method of AbstractAutoProxyCreator, createProxy is called when specificInterceptors are not null indicating that a proxy needs to be created

Again, buildAdvisors is called to wrap and inspect some of the default interceptors

Then we go back to the createProxy method abstractautoXyCreator. In the last step, we call ProxyFactory.getProxy (getProxyClassLoader()); To create a proxy object

When we use AOP, we find that proxyTargetClass always returns true, This value is derived from proxyFactory.copyFrom(this) in the createProxy method abstractautoXyCreator; This method, the current is AnnotationAwareAspectJAutoProxyCreator this object

We then decide to return JDK dynamic proxies if our class is an interface or a ProxyClass, and CGLIB dynamic proxies if it is not

To the back to AbstractAutowireCapableBeanFactory doCreateBean initializeBean method call returns the proxy objects

Let’s go back to the AbstractFactory doGetBean method and look at this logic again

How is the proxy object problem solved when dealing with circular dependencies

For example, A depends on B, and B depends on A; At the same time, we defined the AOP logic to include methods into A and B that need to be proxied. As mentioned earlier, we first created early instances of A by reflection and then put them into singletonFactories, Then, when populateBean is called for dependency injection, it is found that A depends on B, and the instantiation of B should be started. The getSingleton method in the AbstractBeanFactory doGetBean does the trick and it creates an instance of A from singletonFactories, and that’s the proxy object, Let’s see how it does that

The first is an ObjectFactory implementation that you put in when you create singletonFactories

So we’re back in the familiar and we’re back in the method that wrapIfNecessary talked about above. Finally, since A calls the initialization method to operate on the proxy object, A complete instance of the Bean is created