Spring Boot快速搭建入门程序

一、快速搭建入门程序

第一步

新增Spring-Boot-starter-parent依赖【父级项目的web依赖】

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.7.RELEASE</version>
</parent>

第二步

新增Spring-Boot-starter-web依赖【子项目的web依赖】

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

第三步

使用@SpringBootApplication注解并创建主程序类

@SpringBootApplication
public class HelloApplication {
    public static void main(String[] args){
        SpringApplication.run(HelloApplication.class);
    }
}

第四步

创建Controller HelloWorld访问程序

@RestController
public class HelloWorldController {
    @RequestMapping("/hello")
    public String hello(){
        return "This is my first SpringBoot Application";
    }
}

在这里插入图片描述

二、SpringBoot启动的两种方法

1.直接运行Main方法

2.使用插件启动

  • 第一步 新增MAVEN插件spring-boot-maven-plugin
  • 第二步 MAVEN插件增加<configuration>与<mainClass>,配置启动主函数

三、使用application.properties配置项目

properties和yml项目常用的两种文件配置方式,properties的优先级高于yml

  • Spring Boot自动生成Resources目录下的application.properties配置文件
    配置格式:key=value
  • Spring Boot自动生成Resources目录下的application.yml配置文件
    配置格式:key: value(冒号之后由一个空格)
发布了395 篇原创文章 · 获赞 130 · 访问量 20万+

猜你喜欢

转载自blog.csdn.net/qq_40507857/article/details/103688955
今日推荐