Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0e6a496

Browse files
committed
Add spring-boot-features part
1 parent d591592 commit 0e6a496

File tree

4 files changed

+132
-9
lines changed

4 files changed

+132
-9
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22

33
![Spring Boot](https://spring.io/img/homepage/icon-spring-boot.svg)
44

5-
Spring Boot 是构建所有基于 Spring 应用的起点,旨在使开发者能以最小化配置快速启动和运行应用
5+
使用 Spring Boot,您只需极少的配置就能轻松构建出基于 Spring 的独立,生产级别应用程序
66

7-
该项目为 Spring Boot 文档翻译项目,由本人个人发起,基于 [Spring Boot 2.0.0](https://docs.spring.io/spring-boot/docs/2.0.0.RELEASE/reference/htmlsingle/) 的官方文档进行翻译。
7+
该项目为 Spring Boot 文档翻译项目,由本人个人发起,基于 [Spring Boot 2.x](https://spring.io/projects/spring-boot) 的官方文档进行翻译。
88

99
## 阅读方式
1010

11-
[Github](https://github.com/DocsHome/springboot/blob/master/SUMMARY.md) | [Gitbook](https://www.gitbook.com/book/docshome/springboot) | PDF | MOBI
11+
[Github](https://github.com/DocsHome/springboot/blob/master/SUMMARY.md) | [Gitbook](https://www.gitbook.com/book/docshome/springboot)
1212

1313
## 项目状态
1414

SUMMARY.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,9 @@
7777
- [20.5、远程应用](pages/using-spring-boot.md#using-boot-devtools-remote)
7878
- [20.5.1、运行远程客户端应用](pages/using-spring-boot.md#_running_the_remote_client_application)
7979
- [20.5.2、远程更新](pages/using-spring-boot.md#using-boot-devtools-remote-update)
80-
80+
- [21、打包生产应用](pages/using-spring-boot.md#using-boot-packaging-for-production)
81+
- [21、下一步](pages/using-spring-boot.md#using-boot-whats-next)
82+
- [四、Spring Boot 功能](pages/spring-boot-features.md#boot-features)
83+
- [23、SpringApplication](pages/spring-boot-features.md#boot-features-spring-application)
84+
- [23.1、启动失败](pages/spring-boot-features.md#boot-features-startup-failure)
85+
- [23.2、自定义 banner](pages/spring-boot-features.md#boot-features-banner)

pages/spring-boot-features.md

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
<a id="boot-features"></a>
2+
3+
# 四、Spring Boot 功能
4+
5+
本部分将介绍 Spring Boot 相关的细节内容。在这里,您可以学习到可能需要使用和自定义的主要功能。您如果还没有做好充分准备,可能需要阅读[第二部分:入门](#getting-started)[第三部分:使用 Spring Boot](#using-boot),以便打下前期基础。
6+
7+
<a id="boot-features-spring-application"></a>
8+
9+
## 23、SpringApplication
10+
11+
`SpringApplication` 类提供了一种可通过运行 `main()` 方法来启动 Spring 应用的简单方式。多数情况下,您只需要委托给静态的 `SpringApplication.run` 方法:
12+
13+
```java
14+
public static void main(String[] args) {
15+
SpringApplication.run(MySpringConfiguration.class, args);
16+
}
17+
```
18+
19+
当应用启动时,您应该会看到类似以下的内容输出:
20+
21+
```
22+
. ____ _ __ _ _
23+
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
24+
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
25+
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
26+
' |____| .__|_| |_|_| |_\__, | / / / /
27+
=========|_|==============|___/=/_/_/_/
28+
:: Spring Boot :: v2.1.1.RELEASE
29+
30+
2013-07-31 00:08:16.117 INFO 56603 --- [ main] o.s.b.s.app.SampleApplication : Starting SampleApplication v0.1.0 on mycomputer with PID 56603 (/apps/myapp.jar started by pwebb)
31+
2013-07-31 00:08:16.166 INFO 56603 --- [ main] ationConfigServletWebServerApplicationContext : Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@6e5a8246: startup date [Wed Jul 31 00:08:16 PDT 2013]; root of context hierarchy
32+
2014-03-04 13:09:54.912 INFO 41370 --- [ main] .t.TomcatServletWebServerFactory : Server initialized with port: 8080
33+
2014-03-04 13:09:56.501 INFO 41370 --- [ main] o.s.b.s.app.SampleApplication : Started SampleApplication in 2.992 seconds (JVM running for 3.658)
34+
```
35+
36+
默认情况下,将显示 `INFO` 级别的日志信息,包括一些应用启动相关信息。如果您需要修改 `INFO` 日志级别,请参考 [26.4 部分:日志等级](#boot-features-custom-log-levels)
37+
38+
<a id="boot-features-startup-failure"></a>
39+
40+
### 23.1、启动失败
41+
42+
如果您的应用无法启动,注册的 `FailureAnalyzers` 可能会提供有相关的错误信息和解决问题的具体方法。例如,如果您在已经被占用的 `8080` 端口上启动了一个 web 应用,会看到类似以下的错误信息:
43+
44+
```
45+
***************************
46+
APPLICATION FAILED TO START
47+
***************************
48+
49+
Description:
50+
51+
Embedded servlet container failed to start. Port 8080 was already in use.
52+
53+
Action:
54+
55+
Identify and stop the process that's listening on port 8080 or configure this application to listen on another port.
56+
```
57+
58+
**注意**
59+
60+
> Spring Boot 提供了许多的 `FailureAnalyzer` 实现,您也可以[添加自己的实现](#howto-failure-analyzer)
61+
62+
如果没有失败分析器能够处理的异常,您仍然可以显示完整的条件报告以便更好地了解出现的问题。为此,您需要针对 `org.springframework.boot.autoconfigure.logging.ConditionEvaluationReportLoggingListener` [启用 `debug` 属性](#boot-features-external-config)或者[开启 `DEBUG` 日志](#boot-features-custom-log-levels)
63+
64+
例如,如果您使用 `java -jar` 运行应用,可以按以下方式启用 `debug` 属性:
65+
66+
```
67+
$ java -jar myproject-0.0.1-SNAPSHOT.jar --debug
68+
```
69+
70+
<a id="boot-features-banner"></a>
71+
72+
### 23.2、自定义 banner
73+
74+
可以通过在 classpath 下添加一个 `banner.txt` 文件,或者将 `spring.banner.location` 属性指向该文件的位置来更改启动时打印的 banner。如果文件采用了非 UTF-8 编码,您可以设置 `spring.banner.charset` 来解决。除了文本文件,您还可以将 `banner.gif``banner.jpg` 或者 `banner.png` 图片文件添加到 classpath 下,或者设置 `spring.banner.image.location` 属性。指定的图片将会被转换成 ASCII 形式并打印在 banner 文本上方。
75+
76+
您可以在 `banner.txt` 文件中使用以下占位符:
77+
78+
| 变量 | 描述 |
79+
| ---- | ---- |
80+
| `${application.version}` | 您的应用版本号,声明在 `MANIFEST.MF` 中。例如,`Implementation-Version: 1.0` 将被打印为 `1.0`|
81+
| `${application.formatted-version}` | 您的应用版本号,声明在 `MANIFEST.MF` 中,格式化之后打印(用括号括起来,以 `v` 为前缀),例如 (`v1.0`)。 |
82+
| `${spring-boot.version}` | 您使用的 Spring Boot 版本。例如 `2.1.1.RELEASE.`|
83+
| `${spring-boot.formatted-version}` | 你使用的 Spring Boot 版本格式化之后显示(用括号括起来,以 `v` 为前缀)。例如 (`v2.1.1.RELEASE`)。 |
84+
| `${Ansi.NAME}`(或 `${AnsiColor.NAME}`、<br/>`${AnsiBackground.NAME}`、<br/>`${AnsiStyle.NAME}`| 其中 `NAME` 是 ANSI 转义码的名称。有关详细信息,请参阅 [AnsiPropertySource](https://github.com/spring-projects/spring-boot/tree/v2.1.1.RELEASE/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/ansi/AnsiPropertySource.java)|
85+
| `${application.title}` | 您的应用标题,声明在 `MANIFEST.MF` 中,例如 `Implementation-Title: MyApp` 打印为 `MyApp`|
86+
87+
88+
**提示**
89+
90+
> 如果您想以编程的方式生成 banner,可以使用 `SpringApplication.setBanner(​...)` 方法。使用 `org.springframework.boot.Banner` 接口并实现自己的 `printBanner()` 方法。
91+
92+
您还可以使用 `spring.main.banner-mode` 属性来确定是否必须在 `System.out``console`)上打印 banner,还是使用日志记录器(`log`)或者都不打印(`off`)。
93+
94+
打印的 banner 被注册名为 `springBootBanner` 的单例 bean。
95+
96+
**注意**
97+
98+
> YAML 将 `off` 映射为 `false`,因此如果要禁用应用程序 banner,请确保属性添加引号。
99+
100+
```yaml
101+
spring:
102+
main:
103+
banner-mode: "off"
104+
```
105+
106+
**待续……**

pages/using-spring-boot.md

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -878,15 +878,15 @@ spring.devtools.remote.secret=mysecret
878878

879879
**注意**
880880

881-
由于远程客户端与实际应用使用的是同一个 classpath,因此可以直接读取应用的 properties。这也是 `spring.devtools.remote.secret` 属性为什么能被读取和传递给服务器进行身份验证的原因。
881+
> 由于远程客户端与实际应用使用的是同一个 classpath,因此可以直接读取应用的 properties。这也是 `spring.devtools.remote.secret` 属性为什么能被读取和传递给服务器进行身份验证的原因。
882882
883883
**提示**
884884

885-
建议使用 `https://` 作为连接协议,以便加密传输并防止密码被拦截。
885+
> 建议使用 `https://` 作为连接协议,以便加密传输并防止密码被拦截。
886886
887887
**提示**
888888

889-
如果您需要通过代理来访问远程应用,请配置 `spring.devtools.remote.proxy.host``spring.devtools.remote.proxy.port` 属性。
889+
> 如果您需要通过代理来访问远程应用,请配置 `spring.devtools.remote.proxy.host``spring.devtools.remote.proxy.port` 属性。
890890
891891
<a id="using-boot-devtools-remote-update"></a>
892892

@@ -896,6 +896,18 @@ spring.devtools.remote.secret=mysecret
896896

897897
**注意**
898898

899-
文件只有在远程客户端运行时才被监控。如果您在启动远程客户端之前更改了文件,文件将不会被推送到远程服务器。
899+
> 文件只有在远程客户端运行时才被监控。如果您在启动远程客户端之前更改了文件,文件将不会被推送到远程服务器。
900900
901-
**待续……**
901+
<a id="using-boot-packaging-for-production"></a>
902+
903+
## 21、打包生产应用
904+
905+
可执行 jar 可用于生产部署,它们是独立(self-contained,独立、自包含)的,同样也适合云部署。
906+
907+
针对其他**生产就绪**功能,比如健康、审计和 REST 或者 JMX 端点度量,可以添加 `spring-boot-actuator`。有关这方面的详细信息,请参见 [第五部分:“Spring Boot Actuator:生产就绪功能”](#production-ready)
908+
909+
<a id="using-boot-whats-next"></a>
910+
911+
## 21、下一步
912+
913+
您现在应该知道如何使用 Spring Boot 以及应该遵循哪些最佳实践。接下来您可以深入地了解 [Spring Boot 功能](#boot-features),或者您也可以跳过下一部分直接阅读[“生产就绪功能”](h#production-ready)方面的内容。

0 commit comments

Comments
 (0)