一、定义
二、自动配置原理
1、前置准备
启动main主程序
2、大体流程
3、具体过程
- SpringApplication.run(App.class,args)
- SpringApplication run(new Class<?>[] { primarySource }, args)
- new SpringApplication(primarySources).run(args)
- new SpringApplication(primarySources)(创建并初始化SpringApplication实例,App.class是容器的入口配置类)
- SpringApplication(ResourceLoader resourceLoader, Class<?>... primarySources)
- this.primarySources = new LinkedHashSet<>(Arrays.asList(primarySources));
- this.webApplicationType = WebApplicationType.deduceFromClasspath();(推断容器类型,默认是WebApplicationType.SERVLET)
- this.bootstrapRegistryInitializers = new ArrayList<>(getSpringFactoriesInstances(BootstrapRegistryInitializer.class));(从cache或者/META-INF/spring.factories文件里面读取BootstrapRegistryInitializer的bean信息,该bean信息实例化后可以用来初始化BootstrapRegistry实例)
- SpringApplication getSpringFactoriesInstances(Class<T> type)
- SpringApplication getSpringFactoriesInstances(type, new Class<?>[] {})
- ClassLoader classLoader = getClassLoader();
- Set<String> names = new LinkedHashSet<>(SpringFactoriesLoader.loadFactoryNames(type, classLoader));
- SpringFactoriesLoader loadFactoryNames
- SpringFactoriesLoader loadSpringFactories(classLoaderToUse).getOrDefault(factoryTypeName, Collections.emptyList())
- SpringFactoriesLoader loadSpringFactories(ClassLoader classLoader)
- Map<String, List<String>> result = cache.get(classLoader)(先尝试从SpringFactoriesLoader的静态变量cache中获取
- Enumeration<URL> urls = classLoader.getResources(FACTORIES_RESOURCE_LOCATION)(上面的cache获取不到数据,则会通过getResources方法加载FACTORIES_RESOURCE_LOCATION路径的文件,FACTORIES_RESOURCE_LOCATION表示META-INF/spring.factories)
- String[] factoryImplementationNames =StringUtils.commaDelimitedListToStringArray((String) entry.getValue());(由于entry.getValue的结果值是一个由逗号拼接的字符串,所以需要通过该commaDelimitedListToStringArray把字符串转为字符串数组)
- SpringFactoriesLoader cache.put(classLoader, result);(把上面的字符串数组放入SpringFactoriesLoader的静态变量cache
- SpringFactoriesLoader loadSpringFactories(ClassLoader classLoader)
- SpringFactoriesLoader loadSpringFactories(classLoaderToUse).getOrDefault(factoryTypeName, Collections.emptyList())
- SpringFactoriesLoader loadFactoryNames
- List<T> instances = createSpringFactoriesInstances(type, parameterTypes, classLoader, args, names)
- SpringApplication getSpringFactoriesInstances(type, new Class<?>[] {})
- SpringApplication getSpringFactoriesInstances(Class<T> type)
- SpringApplication setInitializers((Collection) getSpringFactoriesInstances(ApplicationContextInitializer.class));(从cache或者META-INF/spring.factories文件里面读取ApplicationContextInitializer的bean信息,该bean信息实例化后可以用来初始化ConfigurableApplicationContext实例)
- SpringApplication setListeners((Collection)getSpringFactoriesInstances(ApplicationListener.class));(从cache或者META-INF/spring.factories文件里面读取ApplicationListener的bean信息,这些bean信息是容器预留的事件监听器)
- this.mainApplicationClass = deduceMainApplicationClass();(找到并设置程序主类)
- SpringApplication(ResourceLoader resourceLoader, Class<?>... primarySources)
- run(String... args)
- ConfigurableApplicationContext context = null
- context = createApplicationContext()(创建容器)
- this.applicationContextFactory.create(this.webApplicationType);
- ApplicationContextFactory.DEFAULT
- SERVLET(容器Web类型是Servlet)
- new AnnotationConfigServletWebServerApplicationContext()
- SERVLET(容器Web类型是Servlet)
- ApplicationContextFactory.DEFAULT
- this.applicationContextFactory.create(this.webApplicationType);
- prepareContext(bootstrapContext, context, environment, listeners, applicationArguments, printedBanner);
- refreshContext(context);
- refresh(context)(刷新容器)
- afterRefresh(context, applicationArguments);
- callRunners(context, applicationArguments)
- new SpringApplication(primarySources)(创建并初始化SpringApplication实例,App.class是容器的入口配置类)
- new SpringApplication(primarySources).run(args)
- SpringApplication run(new Class<?>[] { primarySource }, args)