跟我学spring源码(一)

拉取源代码

git clone https://github.com/goldGreatChen/spring-framework.git

其中gradle需要4版本
看它的import to idea文档
Precompile spring-oxm with ./gradlew :spring-oxm:compileTestJava
对spring-oxm项目预先编译 也可以使用gradle :spring-oxm:compileTestJava命令

Import into IntelliJ (File -> New -> Project from Existing Sources -> Navigate to directory -> Select build.gradle)
导入idea等待完成 很慢

When prompted exclude the spring-aspects module (or after the import via File-> Project Structure -> Modules)
排除spring-aspects项目 如下图所示

跟我学spring源码(一)_第1张图片
1508828148(1).png

在其中新建自己的项目spring-demo

group 'org.springframework'
version '5.0.1.BUILD-SNAPSHOT'

apply plugin: 'java'
apply plugin: "groovy"

sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

dependencies {
    compile(project(":spring-context"))
    testCompile group: 'junit', name: 'junit', version: '4.12'
}
package com.chen;

import com.chen.service.ChenService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class App {
    public static void main(String[] args) {
        ApplicationContext context=new  ClassPathXmlApplicationContext("application.xml");
        ChenService chenService=context.getBean("chenService", ChenService.class);
        System.out.println(chenService.getInfo());
    }
}

成功运行 说明源代码已经编译成功
ClassPathXmlApplicationContext("application.xml");
通过这句话看spring启动过程

    public ClassPathXmlApplicationContext(
            String[] configLocations, boolean refresh, @Nullable ApplicationContext parent)
            throws BeansException {

        super(parent);
        //configlocation 就是application.xml的路径
        setConfigLocations(configLocations);
        if (refresh) {
            refresh();
        }
    }

看refresh

@Override
    public void refresh() throws BeansException, IllegalStateException {
        synchronized (this.startupShutdownMonitor) {
            // Prepare this context for refreshing.
            prepareRefresh();

            // Tell the subclass to refresh the internal bean factory.
            ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();

            // Prepare the bean factory for use in this context.
            prepareBeanFactory(beanFactory);

            try {
                // Allows post-processing of the bean factory in context subclasses.
                postProcessBeanFactory(beanFactory);

                // Invoke factory processors registered as beans in the context.
                invokeBeanFactoryPostProcessors(beanFactory);

                // Register bean processors that intercept bean creation.
                registerBeanPostProcessors(beanFactory);

                // Initialize message source for this context.
                initMessageSource();

                // Initialize event multicaster for this context.
                initApplicationEventMulticaster();

                // Initialize other special beans in specific context subclasses.
                onRefresh();

                // Check for listener beans and register them.
                registerListeners();

                // Instantiate all remaining (non-lazy-init) singletons.
                finishBeanFactoryInitialization(beanFactory);

                // Last step: publish corresponding event.
                finishRefresh();
            }

            catch (BeansException ex) {
                if (logger.isWarnEnabled()) {
                    logger.warn("Exception encountered during context initialization - " +
                            "cancelling refresh attempt: " + ex);
                }

                // Destroy already created singletons to avoid dangling resources.
                destroyBeans();

                // Reset 'active' flag.
                cancelRefresh(ex);

                // Propagate exception to caller.
                throw ex;
            }

            finally {
                // Reset common introspection caches in Spring's core, since we
                // might not ever need metadata for singleton beans anymore...
                resetCommonCaches();
            }
        }
    }

判断是不是有beanFactory了 如果已经有了 那么销毁所有bean 关闭beanFactory 然后再获取新的beanFactory

@Override
    protected final void refreshBeanFactory() throws BeansException {
        if (hasBeanFactory()) {
            destroyBeans();
            closeBeanFactory();
        }
        try {
            DefaultListableBeanFactory beanFactory = createBeanFactory();
            beanFactory.setSerializationId(getId());
            customizeBeanFactory(beanFactory);
            loadBeanDefinitions(beanFactory);
            synchronized (this.beanFactoryMonitor) {
                this.beanFactory = beanFactory;
            }
        }
        catch (IOException ex) {
            throw new ApplicationContextException("I/O error parsing bean definition source for " + getDisplayName(), ex);
        }
    }

你可能感兴趣的:(跟我学spring源码(一))