Spring Boot 2.0 (Ⅰ) Getting Started
系统和环境
行内代码修饰的是本系列使用的环境
Windows10Spring Boot 2.0.3.RELEASEJava8+Spring Framework 5.0.7.RELEASE+- Maven 3.2+
(Maven 3.5)/Gradle 4+ - Tomcat 8.5+
(spring boot default)/Jetty 9.4+/Undertow 1.4+/任何一个兼容Servlet 3.1+的容器
github
Maven 依赖
parent
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>spring-boot-demo</artifactId>
<groupId>github.clyoudu.spring.boot</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-boot-maven-install</artifactId>
<name>spring-boot-maven-install</name>
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
dependencyManagement
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<!--<parent>
<artifactId>spring-boot-demo</artifactId>
<groupId>github.clyoudu.spring.boot</groupId>
<version>1.0-SNAPSHOT</version>
</parent>-->
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-boot-maven-install2</artifactId>
<name>spring-boot-maven-install2</name>
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<!-- Import dependency management from Spring Boot -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.0.3.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
Spring Boot CLI
Spring Boot Command Line Interface,只考虑在Windows上的安装、配置和使用。
安装
下载地址:spring-boot-cli-2.0.3.RELEASE-bin.zip。
解压。
添加环境变量:SPRING_PATH=/path/to/sring/boot/root/dir。
编辑PATH环境变量,末未添加:%SPRING_HOME%\bin。
验证:cmd -> spring --version -> Spring CLI v2.0.3.RELEASE。
简单使用
对于熟悉spring boot的选手来说,这个工具略显鸡肋,但是对于初次学习的人来说,可以快速初始化一个项目,也可以很快打包、启动一个spring boot项目。
写一个正常的
SpringMVC Controller:package github.clyoudu.spring.boot; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * Hello world! */ @RestController public class App { @RequestMapping("/{name}") public String hello(@PathVariable(value = "name") String name){ return "Hello " + name; } }- 打开命令提示符窗口;
- 切换工作目录到
Controller对应目录; - 执行命令:
sping run App.java,等待控制抬输出启动成功消息; - 浏览器输入
http://localhost:8080/clyoudu; - 页面显示:
Hello clyoudu。
很明显,就样可以快速构建一个spring boot项目。当然这并不是CLI唯一的功能,还有跟多高级的功能,比如打包、初始化项目等。
如何运行
开发的时候,直接写一个包含main方法的类,加上SpringBootApplication注解,mian方法内调用SpringApplication.run(App.class,args);即可:
package github.clyoudu.spring.boot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* Hello world!
*/
@SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class,args);
}
}
然后run这个类就能启动,但是注意这个类最好写在所有包的最外层,以便于spring boot扫描组件、配置。
当需要打包到不同的环境运行时,可以使用maven插件打包,先添加插件:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
然后运行命令:mvn -DskipTests=true package,在target文件夹就可以找到your-project-name-1.0-SNAPSHOT.jar,最后使用java -jar your-project-name-1.0-SNAPSHOT.jar就可以启动,当然这里有一些参数可以配置,比如使用哪个profile,后面再记录。
升级
因为spring boot 1.x和spring boot 2.x差别较大,几乎不能直接从1.x升级到2.x,因此对于升级,sring boot有一套专门的解决方案,这里不考虑升级的情况。

本文介绍如何在Windows环境下搭建SpringBoot 2.0开发环境,并通过Maven进行项目依赖管理。此外,还介绍了SpringBoot CLI的安装与使用方法,包括如何快速构建并启动一个SpringBoot应用。
380

被折叠的 条评论
为什么被折叠?



