- 程序中的pom.xml文件:
- 一、父级标签
- <parent>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-parent</artifactId>
- <version>2.0.5.RELEASE</version>
- <relativePath/>
- </parent>
这是一个父级标签:让我们点击org.springframework.boot就会查看到他的父级的依赖:
-
- <parent>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-dependencies</artifactId>
- <version>2.0.5.RELEASE</version>
- <relativePath>../../spring-boot-dependencies</relativePath>
- </parent>
- 就会发现它才是真正的SpringBoot的版本控制中心,这样一来我们在导入
依赖的时候就不需要写导入的版本号了,但是如果不在dependencies里面
管理的依赖还是需要进行版本号的输入的。
- 二、依赖标签(启动器)
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-web</artifactId>
- </dependency>
spring-boot-starter-web:spring-boot-starter:spring-boot场景启动器;帮我们导入了web模块
正常运行所依赖的组件;
Spring Boot将所有的功能场景都抽取出来,做成一个个的starters(启动器),
只需要在项目里面引入这些starter相关场景的所有依赖都会导入进来。要用什么
功能就导入什么场景的启动器。
例如:
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-test</artifactId>
- <scope>test</scope>
- </dependency>
这是一个test(测试用的)启动器,我们要用的是测试功能,导入测试场景的启动器就可以了。
程序中的主程序文件:
- package com.example.demo;
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
- @SpringBootApplication
- public class DemoApplication {
- public static void main(String[] args) {
- SpringApplication.run(DemoApplication.class, args);
- }
- }
@SpringBootApplication:Spring Boot应用标注在某个类上说明这个类是SpringBoot的主配置类,
SpringBoot就应该运行这个类的main方法来启动SpringBoot应用;
我们接下来就研究一下这个@SpringBootApplication注解,点进去后看到的内容是:
- @Target({ElementType.TYPE})
- @Retention(RetentionPolicy.RUNTIME)
- @Documented
- @Inherited
- @SpringBootConfiguration
- @EnableAutoConfiguration
- @ComponentScan(
- excludeFilters = {@Filter(
- type = FilterType.CUSTOM,
- classes = {TypeExcludeFilter.class}
- ), @Filter(
- type = FilterType.CUSTOM,
- classes = {AutoConfigurationExcludeFilter.class}
- )}
- )
- public @interface SpringBootApplication {
1)、@SpringBootConfiguration:SpringBoot的配置类,标注在哪个类上面就代表这个类就是一个SpringBoot
的配置类,我们点进这个注解(@SpringBootConfiguration)里面看一下:
- @Target({ElementType.TYPE})
- @Retention(RetentionPolicy.RUNTIME)
- @Documented
- @Configuration
- public @interface SpringBootConfiguration {
- }
我们看到了这个@Configuration注解,这就证明了SpringBootConfiguration 就是一个配置类,再点进到这个注解
(@Configuration)里面看一下:
- @Target({ElementType.TYPE})
- @Retention(RetentionPolicy.RUNTIME)
- @Documented
- @Component
- public @interface Configuration {
- @AliasFor(
- annotation = Component.class
- )
- String value() default "";
- }
我们看到了@Component这个注解,证明了配置类也是容器中的一个组件。
2)、@EnableAutoConfiguration:开启自动配置,这样一来我们需要的所有配置,SpringBoot都将帮我们
配置完成,我们点进去这个注解(@EnableAutoConfiguration)看一看:
- @Target({ElementType.TYPE})
- @Retention(RetentionPolicy.RUNTIME)
- @Documented
- @Inherited
- @AutoConfigurationPackage
- @Import({AutoConfigurationImportSelector.class})
- public @interface EnableAutoConfiguration {
- String ENABLED_OVERRIDE_PROPERTY = "spring.boot.enableautoconfiguration";
- Class<?>[] exclude() default {};
- String[] excludeName() default {};
- }
这里面有这两个重要的注解(@AutoConfigurationPackage与@Import({AutoConfigurationImportSelector.class}))
先研究一下这个注解(@AutoConfigurationPackage)这是自动配置包的意思我们点进去看一下这个注解:
- @Target({ElementType.TYPE})
- @Retention(RetentionPolicy.RUNTIME)
- @Documented
- @Inherited
- @Import({Registrar.class})
- public @interface AutoConfigurationPackage {
- }
里面有一个@Import({Registrar.class})注解,这是Spring底层的注解,是导入一个组件的意思,导入的组件由
Registrar.class将主配置类(也就是SpringBootApplication标注的类)的所在层级以及该层级以下所有层级的
所有组件都扫描到Spring的容器中。
接下来我们研究一下这个注解(@Import({AutoConfigurationImportSelector.class}))这是一个给容器中导入组件的
一个注解,AutoConfigurationImportSelector:导入哪些组件的选择器;将所有需要导入的组件以全类名的方式返回;
这些组件就会被添加到容器中;会给容器中导入非常多的自动配置类(xxxAutoConfiguration);就是给容器中导入
这个场景需要的所有组件,并配置好这些组件;下面是源码(AutoConfigurationImportSelector这个类下面的getAutoConfigurationImportFilters的方法):
- protected List<AutoConfigurationImportFilter> getAutoConfigurationImportFilters() {
- return SpringFactoriesLoader.loadFactories(AutoConfigurationImportFilter.class, this.beanClassLoader);
- }
我们进入到SpringFactoriesLoader中看一下:这个是加载properties属性文件的方法
- public abstract class SpringFactoriesLoader {
- public static final String FACTORIES_RESOURCE_LOCATION = "META-INF/spring.factories";
- private static final Log logger = LogFactory.getLog(SpringFactoriesLoader.class);
- private static final Map<ClassLoader, MultiValueMap<String, String>> cache = new ConcurrentReferenceHashMap();
- public SpringFactoriesLoader() {
- }
我们看到所加在的properties文件的路径是META-INF/spring.factories,我们找一下这个文件的位置看一下里面都有什么:

Spring Boot在启动的时候从类路径下的META-INF/spring.factories中获取EnableAutoConfiguration指定的值,将这些值作为
自动配置类导入到容器中,自动配置类就生效,帮我们进行自动配置工作;
J2EE的整体整合解决方案和自动配置都在spring-boot-autoconfigure-2.0.5.RELEASE.jar;中