zoukankan      html  css  js  c++  java
  • 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>
    
  • 相关阅读:
    [工具分享]JetBrains ReSharper 9.0 正式版和注册码
    JAVA数据库连接池的革命 -- 从BoneCP到HikariCP
    【C#教程10】C# 判断
    【C#教程09】C# 运算符
    【C#教程07】C# 变量
    【C#教程06】C# 类型转换
    【C# 教程05】C# 数据类型
    【C# 教程04】C# 基本语法
    【C# 教程03】C# 程序结构
    【C#教程02】C# 环境
  • 原文地址:https://www.cnblogs.com/JackHou/p/11514606.html
Copyright © 2011-2022 走看看