spring boot参考指南之入门(2)

10.1 Installation Instructions for the Java Developer

            java开发者安装说明

You can use Spring Boot in the same way as any standard Java library. To do so, include the appropriate spring-boot-*.jar files on your classpath. Spring Boot does not require any special tools integration, so you can use any IDE or text editor. Also, there is nothing special about a Spring Boot application, so you can run and debug a Spring Boot application as you would any other Java program.

你可以和使用标准的Java库一样使用spring boot,为此,请在你的classpath下包含恰当的spring-boot-*.jar文件。spring boot不需要任何特殊工具集,所以你可以使用任何IDE或者文本编辑器。另外,Spring boot应用没有什么特别之处,所以你可以像运行任何其他任何java程序一样运行和调试spring boot应用程序

Although you could copy Spring Boot jars, we generally recommend that you use a build tool that supports dependency management (such as Maven or Gradle).

虽然你可以复制spring boot的jar,我们一般推荐你使用支持依赖管理的构建工具(如:Maven或者Gradle)

 

10.1.1 Maven Installation

               maven安装

Spring Boot is compatible with Apache Maven 3.3 or above. If you do not already have Maven installed, you can follow the instructions at maven.apache.org.

spring boot 兼容Apache maven 3.3或者更高的版本。如果你没有安装maven,你可以参考在maven.apache.org中的说明。

[Tip]

On many operating systems, Maven can be installed with a package manager. If you use OSX Homebrew, try brew install maven. Ubuntu users can run sudo apt-get install maven. Windows users with Chocolatey can run choco install maven from an elevated (administrator) prompt.

在很多操作系统中,maven都可以与软件包管理器一起安装。如果你使用osx homebrew,请尝试brew 安装maven。ubuntu用户可以运行sudoapt-get命令安装maven。windows用户可以从提速提示符中运行choco安装maven

Spring Boot dependencies use the org.springframework.boot groupId. Typically, your Maven POM file inherits from the spring-boot-starter-parent project and declares dependencies to one or more “Starters”. Spring Boot also provides an optional Maven plugin to create executable jars.

spring boot依赖使用org.springframework.boot的groupId。通常,你的maven pom文件继承自spring-boot-starter-parent工程并且声明一个或者更多的“starter”依赖。spring boot也提供了可选的maven插件去创建可执行jar包。

The following listing shows a typical pom.xml file:

以下列表展示了典型的pox.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.1.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>
[Tip]

The spring-boot-starter-parent is a great way to use Spring Boot, but it might not be suitable all of the time. Sometimes you may need to inherit from a different parent POM, or you might not like our default settings. In those cases, see Section 13.2.2, “Using Spring Boot without the Parent POM” for an alternative solution that uses an import scope.

spring-boot-starter-parent是使用spring boot的很好的方式,但是它可能不适合所有的情况。有时候你也许需要去继承不同的parent pom,或者你不喜欢我们默认的设置。在这些案例中,请看13.2.2节的部分使用spring boot的parent pom来获取import范围的替代解决方案

10.1.2 Gradle Installation

                gradle 安装           

Spring Boot is compatible with Gradle 4.4 and later. If you do not already have Gradle installed, you can follow the instructions at gradle.org.

spring boot兼容Gradle4.4及更高的版本。如果你没有安装Gradle,则可以按照gradle.org上的说明进行操作

Spring Boot dependencies can be declared by using the org.springframework.boot group. Typically, your project declares dependencies to one or more “Starters”. Spring Boot provides a useful Gradle plugin that can be used to simplify dependency declarations and to create executable jars.

spring boot的依赖关系可以通过使用org.springframework.boot group来声明。通常,你的工程可以声明一个或者多个“Starters”依赖。spring boot提供简化依赖声明和创建可执行jars的有用的Gradle 插件。

Gradle Wrapper

Gradle 包装

The Gradle Wrapper provides a nice way of “obtaining” Gradle when you need to build a project. It is a small script and library that you commit alongside your code to bootstrap the build process. See docs.gradle.org/4.2.1/userguide/gradle_wrapper.html for details.

