第三章 spring-bean之DefaultListableBeanFactory(7)
< 返回列表时间: 2018-08-04来源:OSCHINA
前言 DefaultListableBeanFactory是beanFactory体系里面最后一个子类,也是唯一的操作类,唯一的实现。DefaultListableBeanFactory继承了AbstractAutowireCapableBeanFactory,实现了ConfigurableListableBeanFactory接口。
解读变量
allowBeanDefinitionOverriding变量 allowBeanDefinitionOverriding是一个坑,DefaultListableBeanFactory重写了父类SimpleAliasRegistry的allowAliasOverriding方法。重写的allowAliasOverriding方法就是使用的allowBeanDefinitionOverriding。如果要修改allowBeanDefinitionOverriding变量,需要深入的考虑SimpleAliasRegistry的行为。 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."); } } ...... }
解读 修改allowBeanDefinitionOverriding,注意SimpleAliasRegistry的行为 allowBeanDefinitionOverriding的作用是是否允许一个name有多个BeanDefinition
dependencyComparator变量 private Comparator<Object> dependencyComparator;
解读 细节请看 第二章 一节spring-core之comparator深入解读
resolvableDependencies 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); } }
AbstractApplicationContext 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); }
解读 resolvableDependencies用来保存spring中自己的bean。这样隔离系统bean与业务bean,好像spring,没有做得彻底。
beanDefinitionMap属性 private final Map<String, BeanDefinition> beanDefinitionMap = new ConcurrentHashMap<String, BeanDefinition>(256); 保存BeanDefinition的实现类,
allBeanNamesByType属性 与 singletonBeanNamesByType变量 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类型的所有beanName
beanDefinitionNames与manualSingletonNames变量 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
热门排行