DefaultListableBeanFactory是beanFactory體系裏面最後一個子類,也是惟一的操做類,惟一的實現。DefaultListableBeanFactory繼承了AbstractAutowireCapableBeanFactory,實現了ConfigurableListableBeanFactory接口。java
allowBeanDefinitionOverriding是一個坑,DefaultListableBeanFactory重寫了父類SimpleAliasRegistry的allowAliasOverriding方法。重寫的allowAliasOverriding方法就是使用的allowBeanDefinitionOverriding。若是要修改allowBeanDefinitionOverriding變量,須要深刻的考慮SimpleAliasRegistry的行爲。spring
private boolean allowBeanDefinitionOverriding = true; public boolean isAllowBeanDefinitionOverriding() { return this.allowBeanDefinitionOverriding; } protected boolean allowAliasOverriding() { return isAllowBeanDefinitionOverriding(); } public void registerBeanDefinition(String beanName, BeanDefinition beanDefinition) throws BeanDefinitionStoreException { oldBeanDefinition = this.beanDefinitionMap.get(beanName); if (oldBeanDefinition != null) { if (!isAllowBeanDefinitionOverriding()) { throw new BeanDefinitionStoreException(beanDefinition.getResourceDescription(), beanName, "Cannot register bean definition [" + beanDefinition + "] for bean '" + beanName + "': There is already [" + oldBeanDefinition + "] bound."); } } ...... }
解讀緩存
private Comparator<Object> dependencyComparator;
解讀this
細節請看第二章 一節spring-core之comparator深刻解讀 .net
private final Map<Class<?>, Object> resolvableDependencies = new ConcurrentHashMap<Class<?>, Object>(16); public void registerResolvableDependency(Class<?> dependencyType, Object autowiredValue) { Assert.notNull(dependencyType, "Dependency type must not be null"); if (autowiredValue != null) { if (!(autowiredValue instanceof ObjectFactory || dependencyType.isInstance(autowiredValue))) { throw new IllegalArgumentException("Value [" + autowiredValue +"] does not implement specified dependency type [" + dependencyType.getName() + "]"); } this.resolvableDependencies.put(dependencyType, autowiredValue); } }
AbstractApplicationContextcode
protected void prepareBeanFactory(ConfigurableListableBeanFactory beanFactory) { // BeanFactory interface not registered as resolvable type in a plain factory. // MessageSource registered (and found for autowiring) as a bean. beanFactory.registerResolvableDependency(BeanFactory.class, beanFactory); beanFactory.registerResolvableDependency(ResourceLoader.class, this); beanFactory.registerResolvableDependency(ApplicationEventPublisher.class, this); beanFactory.registerResolvableDependency(ApplicationContext.class, this); }
解讀blog
resolvableDependencies用來保存spring中本身的bean。這樣隔離系統bean與業務bean,好像spring,沒有作得完全。繼承
private final Map<String, BeanDefinition> beanDefinitionMap = new ConcurrentHashMap<String, BeanDefinition>(256);
保存BeanDefinition的實現類,接口
private final Map<Class<?>, String[]> allBeanNamesByType = new ConcurrentHashMap<Class<?>, String[]>(64); private final Map<Class<?>, String[]> singletonBeanNamesByType = new ConcurrentHashMap<Class<?>, String[]>(64); public String[] getBeanNamesForType(Class<?> type, boolean includeNonSingletons, boolean allowEagerInit) { if (!isConfigurationFrozen() || type == null || !allowEagerInit) { return doGetBeanNamesForType(ResolvableType.forRawClass(type), includeNonSingletons, allowEagerInit); } Map<Class<?>, String[]> cache = (includeNonSingletons ? this.allBeanNamesByType : this.singletonBeanNamesByType); String[] resolvedBeanNames = cache.get(type); if (resolvedBeanNames != null) { return resolvedBeanNames; } resolvedBeanNames = doGetBeanNamesForType(ResolvableType.forRawClass(type), includeNonSingletons, true); if (ClassUtils.isCacheSafe(type, getBeanClassLoader())) { cache.put(type, resolvedBeanNames); } return resolvedBeanNames; }
當getBeanNamesForType方法被調用的時候,會把結果緩存到allBeanNamesByType或singletonBeanNamesByType裏面。getBeanNamesForType參數type對應class類型的全部beanNameip
private volatile List<String> beanDefinitionNames = new ArrayList<String>(256); private volatile Set<String> manualSingletonNames = new LinkedHashSet<String>(16); public void registerBeanDefinition(String beanName, BeanDefinition beanDefinition) throws BeanDefinitionStoreException { if (hasBeanCreationStarted()) { // Cannot modify startup-time collection elements anymore (for stable iteration) synchronized (this.beanDefinitionMap) { this.beanDefinitionMap.put(beanName, beanDefinition); List<String> updatedDefinitions = new ArrayList<String>(this.beanDefinitionNames.size() + 1); updatedDefinitions.addAll(this.beanDefinitionNames); updatedDefinitions.add(beanName); this.beanDefinitionNames = updatedDefinitions; if (this.manualSingletonNames.contains(beanName)) {// beanDefinitionNames 存在的beanName,就不能在manualSingletonNames存在 Set<String> updatedSingletons = new LinkedHashSet<String>(this.manualSingletonNames); updatedSingletons.remove(beanName); this.manualSingletonNames = updatedSingletons; } } } } public void registerSingleton(String beanName, Object singletonObject) throws IllegalStateException { super.registerSingleton(beanName, singletonObject); if (hasBeanCreationStarted()) { // Cannot modify startup-time collection elements anymore (for stable iteration) synchronized (this.beanDefinitionMap) { if (!this.beanDefinitionMap.containsKey(beanName)) { Set<String> updatedSingletons = new LinkedHashSet<String>(this.manualSingletonNames.size() + 1); updatedSingletons.addAll(this.manualSingletonNames); updatedSingletons.add(beanName); this.manualSingletonNames = updatedSingletons; } } } else { // Still in startup registration phase if (!this.beanDefinitionMap.containsKey(beanName)) { this.manualSingletonNames.add(beanName); } } clearByTypeCache(); }
beanDefinitionNames保存全部BeanDefinition的名字,manualSingletonNames保存了全部singleton的BeanDefinitionName。注意beanDefinitionNames聲明瞭 volatile