一、快速搭建入门程序
第一步
新增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(冒号之后由一个空格)