11. 开发您的第一个Spring Boot应用程序
可以先检查一下自己系统要求符合版本吗?
11.0 创建一个maven项目
11.1 创建 pom.xml
文件
<!--Spring Boot提供了许多“Starters”,可以将jar添加到类路径中。-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.7.RELEASE</version>
</parent>
spring-boot-starter-parent是一个特殊的启动器,提供有用的Maven默认值。它还提供了一个依赖项管理部分,以便您可以省略“blessed”依赖项的版本标记。
其他“Starters”提供了在开发特定类型的应用程序时可能需要的依赖项。由于我们正在开发Web应用程序,因此我们添加了spring-boot-starter-web依赖项。在此之前先来了解一下项目依赖项的树表示结构。
因为spring-boot-starter-parent本身不提供依赖关系。所以要添加必要的依赖项,请编辑pom.xml
并在父节下面添加spring-boot-starter-web依赖项 :
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
没加之前:
加上以后:
可以看到 现在有许多其他依赖项,包括Tomcat Web服务器和Spring Boot本身。
11.2 编写代码
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;
/**
* Created with IntelliJ IDEA.
* Description:
* Mail: 761472239@qq.com
* Date: 2019-09-05
* Time: 0:03
*/
@RestController
@EnableAutoConfiguration
public class Example {
@RequestMapping("/")
String home() {
return "Hello world";
}
public static void main(String[] args) {
SpringApplication.run(Example.class, args);
}
}
- @RestController注解
它为阅读代码的人提供了提示,而为Spring提供了特定角色的提示。在这种情况下,我们的类是一个Web @Controller,所以Spring在处理传入的Web请求时会考虑它。
- @RequestMapping注解
The @RequestMapping annotation provides “routing” information.他告诉spring任何带有/
path
http request都映射到home方法。@RestController注释告诉Spring将结果字符串直接渲染回调用者。
@RestController
and @RequestMapping
注解是springmvc的注解。它们不是Spring Boot特有的。
- @EnableAutoConfiguration Annotation
第二个类级别注释是@EnableAutoConfiguration
。这个注释告诉Spring Boot根据你添加的jar依赖关系“猜测”你想要如何配置Spring。由于spring-boot-starter-web添加了Tomcat和Spring MVC,因此自动配置假定您正在开发Web应用程序并相应地设置Spring。
自动配置旨在与“启动器”配合使用(Starters and Auto-configuration),但这两个概念没有直接联系在一起。You are free to pick and choose jar dependencies outside of the starters. Spring Boot still does its best to auto-configure your application.
main
方法
我们的应用程序的最后一部分是主要方法。这只是遵循应用程序入口点的Java约定的标准方法。我们的main方法通过调用run来委托Spring Boot的SpringApplication类。SpringApplication引导我们的应用程序,启动Spring,然后启动自动配置的Tomcat Web服务器。我们需要将Example.class作为参数传递给run方法,以告诉SpringApplication是主要的Spring组件。The args
array is also passed through to expose any command-line arguments.
11.3 Running the Example
At this point, your application should work. Since you used the spring-boot-starter-parent
POM, you have a useful run
goal that you can use to start the application.
命令行:
Type mvn spring-boot:run
from the root project directory to start the application. You should see output similar to the following:
IDEA:直接运行就完了
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.1.7.RELEASE)
2019-09-05 13:22:46.571 INFO 10740 --- [ main] Example : Starting Example on LAPTOP-42OC4N00 with PID 10740 (D:\Workspaces\IdeaProjects\gs_rest_service\myproject\target\classes started by 76147 in D:\Workspaces\IdeaProjects\gs_rest_service)
2019-09-05 13:22:46.577 INFO 10740 --- [ main] Example : No active profile set, falling back to default profiles: default
2019-09-05 13:22:49.766 INFO 10740 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2019-09-05 13:22:49.818 INFO 10740 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2019-09-05 13:22:49.818 INFO 10740 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.22]
2019-09-05 13:22:50.028 INFO 10740 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2019-09-05 13:22:50.028 INFO 10740 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 3360 ms
2019-09-05 13:22:50.312 INFO 10740 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2019-09-05 13:22:50.638 INFO 10740 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2019-09-05 13:22:50.641 INFO 10740 --- [ main] Example : Started Example in 4.999 seconds (JVM running for 6.212)
11.4 创建一个可执行的Jar
后续补充
可参考:https://docs.spring.io/spring-boot/docs/current/reference/html/getting-started-first-application.html#getting-started-first-application-dependencies