0%

springboot系列(000)之搭建项目

SpringBoot使用第一步、使用SpringBoot创建一个Maven项目。
创建SpringBoot项目有多种方法。

一、第一种从Spring官网下载

1、打开Sprig官方网站自行选择,初始化一个项目包
官网初始化SpringBoot项目

1
2
点击图片右上角ADD DEPENDENCIES……添加依赖
点击图片下方GENERATE CTRL 下载初始化的SpringBoot项目

2、把下载的项目解压后导入开发工具

二、第二种使用开发工具生成

以IntelliJ IDEA 为例
1、New Project 选择 Spring Initializr项、选择jdk版本
初始化项目
2、填写项目信息、选择Java Version、以及Packaging类型(jar/war)
填写项目信息
3、选择项目依赖、以及Spring Boot 版本
选择项目依赖
4、选择项目路径、并提交。
选择项目路径
5、打开结构、Packaging类型jar
选择项目路径
6、打开结构、Packaging类型war
选择项目路径

三、选择war和jar的不同

1
2
3
4
5
war项目在启动目录下比jar多了一个ServletInitializer文件
默认生成的pom.xml文件中war比jar多了spring-boot-starter-tomcat的引用
jar引用的是spring-boot-starter
war引用的是spring-boot-starter-web
DemoApplication.jar内容完全相同

web项目的ServletInitializer.jar内容

1
2
3
4
5
6
7
8
9
10
11
12
13
package net.zuze.demo;

import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;

public class ServletInitializer extends SpringBootServletInitializer {

@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(DemoApplication.class);
}

}

jar项目的pom.xml

1
2
3
4
5
6
7
8
9
10
11
12
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>

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

web项目的pom.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

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

至此SpringBoot项目Maven创建介绍结束