一、快速入门

  使用 SpringBoot 向浏览器输出 helloWord

1、创建 Maven 项目

image.png

2、引入依赖

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.5.RELEASE</version>
        <relativePath/> 
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

3、编写程序

  创建主程序。

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * @SpringBootApplication 来标注一个主程序类,索命这是一个Spring Boot 应用
 */
@SpringBootApplication
public class SpringBootDemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringBootDemoApplication.class, args);
    }

}

  创建 Controller 层。

package top.zyxwmj.springbootdemo.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class DemoController {

    @RequestMapping("/")
    @ResponseBody
    public String home(){
        return "hello world";
    }
}

4、启动程序

  运行 main 方法,访问 http://localhost:8080/ 即可。

image.png

二、简化部署

  SpringBoot 项目有内置的 Tomcat 可以打成 jar 包快速进行部署。在 pom.xml 文件中导入以下插件即可。

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

  运行 mvn package 命名将项目进行打包,jar 包放在 target 文件夹中。在 DOS 命令窗口中切换到 jar 包的位置使用 java -jar jar包名 命令即可直接运行项目。

image.png

三、SpringBoot 自动化配置的原理

1、父项目

  我们在使用 SpringBoot 构建项目时,都会继承 spring-boot-starter-parent 项目。

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

  我们点进 spring-boot-starter-parent 后,发现它继承 spring-boot-dependencies,而在 spring-boot-dependencies 中的 properties 标签,我们可以看到定义了各种依赖的版本。这也是我们在导入 spring-boot-starter-web 依赖不用写版本号的原因。

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

2、启动器

  spring-boot-starter 是 SpringBoot 场景启动器,而 spring-boot-starter-web 帮我们导入了 Web 模块正常运行所依赖的所有组件。
  SpringBoot 将所有的功能场景都抽取出来,做成一个个的 starters(启动器)。只需要在项目里面引入这些 starters ,相关场景的所有依赖都会导入进来,要用什么功能就导入什么场景的启动器。

3、@SpringBootApplication

  @SpringBootApplication 标注在某个类上说明这个类是 SpringBoot 的主配置类,SpringB 就会运行这个类的 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 {
@SpringBootConfiguration

  @SpringBootConfiguration 是 SpringBoot 的配置类,标注在某个类上,表示个类是 SpringBoot 的配置类。

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Configuration
public @interface SpringBootConfiguration {
@Configuration

  @Configuration 用来标注配置类(配置文件),配置类也是容器中的一个组件 @Component

@EnableAutoConfiguration

  以前我们使用 Spring 需要配置的东西,SpringBoot 帮我们自动配置,@EnableAutoConfiguration 告诉 SpringBoot 开启自动配置功能 ,这样自动配置才能生效。

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
@Import({AutoConfigurationImportSelector.class})
public @interface EnableAutoConfiguration {
@AutoConfigurationPackage 和@Import

  @AutoConfigurationPackage 是自动配置包。

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@Import(AutoConfigurationPackages.Registrar.class)
public @interface AutoConfigurationPackage {

  @Import(AutoConfigurationPackages.Registrar.class),@Import 是 Spring 的底层注解,给容器中导入一个组件。组件 AutoConfigurationPackages.Registrar.class 将主配置类(@SpringBootApplication 标注的类)所在包及下面所有子包里面的所有组件扫描到 Spring 容器中 。

  @Import(AutoConfigurationImportSelector.class),AutoConfigurationImportSelector:导入哪些组件的选择器;将所有需要导入的组件以全类名的方式返回,这些组件就会被添加到容器中,会给容器中导入非常多的自动配置类(xxxAutoConfiguration),就是给容器中导入这个场景需要的所有组件,并配置好这些组件。

	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;
	}

  有了自动配置类,免去了我们手动编写配置注入功能组件的工作。

image.png

  J2EE 的整体整合解决方案和自动配置都在 spring-boot-autoconfigure-xxx.RELEASE.jar 中。


标题:Spring Boot 入门
作者:Yi-Xing
地址:http://47.94.239.232/articles/2020/03/26/1585219339738.html
博客中若有不恰当的地方,请您一定要告诉我。前路崎岖,望我们可以互相帮助,并肩前行!