spring source to solve circular references idea

Development process, B, similar to the case of A-> B-> A often exists; see the spring source DefaultSingletonBeanRegistry

getSingleton(String beanName, boolean allowEarlyReference)方法:
/ ** 
	 * Process thinking spring circular references: references allowed to perform singleton object currently created in advance 
	 * attributes inject singleton object references in advance of this time has not been completed, so you can solve the problem of circular references 
	 * Return the (raw ) Singleton Object Registered an under the GIVEN name. 
	 * <P> checks the already instantiated singletons and Also android.permission for AN early 
	 * Reference to a Currently Created Singleton (Resolving a Circular Reference). 
	 * check the instantiated singleton object and allows advance the current reference implementation of the singleton created (to resolve circular references). 
	 The name of the beanName @param * The look for the bean to 
	 * @param allowEarlyReference Early References Whether or Not Created Should BE 
	 * Registered @return The Object Singleton, IF} or {@code null none found 
	 * / 
	@Nullable 
		// singleton object isSingletonCurrentlyInCreation determine whether the corresponding creation 
	protected Object getSingleton (String beanName, boolean allowEarlyReference) {
		// start Singleton cache to find, did not find the will first determine whether the bean is being created 
		Object singletonObject = the this. SingletonObjects .get (beanName); 
		IF (singletonObject == null && isSingletonCurrentlyInCreation (beanName)) { 
			the synchronized (the this. singletonObjects) { 
				// earlySingletonObjects stored in advance all single exposure embodiments, try to find from the earlySingletonObjects 
				singletonObject = the this. earlySingletonObjects .get (the beanName); 
				IF (&& allowEarlyReference singletonObject == null) { 
					// if allowed early dependency, can try found in the corresponding singleton singletonFactories plant 
					the ObjectFactory the this singletonFactory = <?>. singletonFactories .get (the beanName); 
					IF (! singletonFactory = null) { 
						// create a bean, and early exposure of the cache bean, attribute injection is not yet the bean, used to resolve circular dependencies 
						singletonObject singletonFactory.getObject = ();
						this.earlySingletonObjects.put(beanName, singletonObject);
						this.singletonFactories.remove(beanName);
					}
				}
			}
		}
		return singletonObject;
	}

  

Description:

  

	/ ** Singleton Objects of the Cache:. The bean to the bean instance name * / 
	/ * ** cache single embodiment the bean / 
	Private Final the Map <String, Object> singletonObjects = new new of ConcurrentHashMap <> (256); 

	/ ** Singleton of the Cache Factories .: bean name to the ObjectFactory * / 
	/ ** Singleton factory cache * / 
	Private Final the Map <String, the ObjectFactory <= >> singletonFactories new new HashMap <> (16);? 

	/ ** Singleton cache of Early Objects: bean name to . bean instance * / 
	/ ** cache early exposure singleton bean i.e. not yet completed injection property * bean / 
	Private Final the Map <String, Object> = new new earlySingletonObjects the HashMap <> (16); 

	/ ** Registered singletons of the Set , containing at The bean names in the Order Registration. * / 
	Private Final the Set <String>registeredSingletons = new LinkedHashSet<>(256);

 

getObject ObjectFactory interface () method:

public interface ObjectFactory<T> {

	/**
	 * 此方法返回被bean工厂管理的bean的实例
	 * Return an instance (possibly shared or independent)
	 * of the object managed by this factory.
	 * @return the resulting instance
	 * @throws BeansException in case of creation errors
	 */
	T getObject() throws BeansException;

}

  

  

 

 

Recycling solutions references:

    1 using @Autowired annotation, injection time determined by the object attribute spring, a reference to the object A is exposed, when the object B requires the injection;

    2 setter injection method based on attribute B

Guess you like

Origin www.cnblogs.com/wl20200316/p/12516384.html