当您需要构建项目时,Gradle Wrapper提供了一种“获取”Gradle的好方法。它是一个小脚本和库,您可以与代码一起提交以引导构建过程。有关详细信息,请参阅docs.gradle.org/4.2.1/userguide/gradle_wrapper.html

More details on getting started with Spring Boot and Gradle can be found in the Getting Started section of the Gradle plugin’s reference guide.

有关Spring Boot和Gradle 入门的更多详细信息 ,请参阅Gradle插件参考指南的“ 入门”部分

10.2.5 Command-line Completion

              命令行结束

The Spring Boot CLI includes scripts that provide command completion for the BASH and zsh shells. You can source the script (also named spring) in any shell or put it in your personal or system-wide bash completion initialization. On a Debian system, the system-wide scripts are in /shell-completion/bash and all scripts in that directory are executed when a new shell starts. For example, to run the script manually if you have installed by using SDKMAN!, use the following commands:

spring boot cll包含提供bash和zsh shell命令完成脚本。你可以获取脚本在任何shell里或者推送它在你的个人的或者全系统bash完成初始化。在debian系统中,全系统脚本在/shell-completion/bash 下并且当一个新的shell开始的时候在这个目录中的所有脚本都会被执行。例如,如果你通过sdkman安装,去手动运行脚本!使用如下的命令:

$ . ~/.sdkman/candidates/springboot/current/shell-completion/bash/spring
$ spring <HIT TAB HERE>
  grab  help  jar  run  test  version
[Note]

If you install the Spring Boot CLI by using Homebrew or MacPorts, the command-line completion scripts are automatically registered with your shell.

如果你安装spring boot cll通过使用homebrew或则macports,命令行完成脚本会自动注册在你的shell里

 

10.2.6 Windows Scoop Installation

              windows scoop安装

If you are on a Windows and use Scoop, you can install the Spring Boot CLI by using the following commands:

如果你使用的是windows并使用scoop,你可以通过以下命令安装spring boot cll:

> scoop bucket add extras
> scoop install springboot

Scoop installs spring to ~/scoop/apps/springboot/current/bin.

scoop安装spring到~/scoop/apps/springboot/current/bin

[Note]

If you do not see the app manifest, your installation of scoop might be out-of-date. In that case, run scoop update and try again.

如果你没有看到app清单,你scoop安装可能过期了,在这种情况下,运行scoop更新并且再试一次。

 

10.2.7 Quick-start Spring CLI Example

              快速开始spring cll示例

You can use the following web application to test your installation. To start, create a file called app.groovy, as follows:

你可以使用以下web应用去测试你的安装。首先,创建一个叫app.groovy的文件,如下所示:

@RestController
class ThisWillActuallyRun {

	@RequestMapping("/")
	String home() {
		"Hello World!"
	}

}

Then run it from a shell, as follows:

然后在shell中运行它,如下所示:·

$ spring run app.groovy
[Note]

The first run of your application is slow, as dependencies are downloaded. Subsequent runs are much quicker.

由于依赖需要下载,第一次运行你的应用会比较慢。随后的运行就会快的多

Open localhost:8080 in your favorite web browser. You should see the following output:

在你最喜欢的web浏览器上打开localhost:8080.你应该能看到以下的输出:

Hello World!

10.3 Upgrading from an Earlier Version of Spring Boot

            从早期的spring boot版本升级

If you are upgrading from an earlier release of Spring Boot, check the “migration guide” on the project wiki that provides detailed upgrade instructions. Check also the“release notes” for a list of “new and noteworthy” features for each release.

如果你从早期的Spring boot发布版升级,查看“migration guide” on the project wiki 可以提供详细的升级说明。也可以查看“release notes” 每个发布版的新的和值得注意的特征的列表。

When upgrading to a new feature release, some properties may have been renamed or removed. Spring Boot provides a way to analyze your application’s environment and print diagnostics at startup, but also temporarily migrate properties at runtime for you. To enable that feature, add the following dependency to your project:

