Spring Cloud微服务实战 打造企业级优惠券系统 3-3 SpringBoot自动配置原理

0    课程地址

https://coding.imooc.com/lesson/380.html#mid=28271

1    浓缩精华
1.1  自动配置原理(自己理解)

SpringBootConfiguration注解   允许xml配置方式

ComponentScan        扫描通用注解

AutoConfigurationPackage  扫描第三方注解

loadSpringFactories     将jar包和class生成对应properties加载到ioc容器中

2    个人关注
2.1  个人关注

3.3

3    课程内容
3.1  自动配置

根据在类路径中的jar包,类中配置bean,SpringBoot将所有功能场景抽取出来做成1个个starter,我们只需要在项目中引入这些starter,相关场景依赖都会引入

3.2  自动配置三个重要的注解

3.3  自动配置三个重要注解解释:

@SpringBootApplication--》@SpringBootConfiguration --》@Configuration   支持javaconfig方式对代码进行配置(等同于Spring中写的xml文件)

@SpringBootApplication--》@ComponentScan  扫描当前类路径下的package(通用的注解Controller,Service,Repository注解等加载到IOC容器中)

@SpringBootApplication--》@EnableAutoConfiguration--》@AutoConfigurationPackage --》Registrar.class.registerBeanDefinitions 自动配置包,将主配置的包及子包的组件(Springdatajpa等额外的注解@Entity等)加载到ioc容器中

@SpringBootApplication--》@EnableAutoConfiguration--》AutoConfigurationImportSelector.class.selectImports.getAutoConfigurationEntry.getCandidateConfigurations--》SpringFactoriesLoader.loadFactoryNames--》loadSpringFactories:自动配置的核心是从META-INF/spring.factories 路径下找你所配置的jar包,然后把其包装成对应的properties文件,然后遍历properties,加载到ioc容器中。 

private static Map<String, List<String>> loadSpringFactories(@Nullable ClassLoader classLoader) {
        MultiValueMap<String, String> result = (MultiValueMap)cache.get(classLoader);
        if (result != null) {
            return result;
        } else {
            try {
                Enumeration<URL> urls = classLoader != null ? classLoader.getResources("META-INF/spring.factories") : ClassLoader.getSystemResources("META-INF/spring.factories");
                LinkedMultiValueMap result = new LinkedMultiValueMap();

                while(urls.hasMoreElements()) {
                    URL url = (URL)urls.nextElement();
                    UrlResource resource = new UrlResource(url);
                    Properties properties = PropertiesLoaderUtils.loadProperties(resource);
                    Iterator var6 = properties.entrySet().iterator();

                    while(var6.hasNext()) {
                        Entry<?, ?> entry = (Entry)var6.next();
                        String factoryClassName = ((String)entry.getKey()).trim();
                        String[] var9 = StringUtils.commaDelimitedListToStringArray((String)entry.getValue());
                        int var10 = var9.length;

                        for(int var11 = 0; var11 < var10; ++var11) {
                            String factoryName = var9[var11];
                            result.add(factoryClassName, factoryName.trim());
                        }
                    }
                }

                cache.put(classLoader, result);
                return result;
            } catch (IOException var13) {
                throw new IllegalArgumentException("Unable to load factories from location [META-INF/spring.factories]", var13);
            }
        }
    }
诸葛
原文地址:https://www.cnblogs.com/1446358788-qq/p/14296094.html