本节主要讲述:Hello World是如何实现的部分原理
一、pom.xml 文件
- 父项目
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.7.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
进入它的父项目:ctrl + 左键 + spring-boot-starter-parent
<!-- 它来真正管理Spring Boot 应用里面所有依赖版本 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.1.7.RELEASE</version>
<relativePath>../../spring-boot-dependencies</relativePath>
</parent>
查看已经配置好的依赖版本:ctrl + 左键 + spring-boot-dependencies
<properties>
<activemq.version>5.15.9</activemq.version>
<antlr2.version>2.7.7</antlr2.version>
<appengine-sdk.version>1.9.76</appengine-sdk.version>
<artemis.version>2.6.4</artemis.version>
<aspectj.version>1.9.4</aspectj.version>
<assertj.version>3.11.1</assertj.version>
<atomikos.version>4.0.6</atomikos.version>
<bitronix.version>2.1.4</bitronix.version>
<!--这里只截取了部分,做为示例-->
</properties>
Spring Boot 版本仲裁中心
以后我们导入依赖默认不需要写版本,但没有在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,相关场景的所有依赖都会导入进来。要用什么功能就用什么场景的启动器。
在spring 文档中(13.5 Starters章节)明确列举了spring boot 为我们提供了哪些starters,截取了部分做示例
二、主程序类
@SpringBootApplication:标注在某个类上,说明这个类是spring boot 的主配置类,
spring boot 就应该运行这个类的main函数来启动spring boot 应用。
进入@SpringBootApplication具体实现类:ctrl + 鼠标左键 + @SpringBootApplication
/*
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.autoconfigure;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.context.TypeExcludeFilter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.core.annotation.AliasFor;
/**
* Indicates a {@link Configuration configuration} class that declares one or more
* {@link Bean @Bean} methods and also triggers {@link EnableAutoConfiguration
* auto-configuration} and {@link ComponentScan component scanning}. This is a convenience
* annotation that is equivalent to declaring {@code @Configuration},
* {@code @EnableAutoConfiguration} and {@code @ComponentScan}.
*
* @author Phillip Webb
* @author Stephane Nicoll
* @since 1.2.0
*/
@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 {
/**
* Exclude specific auto-configuration classes such that they will never be applied.
* @return the classes to exclude
*/
@AliasFor(annotation = EnableAutoConfiguration.class)
Class<?>[] exclude() default {};
/**
* Exclude specific auto-configuration class names such that they will never be
* applied.
* @return the class names to exclude
* @since 1.3.0
*/
@AliasFor(annotation = EnableAutoConfiguration.class)
String[] excludeName() default {};
/**
* Base packages to scan for annotated components. Use {@link #scanBasePackageClasses}
* for a type-safe alternative to String-based package names.
* @return base packages to scan
* @since 1.3.0
*/
@AliasFor(annotation = ComponentScan.class, attribute = "basePackages")
String[] scanBasePackages() default {};
/**
* Type-safe alternative to {@link #scanBasePackages} for specifying the packages to
* scan for annotated components. The package of each class specified will be scanned.
* <p>
* Consider creating a special no-op marker class or interface in each package that
* serves no purpose other than being referenced by this attribute.
* @return base packages to scan
* @since 1.3.0
*/
@AliasFor(annotation = ComponentScan.class, attribute = "basePackageClasses")
Class<?>[] scanBasePackageClasses() default {};
}
进入之后,上面有一堆注解标志,这里主要看一下@SpringBootConfiguration和@EnableAutoConfiguration这两个标记
@SpringBootConfiguration:spring boot 配置类,标注在某个类上,表示这是一个spring boot 配置类。
再点进去,发现它也是由@Configuration标注的(@Configuration:配置类上来标注这个注解);
再点@Configuration进去,发现它由@Component标注,表示配置类也是容器中的一个组件。
@EnableAutoConfiguration:启用自动配置功能
以前我们需要配置的东西,spring boot 帮我们自动配置;@EnableAutoConfiguration告诉spring boot 开启自动配置功能,这样自动配置才能生效。
点进去:ctrl + 左键 + @EnableAutoConfiguration
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
@Import(AutoConfigurationImportSelector.class)
public @interface EnableAutoConfiguration {
主要看,
@AutoConfigurationPackage和
@Import(AutoConfigurationImportSelector.class)
这两个注解标记。
@AutoConfigurationPackage:自动配置包,点进去
/*
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.autoconfigure;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.context.annotation.Import;
/**
* Indicates that the package containing the annotated class should be registered with
* {@link AutoConfigurationPackages}.
*
* @author Phillip Webb
* @since 1.3.0
* @see AutoConfigurationPackages
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@Import(AutoConfigurationPackages.Registrar.class)
public @interface AutoConfigurationPackage {
}
@Import(AutoConfigurationPackages.Registrar.class):@Import底层注解标记,表示从AutoConfigurationPackages.Registrar.class中导入组件
@Override
public void registerBeanDefinitions(AnnotationMetadata metadata, BeanDefinitionRegistry registry) {
register(registry, new PackageImport(metadata).getPackageName());
}
}
这个类的大概意思是,将主配置类(@SpringBootApplication标注的类),所在包及下面所有子包里面的所有组件扫描到spring容器。
@Import(AutoConfigurationImportSelector.class):spring 底层注解@Import给容器导入一个组件;
@Import(AutoConfigurationImportSelector.class)
给容器中导入组件
AutoConfigurationImportSelector:导入哪些组件的选择器
将所有需要导入的组件以全类名的方式返回;这些组件就会被添加到容器中;
会给容器导入非常多的自动配置类(xxxautoConfiguration);就是给容器中导入这个场景所需要的所有组件,并配置好这些组件。
点进去:ctrl + 左键 + AutoConfigurationImportSelector
@Override
public String[] selectImports(AnnotationMetadata annotationMetadata) {
if (!isEnabled(annotationMetadata)) {
return NO_IMPORTS;
}
AutoConfigurationMetadata autoConfigurationMetadata = AutoConfigurationMetadataLoader
.loadMetadata(this.beanClassLoader);
AutoConfigurationEntry autoConfigurationEntry = getAutoConfigurationEntry(autoConfigurationMetadata,
annotationMetadata);
return StringUtils.toStringArray(autoConfigurationEntry.getConfigurations());
}
有了自动配置类,免去了我们手动编写配置注入功能组件等的工作
AutoConfigurationMetadata autoConfigurationMetadata = AutoConfigurationMetadataLoader
.loadMetadata(this.beanClassLoader);
protected static final String PATH = "META-INF/" + "spring-autoconfigure-metadata.properties";
public static AutoConfigurationMetadata loadMetadata(ClassLoader classLoader) {
return loadMetadata(classLoader, PATH);
}
spring boot 在启动时,从类路径下的"META-INF/" + "spring-autoconfigure-metadata.properties"中获取值,将这些值做为自动配置类导入到容器中,自动配置类就生效,帮助我们自动配置工作。
以前我们需要自己配置的东西,自动配置类都帮我们配置好了
J2EE整体整合解决方案和自动配置都在
org\springframework\boot\spring-boot-autoconfigure\2.1.7.RELEASE\spring-boot-autoconfigure-2.1.7.RELEASE.jar!\org\springframework\boot\autoconfigure