SpringBoot系列__02HelloWorld探究

在前文中,我们创建了一个简单的hello world,现在,利用这个简单的程序,来简单分析一下SpringBoot的启动过程。
如果你是使用过SSM框架的人,尤其是4.0之前的版本,相信你使用过xml方式来配置你的项目;但是,当你首次使用SpringBoot的时候,会惊奇的发现,一点配置文件也没写(你没写,但是不代表没有),就能成功启动一个web应用。
下面关于这点,来简单解释一下。

1.通过解析POM文件来查看SpringBoot中默认为我们添加了那些maven依赖。

这里,贴一下代码:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.8.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.bm.springboot</groupId>
    <artifactId>helloworld</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>helloworld</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

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

</project>

在这里,我们发现,hello world项目,作为一个web项目,首先有一个父项目:spring-boot-starter-parent该项目是所有的SpringBoot项目的父项目,根据maven项目的继承关系,会被其他所有模块(如aop模块, ioc模块,以及我们用到的web模块)。点进去查看一下:

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


 <resource>
        <filtering>true</filtering>
        <directory>${basedir}/src/main/resources</directory>
        <includes>
          <include>**/application*.yml</include>
          <include>**/application*.yaml</include>
          <include>**/application*.properties</include>
        </includes>
      </resource>
      <resource>
        <directory>${basedir}/src/main/resources</directory>
        <excludes>
          <exclude>**/application*.yml</exclude>
          <exclude>**/application*.yaml</exclude>
          <exclude>**/application*.properties</exclude>
        </excludes>
      </resource>

在这里,我们看到:

1.在这里定义了引入SpringBoot的版本号,因此,我们引入其他模块的时候,无需自己定义,也不会担心版本不匹配的问题了;

2.SpringBoot首先排除掉了无条件的加载配置文件,而是使用拦截器先行校验(优先使用用户自定义的配置文件,用户没有定义的选项,使用默认的配置进行配置)。

2.启动器

在pom文件中,我们发现了这个:

<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
</dependency>
posted @ 2019-09-12 18:42  本墨  阅读(206)  评论(0编辑  收藏  举报