(参照官方入门示例)
基本概念
Spring Boot的目的:便捷地创建基于Spring的独立、生产级别的应用
典型的pom.xml如下:
<?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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>myproject</artifactId>
<version>0.0.1-SNAPSHOT</version>
<!-- Inherit defaults from Spring Boot -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
</parent>
<!-- Add typical dependencies for a web application -->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<!-- Package as an executable jar -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
编写你的第一个Spring Boot应用
spring.io包含很多使用SpringBoot的“Getting Started”指导。如果你需要解决某类问题,则可以去官网参考 可以使用start.spring.io辅助项目结构的生成
前提条件
JDK和Maven已正确安装配置
创建POM
<?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">
<modelVersion>4.0.0</modelVersion>
<groupId>cn.ada.springboot</groupId>
<artifactId>springboot-test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
</parent>
<!-- Additional lines to be added here... -->
</project>
加入依赖
Spring Boot提供了大量的Starter
- 各种Starter提供了针对特定需求的依赖清单
spring-boot-starter-parent是一个特殊的Starter:
- 提供给了有用的Maven默认值
- 提供了依赖管理节点,所以在指定依赖时可以忽略version标签
spring-boot-starter-web
- 提供了开发web应用所需的依赖
要开发的第一个应用是web应用,所有需要引入spring-boot-starter-web
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
通过start.spring.io创建项目结构,及POM(即完成以上两步)
1. 生成项目结构,并下载
2. 查看项目结构
3. 查看pom.xml文件
<?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">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>cn.ada.springboot</groupId>
<artifactId>springboot-test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot-test</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
编写代码
package cn.ada.springboot.test;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@EnableAutoConfiguration
public class Example {
@RequestMapping("/")
String home() {
return "Hello World!";
}
public static void main(String[] args) {
SpringApplication.run(Example.class, args);
}
}
- @RestController 相当于 @Controller + @ResponseBody
- @EnableAutoConfiguration 基于jar依赖进行猜测并自动配置Spring
spring-boot-starter-web会加入Tomcat和Spring MVC
运行程序
命令方式运行
mvn spring-boot:run
使用IDE的Run功能
在浏览器中打开localhost:8080,则输出内容: Hello World!
创建一个可执行的Jar包
可执行Jar包通常包含编译后的class文件和所有的jar依赖 创建一个可执行Jar包,需要添加spring-boot-maven-plugin到pom中
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
spring-boot-starter-parent POM包含<executions>配置:绑定repackage目标。如果没有使用parent POM,则需要自己声明绑定repackage目标
打包
mvn package
打包完后,会在target目录下看到
可执行jar:springboot-test-0.0.1-SNAPSHOT.jar 原始的jar:springboot-test-0.0.1-SNAPSHOT.jar.original
运行jar包
java -jar target/springboot-test-0.0.1-SNAPSHOT.jar