当升级到一个新的发布的特征,一些配置可能被重命名或者移除。spring boot提供一种在启动时去分析你的应用环境并打印诊断的途径,但是也可以在运行时为你临时迁移配置,要启用该特征,添加以下依赖到你的工程中:

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-properties-migrator</artifactId>
	<scope>runtime</scope>
</dependency>
[Warning]

Properties that are added late to the environment, such as when using @PropertySource, will not be taken into account.

添加到环境后期的属性,例如当使用@PropertySource将不能考虑在内 

[Note]

Once you’re done with the migration, please make sure to remove this module from your project’s dependencies.

你完成一次迁移,请确认从你的工程的依赖中移除了这个模块

To upgrade an existing CLI installation, use the appropriate package manager command (for example, brew upgrade) or, if you manually installed the CLI, follow thestandard instructions, remembering to update your PATH environment variable to remove any older references.

去升级存在的cll安装,使用适当的包管理命令,如果你手动安装cll,请按照 标准说明操作,记住更新你的环境变量path 移除任何旧的引用

 

 11 Developing Your First Spring Boot Application

         开发你的第一个spring boot应用

 

This section describes how to develop a simple “Hello World!” web application that highlights some of Spring Boot’s key features. We use Maven to build this project, since most IDEs support it.

这个部分描述怎么去开发一个简单的“Hello World”web程序,该应用程序突出了Spring Boot的一些主要功能。我们使用Maven来构建这个项目,因为大多数IDE都支持它。

[Tip]

The spring.io web site contains many “Getting Started” guides that use Spring Boot. If you need to solve a specific problem, check there first.

spring io网站包含许多使用Springboot“开始”指南

spring.io网站包含了许多使用Spring的“入门” 指南。如果您需要解决特定问题,请先检查一下。

You can shortcut the steps below by going to start.spring.io and choosing the "Web" starter from the dependencies searcher. Doing so generates a new project structure so that you can start coding right away. Check the Spring Initializr documentation for more details.

您可以通过转到start.spring.io并从依赖关系搜索器中选择“Web”启动器来快捷执行以下步骤。这样做会生成一个新的项目结构,以便您可以立即开始编码。有关更多详细信息,请查看Spring Initializr文档

Before we begin, open a terminal and run the following commands to ensure that you have valid versions of Java and Maven installed:

在我们开始之前,打开终端并运行以下命令以确认你安装了有效的java和maven版本

$ java -version
java version "1.8.0_102"
Java(TM) SE Runtime Environment (build 1.8.0_102-b14)
Java HotSpot(TM) 64-Bit Server VM (build 25.102-b14, mixed mode)
$ mvn -v
Apache Maven 3.5.4 (1edded0938998edf8bf061f1ceb3cfdeccf443fe; 2018-06-17T14:33:14-04:00)
Maven home: /usr/local/Cellar/maven/3.3.9/libexec
Java version: 1.8.0_102, vendor: Oracle Corporation
[Note]

This sample needs to be created in its own folder. Subsequent instructions assume that you have created a suitable folder and that it is your current directory.

 

此示例需要在其自己的文件夹中创建。后续说明假定您已创建合适的文件夹,并且它是您当前的目录。

 

11.2 Adding Classpath Dependencies

            添加类路径依赖项

Spring Boot provides a number of “Starters” that let you add jars to your classpath. Our sample application has already used spring-boot-starter-parent in the parent section of the POM. The spring-boot-starter-parent is a special starter that provides useful Maven defaults. It also provides a dependency-managementsection so that you can omit version tags for “blessed” dependencies.

spring boot提供了许多让你添加jar包到你的类路径下的starters。我们的示例应用程序已经在pom文件的父部分使用了spring-boot-starter-parent。spring-boot-starter-parent是一个特殊的提供有用maven默认的启动器。它也提供一个依赖管理部分所以你可以省略“blessed”依赖的版本标签

Other “Starters” provide dependencies that you are likely to need when developing a specific type of application. Since we are developing a web application, we add aspring-boot-starter-web dependency. Before that, we can look at what we currently have by running the following command:

其他的starter提供当你可能开发一个特殊类型的应用程序的依赖。由于我们正在开发一个web应用程序,因此我们添加spring-boot-starter-web依赖。在这之前,我们通过运行以下命令看下我们当前内容:

