SpringbBoot-01运行原理

    技术2023-10-14  82

    SpringbBoot-01

    什么是SpringBoot: 就是一个javaweb的开发框架,和SpringMVC类似,对比其他javaweb框架的好处,简化开发,能迅速的开发web应用,省去了SSM的配置文件,可以实现自动装配。

    SpringbBoot的项目部署: IDEA可以直接在软件部署 其他软件在springboot官网(https://start.spring.io/)自行选择配置

    SpringbBoot的运行原理: pom.xml文件: 在这里配置所有的依赖,首先他依赖一个父项目,主要是管理项目的资源过滤和插件

    <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.3.1.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent>

    父依赖点进去还有一个父依赖,这里才是真正管理SpringBoot应用里边所有依赖版本的地方,SpringBoot的版本控制中心

    <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>2.3.1.RELEASE</version> </parent>

    启动器 spring-boot-starter -XXX: 就是SpringBoot的场景启动器,能将所有功能场景都提取出来,做成一个个starter启动器,使用只需在pom.xml文件引入依赖即可

    默认主启动类: 1.首先点进 是这样的 这个注解对应XML配置中的元素,自动扫描并加载符合条件的组件或者bean,将这个bean定义加载到IOC容器 配置类,标注在某个类上,表示这是SpringBoot的配置类

    继续点进去看,发现@Component,原来启动类本身也只是Spring的一个组件,负责启动应用

    2.回到SpringBootApplication注解中继续看下一个 这个很重要,以前学SSM需要自己配置文件,在SpringBoot 中就是这个注解帮助我们开起自动配置功能的 自动配置包 点进去是这样的,@import是给容器导入一个组件,registrar.class则是将主启动类的所在包及包下面所有子包里面的所有组件扫描到Spring容器 3.回到上边的@EnableAutoConfiguration @Import:给容器导入组件 AutoConfigurationImportSelector :自动配置导入选择器 点进去,里边有一个这样的方法,作用是获取候选的配置,返回的就是最开始的注解类EnableAutoConfiguration

    protected List<String> getCandidateConfigurations(AnnotationMetadata metadata, AnnotationAttributes attributes) { List<String> configurations = SpringFactoriesLoader.loadFactoryNames(getSpringFactoriesLoaderFactoryClass(), getBeanClassLoader()); Assert.notEmpty(configurations, "No auto configuration classes found in META-INF/spring.factories. If you " + "are using a custom packaging, make sure that file is correct."); return configurations; }

    然后点进SpringFactoriesLoader的loadFactoryNames

    public static List<String> loadFactoryNames(Class<?> factoryType, @Nullable ClassLoader classLoader) { String factoryTypeName = factoryType.getName(); return loadSpringFactories(classLoader).getOrDefault(factoryTypeName, Collections.emptyList()); }

    继续点进loadSpringFactories

    private static Map<String, List<String>> loadSpringFactories(@Nullable ClassLoader classLoader) { MultiValueMap<String, String> result = cache.get(classLoader); if (result != null) { return result; } try { Enumeration<URL> urls = (classLoader != null ? classLoader.getResources(FACTORIES_RESOURCE_LOCATION) : ClassLoader.getSystemResources(FACTORIES_RESOURCE_LOCATION)); result = new LinkedMultiValueMap<>(); while (urls.hasMoreElements()) { URL url = urls.nextElement(); UrlResource resource = new UrlResource(url); Properties properties = PropertiesLoaderUtils.loadProperties(resource); for (Map.Entry<?, ?> entry : properties.entrySet()) { String factoryTypeName = ((String) entry.getKey()).trim(); for (String factoryImplementationName : StringUtils.commaDelimitedListToStringArray((String) entry.getValue())) { result.add(factoryTypeName, factoryImplementationName.trim()); } } } cache.put(classLoader, result); return result; } catch (IOException ex) { throw new IllegalArgumentException("Unable to load factories from location [" + FACTORIES_RESOURCE_LOCATION + "]", ex); } }

    发现一个多次出现的文件:spring.factories

    打开它, 看到了很多自动配置的文件;这就是自动配置根源所在!

    结论: 1.Springboot在启动的时候从类路径下的META-INF/spring.factoriesz中获取EnableAutoConfiguration指定的值 2.将这些值作为自动配置类导入容器,自动配置类就生效 3.整个J2EE的整体解决方案和自动配置都在springboot-autoconfigureV的jar包中 4.它会给容器中导入非常多的自动配置类(xxxConfiguration),就是给容器中导入这个场景需要的所有组件,并配置好这些组件 5.有了自动配置类,免去了我们手动编写配置注入功能的工作

    Processed: 0.014, SQL: 9