<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.9.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
进入父项目
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.1.9.RELEASE</version>
<relativePath>../../spring-boot-dependencies</relativePath>
</parent>
这里才是真正管理SpringBoot应用里面所有依赖版本的地方,SpringBoot的版本控制中心;
以后我们导入依赖默认是不需要写版本;但是如果导入的包没有在依赖中管理着就需要手动配置版本了;
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
// @SpringBootApplication 标注下面的这个类是一个SpringBoot的应用
@SpringBootApplication
public class HalftownApplication {
public static void main(String[] args) {
// 将SpringBoot 应用启动
SpringApplication.run(HalftownApplication.class, args);
}
}
@SpringBootConfiguration : SpringBoot的配置类,标注在某个类上, 表示这是一个SpringBoot的配置类
@Configuration: Spring配置类,配置类上来标注这个注解,说明这是一个配置类 ,配置类---即----配置文件;
@Componet: 说明这是一个Spring的组件,启动类本身也是Spring中的一个组件而已,负责启动应用
@EnableAutoConfiguration:开启自动配置功能,以前我们需要自己配置的东西,而现在SpringBoot可以自动帮我们配置,@EnableAutoConfiguration告诉SpringBoot开启自动配置功能,这样自动配置才能生效
@AutoConfigurationPackage: 自动配置包
@Import({Registrar.class}): 自动配置包注册器
@Import({AutoConfigurationImportSelector.class}): 自动配置导入选择器
(@import :Spring底层注解@import , 给容器中导入一个组件)
Registrar.class 将主配置类 【即@SpringBootApplication标注的类】的所在包及包下面所有子包里面的所有组件扫描到Spring容器 ;
@ComponentScan:这个注解在Spring中很重要 , 它对应XML配置中的元素。@ComponentScan的功能就是自动扫描并加载符合条件的组件或者bean , 将这个bean定义加载到IOC容器中 ;
META-INF/spring.factories: 自动配置的的核心文件
springboot所有自动配置都是在启动的时候扫描并加载:spring.factories所有的自动配置类都在这里面,但是不一定生效,要判断条件是否成立,只要导入了对应的start,就有对应的启动器了,有了启动器,我们自动装配就会生效,然后就配置成功!
所以,自动配置真正实现是从classpath中搜寻所有的META-INF/spring.factories配置文件 ,并将其中对应的 org.springframework.boot.autoconfigure. 包下的配置项,通过反射实例化为对应标注了 @Configuration的JavaConfig形式的IOC容器配置类 , 然后将这些都汇总成为一个实例并加载到IOC容器中。
结论:
Run
我最初以为就是运行了一个main方法,没想到却开启了一个服务;
@SpringBootApplication
public class SpringbootDemo02Application {
public static void main(String[] args) {
//该方法返回一个ConfigurableApplicationContext对象
//参数一:应用入口的类 参数类:命令行参数
SpringApplication.run(SpringbootDemo02Application.class, args);
}
}
分析该方法主要分两部分:
SpringApplication
这个类主要做了以下四件事情
public SpringApplication(ResourceLoader resourceLoader, Class... primarySources) {
this.sources = new LinkedHashSet();
this.bannerMode = Mode.CONSOLE;
this.logStartupInfo = true;
this.addCommandLineProperties = true;
this.addConversionService = true;
this.headless = true;
this.registerShutdownHook = true;
this.additionalProfiles = new HashSet();
this.isCustomEnvironment = false;
this.resourceLoader = resourceLoader;
Assert.notNull(primarySources, "PrimarySources must not be null");
this.primarySources = new LinkedHashSet(Arrays.asList(primarySources));
this.webApplicationType = WebApplicationType.deduceFromClasspath();
this.setInitializers(this.getSpringFactoriesInstances(ApplicationContextInitializer.class));
this.setListeners(this.getSpringFactoriesInstances(ApplicationListener.class));
this.mainApplicationClass = this.deduceMainApplicationClass();
}
部分内容来自于秦疆のJava世界
原文:https://www.cnblogs.com/lskreno/p/12221241.html