$ mvn dependency:tree

[INFO] com.example:myproject:jar:0.0.1-SNAPSHOT

The mvn dependency:tree command prints a tree representation of your project dependencies. You can see that spring-boot-starter-parent provides no dependencies by itself. To add the necessary dependencies, edit your pom.xml and add the spring-boot-starter-web dependency immediately below the parentsection:

mvn dependency:tree命令打印一个你工程依赖的树表示。你可以看到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>

If you run mvn dependency:tree again, you see that there are now a number of additional dependencies, including the Tomcat web server and Spring Boot itself.

如果你再次运行mvn dependency:tree,你可以看到有许多其他的依赖项,包括tomcat web server和spring boot本身。

您会发现现在有许多其他依赖项,包括Tomcat Web服务器和Spring Boot本身。

 

11.3.2 The @EnableAutoConfiguration Annotation

                @EnableAutoConfiguration注解

The second class-level annotation is @EnableAutoConfiguration. This annotation tells Spring Boot to “guess” how you want to configure Spring, based on the jar dependencies that you have added. Since spring-boot-starter-web added Tomcat and Spring MVC, the auto-configuration assumes that you are developing a web application and sets up Spring accordingly.

第二类等级注解是@EnableAutoConfiguration.这个注解告诉spring boot去推测你想要配置的spring,基于你添加的jar依赖。因为spring-boot-starter-web添加了tomcat和springmvc,自动配置假定您正在开发Web应用程序并相应地设置Spring。

Starters and Auto-configuration

启动器和自动配置

Auto-configuration is designed to work well with “Starters”, but the two concepts are not directly tied. You are free to pick and choose jar dependencies outside of the starters. Spring Boot still does its best to auto-configure your application.

自动配置旨在与“启动器”配合使用,但是两个概念不是直接相关。你可以自由的去挑选和决定starter之外的jar依赖。

Spring Boot仍然尽力自动配置您的应用程序。

 

11.3.3 The “main” Method

              main方法

The final part of our application is the main method. This is just a standard method that follows the Java convention for an application entry point. Our main method delegates to Spring Boot’s SpringApplication class by calling runSpringApplication bootstraps our application, starting Spring, which, in turn, starts the auto-configured Tomcat web server. We need to pass Example.class as an argument to the run method to tell SpringApplication which is the primary Spring component. The args array is also passed through to expose any command-line arguments.

我们的应用程序的最后一部分是main方法。这只是遵循应用程序入口点的Java约定的标准方法。我们的main方法SpringApplication通过调用委托给Spring Boot的类run。 SpringApplication引导我们的应用程序,启动Spring,然后启动自动配置的Tomcat Web服务器。我们需要Example.class作为参数传递给run方法,以告诉SpringApplication哪个是主要的Spring组件。该 args数组也被传递以公开任何命令行参数。

 

11.4 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:

此时此刻你的应用应该能工作了。由于你使用了spring-boot-starter-parent pom,你有一个有用的运行目标,你可以用来启动你的应用程序。运行mvn spring-boot:run在根目录来启动你的程序。你应该能看到类似于以下内容的输出:

$ mvn spring-boot:run

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::  (v2.1.1.RELEASE)
....... . . .
....... . . . (log output here)
....... . . .
........ Started Example in 2.222 seconds (JVM running for 6.514)

If you open a web browser to localhost:8080, you should see the following output:

如果你打开web浏览器输入localhost:8080,你可以看到如下输出:

Hello World!

To gracefully exit the application, press ctrl-c.

优雅的退出应用程序,请按ctrl -c

 

11.5 Creating an Executable Jar

          创建一个可执行jar

We finish our example by creating a completely self-contained executable jar file that we could run in production. Executable jars (sometimes called “fat jars”) are archives containing your compiled classes along with all of the jar dependencies that your code needs to run.

我们通过创建一个完全自包含的可执行jar文件来完成我们的示例,我们可以在生产中运行它。可执行jar(有时称为“fat jar”)是包含已编译类以及代码需要运行的所有jar依赖项的归档。

 

