IOC源码解析(1)

对于IOC的源码解析,首先便是声明bean对象,可以通过配置文件或者注解的形式。例如:

BeanFactory context=new ClassPathXmlApplicationContext("xxx.xml");

通过ClassPathXmlApplicationContext中了解到,此处调用了其构造方法:
IOC源码解析(1)_第1张图片
定位到具体的有参构造方法:
IOC源码解析(1)_第2张图片
此处的refresh()实现了IOC容器的初始化过程:
IOC源码解析(1)_第3张图片
定位到obtainFreshBeanFactory(),加载Bean的资源文件。

ConfigurableListableBeanFactory beanFactory = this.obtainFreshBeanFactory();

进入obtainFreshBeanFactory中
IOC源码解析(1)_第4张图片
查看refreshBeanFactory()的具体实现:
IOC源码解析(1)_第5张图片

//工厂的创建
DefaultListableBeanFactory beanFactory = this.createBeanFactory();
beanFactory.setSerializationId(this.getId());
this.customizeBeanFactory(beanFactory);
this.loadBeanDefinitions(beanFactory);
this.beanFactory = beanFactory;

进入loadBeanDefinitions(beanFactory)中
IOC源码解析(1)_第6张图片
获取配置资源Resource接着进入loadBeanDefinitions(configLocations)中
IOC源码解析(1)_第7张图片
IOC源码解析(1)_第8张图片
进入this.loadBeanDefinitions(location)中

public int loadBeanDefinitions(String location, @Nullable Set actualResources) throws BeanDefinitionStoreException {
		//资源加载
        ResourceLoader resourceLoader = this.getResourceLoader();
        if (resourceLoader == null) {
            throw new BeanDefinitionStoreException("Cannot load bean definitions from location [" + location + "]: no ResourceLoader available");
        } else {
            int count;
            if (resourceLoader instanceof ResourcePatternResolver) {
                try {
                    Resource[] resources = ((ResourcePatternResolver)resourceLoader).getResources(location);
                    count = this.loadBeanDefinitions(resources);
                    if (actualResources != null) {
                        Collections.addAll(actualResources, resources);
                    }

                    if (this.logger.isTraceEnabled()) {
                        this.logger.trace("Loaded " + count + " bean definitions from location pattern [" + location + "]");
                    }

                    return count;
                } catch (IOException var6) {
                    throw new BeanDefinitionStoreException("Could not resolve bean definition resource pattern [" + location + "]", var6);
                }
            } else {
                Resource resource = resourceLoader.getResource(location);
                //进入此处
                count = this.loadBeanDefinitions((Resource)resource);
                if (actualResources != null) {
                    actualResources.add(resource);
                }

                if (this.logger.isTraceEnabled()) {
                    this.logger.trace("Loaded " + count + " bean definitions from location [" + location + "]");
                }

                return count;
            }
        }
    }

进入this.loadBeanDefinitions((Resource)resource)

public int loadBeanDefinitions(EncodedResource encodedResource) throws BeanDefinitionStoreException {
        Assert.notNull(encodedResource, "EncodedResource must not be null");
        if (this.logger.isTraceEnabled()) {
            this.logger.trace("Loading XML bean definitions from " + encodedResource);
        }

        Set currentResources = (Set)this.resourcesCurrentlyBeingLoaded.get();
        if (!currentResources.add(encodedResource)) {
            throw new BeanDefinitionStoreException("Detected cyclic loading of " + encodedResource + " - check your import definitions!");
        } else {
            int var6;
            
            try {
           		// 输出流处理配置文件
                InputStream inputStream = encodedResource.getResource().getInputStream();
                Throwable var4 = null;

                try {
                    InputSource inputSource = new InputSource(inputStream);
                    if (encodedResource.getEncoding() != null) {
                        inputSource.setEncoding(encodedResource.getEncoding());
                    }
					// 通过BeanDefinitions。加载Bean
                    var6 = this.doLoadBeanDefinitions(inputSource, encodedResource.getResource());
                } catch (Throwable var24) {
                    var4 = var24;
                    throw var24;
                } finally {
                    if (inputStream != null) {
                        if (var4 != null) {
                            try {
                                inputStream.close();
                            } catch (Throwable var23) {
                                var4.addSuppressed(var23);
                            }
                        } else {
                            inputStream.close();
                        }
                    }

                }
            } catch (IOException var26) {
                throw new BeanDefinitionStoreException("IOException parsing XML document from " + encodedResource.getResource(), var26);
            } finally {
                currentResources.remove(encodedResource);
                if (currentResources.isEmpty()) {
                    this.resourcesCurrentlyBeingLoaded.remove();
                }

            }

            return var6;
        }
    }

进入doLoadBeanDefinitions方法,
IOC源码解析(1)_第9张图片
通过registerBeanDefinitions方法解析和注册bean,进入registerBeanDefinitions方法中
IOC源码解析(1)_第10张图片
进入this.getRegistry().getBeanDefinitionCount()中
在这里插入图片描述
进入this.beanFactory.getBeanDefinition(beanName)方法中,会发现

private final Map beanDefinitionMap;

DefaultListableBeanFactory中,bean对象的存储最终停留在map对象中,具体实现为一个ConcurrentHashMap类。

你可能感兴趣的:(spring,java,后端)