准备
本文使用maven。请确保你的maven在3.4+版本
本文使用当前最新的springBoot版本2.5.1
SpringBoot2.5.1需要jdk1.8+
本文使用idea开发
新建springboot项目
项目结构如下:
编写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>com.yyoo.mytest</groupId>
<artifactId>springboot</artifactId>
<version>1.0-SNAPSHOT</version>
<!-- 默认打war包 -->
<packaging>war</packaging>
<!-- spring-boot-starter-parent是一个特殊的启动器,他可以使我们在下面定义jar依赖时,不用提供版本标签 version -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.1</version>
</parent>
<dependencies>
<!-- 添加web依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
编写Application启动类
package com.yyoo.boot;
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
@SpringBootApplication(scanBasePackages = "com.yyoo")
public class Appliction {
@RequestMapping("/")
public String hello(){
return "Hello World!";
}
public static void main(String[] args) {
SpringApplication.run(Appliction.class, args);
}
}
@SpringBootApplication(scanBasePackages = “com.yyoo”) 注意填你自己的包名地址
运行示例
直接执行main方法即可。启动日志如下:
我们可以看到“ Tomcat started on port(s): 8080 (http) with context path ‘’ ”字样,表示web应用已经成功启动,端口是8080,上下文是空。
访问示例
在浏览器访问:http://localhost:8080
就可以看到如下输出:
我们的示例就成功了。
创建可执行的jar
传统的web应用部署方式是打war包(我们默认也是打的war包),然后部署到应用服务器(tomcat、jboos或weblogic等)应用服务器中,Spring Boot提供了更简单的方式来部署或运行我们的web应用程序。
我们修改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>com.yyoo.mytest</groupId>
<artifactId>springboot</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<!-- spring-boot-starter-parent是一个特殊的启动器,他可以使我们在下面定义jar依赖时,不用提供版本标签 version -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.1</version>
</parent>
<dependencies>
<!-- 添加web依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
修改了packaging标签为打jar包,新增了spring-boot-maven-plugin插件
执行mvn打包后查看target目录
你会看到如下两个文件:
- springboot-1.0-SNAPSHOT.jar:可直接执行的jar包文件
- springboot-1.0-SNAPSHOT.jar.original:这是Maven在Spring Boot重新打包之前创建的原始jar文件。
打好包之后,使用命令行执行
$ java -jar springboot-1.0-SNAPSHOT.jar
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.5.1)
2021-06-16 15:29:39.035 INFO 19408 --- [ main] com.yyoo.boot.Appliction : Starting Appliction v1.0-SNAPSHOT using Java 1.8.0_181 on DESKTOP-0886E44 with PID 19408 (C:\work\project\mytest\springboot\target\springboot-1.0-SNAPSHOT.jar started by zhou in C:\work\project\mytest\springboot\target)
2021-06-16 15:29:39.040 INFO 19408 --- [ main] com.yyoo.boot.Appliction : No active profile set, falling back to default profiles: default
2021-06-16 15:29:42.288 INFO 19408 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2021-06-16 15:29:42.312 INFO 19408 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2021-06-16 15:29:42.312 INFO 19408 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.46]
2021-06-16 15:29:42.475 INFO 19408 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2021-06-16 15:29:42.475 INFO 19408 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 3318 ms
2021-06-16 15:29:43.312 INFO 19408 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2021-06-16 15:29:43.329 INFO 19408 --- [ main] com.yyoo.boot.Appliction : Started Appliction in 5.203 seconds (JVM running for 5.992)
2021-06-16 15:30:11.349 INFO 19408 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
2021-06-16 15:30:11.350 INFO 19408 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2021-06-16 15:30:11.352 INFO 19408 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 1 ms
按下ctrl+c即可退出关闭程序。
到此我们的Spring Boot入门程序就完成了。非常简单。为什么简单?因为Spring Boot默认为我们做了很多事情。随着学习的深入,你会更加理解。