Executable jars and Java

可执行jar和java

Java does not provide a standard way to load nested jar files (jar files that are themselves contained within a jar). This can be problematic if you are looking to distribute a self-contained application.

Java没有提供加载嵌套jar文件的标准方法(jar文件本身包含在jar中)。如果您要分发自包含的应用程序,这可能会有问题。

To solve this problem, many developers use “uber” jars. An uber jar packages all the classes from all the application’s dependencies into a single archive. The problem with this approach is that it becomes hard to see which libraries are in your application. It can also be problematic if the same filename is used (but with different content) in multiple jars.

为了解决这个问题,许多开发人员使用“超级”罐子。uber jar将所有应用程序依赖项中的所有类打包到一个存档中。这种方法的问题在于很难看出应用程序中有哪些库。如果在多个罐子中使用相同的文件名(但具有不同的内容),也可能会有问题。

 

Spring Boot takes a different approach and lets you actually nest jars directly.

Spring Boot采用不同的方法,让你直接嵌套jar。

To create an executable jar, we need to add the spring-boot-maven-plugin to our pom.xml. To do so, insert the following lines just below the dependenciessection:

要创建可执行jar,我们需要添加spring-boot-maven-plugin到我们的 pom.xml。为此,请在该dependencies部分下方插入以下行:

<build>
	<plugins>
		<plugin>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-maven-plugin</artifactId>
		</plugin>
	</plugins>
</build>
[Note]

The spring-boot-starter-parent POM includes <executions> configuration to bind the repackage goal. If you do not use the parent POM, you need to declare this configuration yourself. See the plugin documentation for details.

spring-boot-starter-parent pom包含<executions>配置去绑定重新包装的目标。如果您不使用父POM,则需要自己声明此配置。有关详细信息,请参阅插件文档

Save your pom.xml and run mvn package from the command line, as follows:

保存pom.xml并从命令行运行mvn package,如下所示:

$ mvn package

[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building myproject 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] .... ..
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ myproject ---
[INFO] Building jar: /Users/developer/example/spring-boot-example/target/myproject-0.0.1-SNAPSHOT.jar
[INFO]
[INFO] --- spring-boot-maven-plugin:2.1.1.RELEASE:repackage (default) @ myproject ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------

If you look in the target directory, you should see myproject-0.0.1-SNAPSHOT.jar. The file should be around 10 MB in size. If you want to peek inside, you can use jar tvf, as follows:

如果你看了target目录,你应该能看见myproject-0.0.1-SNAPSHOT.jar.这个文件应该有10Mb大小。如果你想看里面有什么,你可以使用jar tvf,如下:

$ jar tvf target/myproject-0.0.1-SNAPSHOT.jar

You should also see a much smaller file named myproject-0.0.1-SNAPSHOT.jar.original in the target directory. This is the original jar file that Maven created before it was repackaged by Spring Boot.

你应该也能看见非常小的名为myproject-0.0.1-SNAPSHOT.jar.original文件在target目录中。这是Maven在Spring Boot重新打包之前创建的原始jar文件。

要运行该应用程序,请使用以下java -jar命令:

To run that application, use the java -jar command, as follows:

运行应用程序,使用java -jar命令,如下:

$ java -jar target/myproject-0.0.1-SNAPSHOT.jar

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::  (v2.1.1.RELEASE)
....... . . .
....... . . . (log output here)
....... . . .
........ Started Example in 2.536 seconds (JVM running for 2.864)

As before, to exit the application, press ctrl-c.

和以前一样,要退出应用程序,请按ctrl-c

12. What to Read Next

         

     接下来要阅读的内容

 

Hopefully, this section provided some of the Spring Boot basics and got you on your way to writing your own applications. If you are a task-oriented type of developer, you might want to jump over to spring.io and check out some of the getting started guides that solve specific “How do I do that with Spring?” problems. We also have Spring Boot-specific “How-to” reference documentation.

希望这个部分提供spring boot基本的知识并且让您开始编写自己的应用程序。如果你是一个面向任务类型的开发者,你可能想跳过spring-io并查看一些入门指南,这些指南解决了特定的“我怎么使用spring”的问题。我们也有特定于spring-boot的“how-to”的参考文档

