Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand Java Spring three-level cache and circular dependency

2025-02-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Today, I will talk to you about how to understand Java Spring three-level cache and circular dependency, which may not be well understood by many people. In order to make you understand better, the editor has summarized the following for you. I hope you can get something from this article.

First, what is circular dependency? What is a three-tier cache?

[what is circular dependency] what is circular dependency is easy to understand, when it appears in our code, such as dependency injection into BeanB class in BeanA class, and dependency injection into class An in BeanB class, after instantiating An in the process of IOC, we find that we can't directly initbeanA object, we need to inject B object, and we find that there is no B object in the object pool. Create an instantiation of the B object by building the function. And because B object needs to be injected into An object, it is found that there is no An object in the object pool, so it will nest dolls.

[three-level cache] three-level cache is actually three Map objects, starting from the order in which the objects are stored

The three-level cache singletonFactories holds the ObjectFactory, passing in an anonymous inner class, and the ObjectFactory.getObject () method eventually calls getEarlyBeanReference () for processing, returning the lambda expression that creates the bean instantiation.

The second-level cache earlySingletonObjects stores bean, saves the semi-finished bean instance, and saves the instance beanProxy of proxy bean when the object needs to be replaced by AOP.

First-tier cache (singleton pool) singletonObjects stores complete bean instances

/ * Cache of singleton objects: bean name to bean instance. * / private final Map singletonObjects = new ConcurrentHashMap; / * * Cache of early singleton objects: bean name to bean instance. * / private final Map earlySingletonObjects = new HashMap (16); / * * Cache of singleton factories: bean name to ObjectFactory. * / private final Map singletonFactory = this.singletonFactories.get (beanName); if (singletonFactory! = null) {/ / will eventually call the incoming anonymous inner class getEarlyBeanReference () method, which will generate a new proxy object singletonObject = singletonFactory.getObject () This.earlySingletonObjects.put (beanName, singletonObject); this.singletonFactories.remove (beanName) Return singletonObject;} protected Object getEarlyBeanReference (String beanName, RootBeanDefinition mbd, Object bean) {Object exposedObject = bean If (! mbd.isSynthetic () & & hasInstantiationAwareBeanPostProcessors ()) {for (BeanPostProcessor bp: getBeanPostProcessors ()) {if (bp instanceof SmartInstantiationAwareBeanPostProcessor) {SmartInstantiationAwareBeanPostProcessor ibp = (SmartInstantiationAwareBeanPostProcessor) bp; exposedObject = ibp.getEarlyBeanReference (exposedObject, beanName) } return exposedObject;} after reading the above, do you have any further understanding of how to understand Java Spring three-level caching and circular dependencies? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report