SpringBoot [ 1.快速入门 ] - 目录
1.Maven项目构建
1.1构建Maven项目
使用编译器构建Maven项目,此处略.
1.2.引入POM依赖
<!-- 全局常量 -->
<properties>
<!-- SpringBoot -->
<spring.boot.version>2.1.4.RELEASE</spring.boot.version>
</properties>
<dependencies>
<!-- SpringBoot Web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<!-- 依赖统一管理 -->
<dependencyManagement>
<dependencies>
<!-- Spring Boot -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring.boot.version}</version>
<scope>import</scope>
<type>pom</type>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<!-- 插件 -->
<plugins>
<!-- 打包插件 -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.1.4.RELEASE</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>com.mantou.boot.ManTouSpringBootApplication</mainClass>
</configuration>
</plugin>
</plugins>
</build>
可是为什么需要spring-boot-starter-web呢?我们看一下项目的依赖就明白了.
我们可以看的spring-boot-starter, web,tomcat都在这个依赖下,特别是spring-boot内嵌的tomcat在该依赖下,所以该依赖是必要的.
3.启动类配置
/**
* SpringBoot 启动类
* @author mantou
*/
@SpringBootApplication
public class ManTouBootApplication {
public static void main(String[] args) {
SpringApplication.run(ManTouBootApplication.class, args);
}
}
4.配置文件
4.1.基本配置文件
- application.yml / application.properties
(SpringBoot配置文件,可使用yml文件或者properties文件; 个人推荐使用yml文件,支持多种写法,可读性强)
4.2.基本配置内容
application.yml
server:
# 端口
port: 8888
servlet:
# 项目访问前缀
context-path: /m-boot
4.4.多环境配置
在真实开发的时候,项目大多使用多环境配置,用于区分各种环境(测试,生成,本地…),如图下:
当使用多环境配置的时候,就要在主配置文件(application.yml)中选择使用的配置文件,如下:
server:
# 端口
port: 8888
servlet:
# 项目访问前缀
context-path: /m-boot
spring:
profiles:
# 选择活动的配置文件
active: dev
5.启动服务
如果编译器用的是idea,在微服务多的时候可以打开idea的RunDashboard进行服务操作(具体使用请百度)
5.1.1查看日志
控制台日志如下即启动成功