The Spring Boot repository also has a bunch of samples you can run. The samples are independent of the rest of the code (that is, you do not need to build the rest to run or use the samples).

spring boot库中也有一些示例可以运行。示例是独立其余的代码(也就是说,您无需构建其余代码来运行或使用示例)

Otherwise, the next logical step is to read Part III, “Using Spring Boot”. If you are really impatient, you could also jump ahead and read about Spring Boot features.

否则,下一个逻辑步骤是阅读第III部分“使用Spring Boot”。如果你真的很不耐烦,你也可以跳过去阅读有关 Spring Boot功能的内容

Part IX. ‘How-to’ guides

                        怎么去做指南

This section provides answers to some common ‘how do I do that…​’ questions that often arise when using Spring Boot. Its coverage is not exhaustive, but it does cover quite a lot.

本节提供了使用Spring Boot时经常出现的一些常见“我该怎么做......”的问题的答案。它的报道并不详尽,但确实涵盖了很多。

If you have a specific problem that we do not cover here, you might want to check out stackoverflow.com to see if someone has already provided an answer. This is also a great place to ask new questions (please use the spring-boot tag).

如果您遇到我们未在此处讨论的特定问题,您可能需要查看 stackoverflow.com以查看是否有人已提供答案。这也是提出新问题的好地方(请使用spring-boot标签)。

We are also more than happy to extend this section. If you want to add a ‘how-to’, send us a pull request.

我们也非常乐意扩展这一部分。如果您想添加“操作方法”,请向我们发送拉取请求

Spring Boot Application

spring boot 应用程序

This section includes topics relating directly to Spring Boot applications.

本节包括与Spring Boot应用程序直接相关的主题。

76.1 Create Your Own FailureAnalyzer

            创建你自己的失败分析器

FailureAnalyzer is a great way to intercept an exception on startup and turn it into a human-readable message, wrapped in a FailureAnalysis. Spring Boot provides such an analyzer for application-context-related exceptions, JSR-303 validations, and more. You can also create your own.

FailureAnalyzer是一种在启动时拦截异常并将其转换为人类可读消息的好方法,包含在FailureAnalysis。Spring Boot为应用程序上下文相关异常,JSR-303验证等提供了这样的分析器。您也可以创建自己的。

AbstractFailureAnalyzer is a convenient extension of FailureAnalyzer that checks the presence of a specified exception type in the exception to handle. You can extend from that so that your implementation gets a chance to handle the exception only when it is actually present. If, for whatever reason, you cannot handle the exception, return null to give another implementation a chance to handle the exception.

FailureAnalyzer implementations must be registered in META-INF/spring.factories. The following example registers ProjectConstraintViolationFailureAnalyzer:

org.springframework.boot.diagnostics.FailureAnalyzer=\
com.example.ProjectConstraintViolationFailureAnalyzer
[Note]

If you need access to the BeanFactory or the Environment, your FailureAnalyzer can simply implement BeanFactoryAware or EnvironmentAware respectively.

77.1.1 Automatic Property Expansion Using Maven

              使用Maven自动扩展属性

You can automatically expand properties from the Maven project by using resource filtering. If you use the spring-boot-starter-parent, you can then refer to your Maven ‘project properties’ with @..@ placeholders, as shown in the following example:

您可以使用资源过滤从Maven项目自动扩展属性。如果使用spring-boot-starter-parent,则可以使用@..@占位符引用Maven“项目属性” ,如以下示例所示:

app.encoding=@project.build.sourceEncoding@
app.java.version=@java.version@
[Note]

Only production configuration is filtered that way (in other words, no filtering is applied on src/test/resources).

只有生产配置以这种方式过滤(换句话说,不应用过滤src/test/resources)。

 

[Tip]

If you enable the addResources flag, the spring-boot:run goal can add src/main/resources directly to the classpath (for hot reloading purposes). Doing so circumvents the resource filtering and this feature. Instead, you can use the exec:java goal or customize the plugin’s configuration. See theplugin usage page for more details.

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值