* pr/10892: Format production-ready-features.adoc to 90 chars Format spring-boot-features.adoc to 90 chars Format using-spring-boot.adoc to 90 chars Format getting-started.adoc to 90 chars
This commit is contained in:
commit
200eb8f5b5
|
@ -4,19 +4,20 @@
|
||||||
[partintro]
|
[partintro]
|
||||||
--
|
--
|
||||||
If you are getting started with Spring Boot, or "Spring" in general, start by reading
|
If you are getting started with Spring Boot, or "Spring" in general, start by reading
|
||||||
this section. It answers the basic "`what?`", "`how?`" and "`why?`" questions. It includes
|
this section. It answers the basic "`what?`", "`how?`" and "`why?`" questions. It
|
||||||
an introduction to Spring Boot, along with installation instructions.
|
includes an introduction to Spring Boot, along with installation instructions. We then
|
||||||
We then walk you through building your first Spring Boot application, discussing some core
|
walk you through building your first Spring Boot application, discussing some core
|
||||||
principles as we go.
|
principles as we go.
|
||||||
--
|
--
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
[[getting-started-introducing-spring-boot]]
|
[[getting-started-introducing-spring-boot]]
|
||||||
== Introducing Spring Boot
|
== Introducing Spring Boot
|
||||||
Spring Boot makes it easy to create stand-alone, production-grade Spring based
|
Spring Boot makes it easy to create stand-alone, production-grade Spring based
|
||||||
Applications that you can run. We take an opinionated view of the Spring
|
Applications that you can run. We take an opinionated view of the Spring platform and
|
||||||
platform and third-party libraries, so that you can get started with minimum fuss. Most
|
third-party libraries, so that you can get started with minimum fuss. Most Spring Boot
|
||||||
Spring Boot applications need very little Spring configuration.
|
applications need very little Spring configuration.
|
||||||
|
|
||||||
You can use Spring Boot to create Java applications that can be started by using
|
You can use Spring Boot to create Java applications that can be started by using
|
||||||
`java -jar` or more traditional war deployments. We also provide a command line tool that
|
`java -jar` or more traditional war deployments. We also provide a command line tool that
|
||||||
|
@ -38,8 +39,9 @@ configuration).
|
||||||
[[getting-started-system-requirements]]
|
[[getting-started-system-requirements]]
|
||||||
== System Requirements
|
== System Requirements
|
||||||
Spring Boot {spring-boot-version} requires http://www.java.com[Java 8] and Spring
|
Spring Boot {spring-boot-version} requires http://www.java.com[Java 8] and Spring
|
||||||
Framework {spring-version} or above. Explicit build support is provided for Maven
|
Framework {spring-version} or above. Explicit build support is provided for Maven 3.2+
|
||||||
3.2+, and Gradle 4.
|
and Gradle 4.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
[[getting-started-system-requirements-servlet-containers]]
|
[[getting-started-system-requirements-servlet-containers]]
|
||||||
|
@ -74,8 +76,8 @@ begin, you should check your current Java installation by using the following co
|
||||||
$ java -version
|
$ java -version
|
||||||
----
|
----
|
||||||
|
|
||||||
If you are new to Java development or if you want to experiment with Spring Boot,
|
If you are new to Java development or if you want to experiment with Spring Boot, you
|
||||||
you might want to try the <<getting-started-installing-the-cli, Spring Boot CLI>> (Command
|
might want to try the <<getting-started-installing-the-cli, Spring Boot CLI>> (Command
|
||||||
Line Interface) first, otherwise, read on for "`classic`" installation instructions.
|
Line Interface) first, otherwise, read on for "`classic`" installation instructions.
|
||||||
|
|
||||||
|
|
||||||
|
@ -85,28 +87,28 @@ Line Interface) first, otherwise, read on for "`classic`" installation instructi
|
||||||
You can use Spring Boot in the same way as any standard Java library. To do so, include
|
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
|
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
|
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
|
is nothing special about a Spring Boot application, so you can run and debug a Spring
|
||||||
application as you would any other Java program.
|
Boot application as you would any other Java program.
|
||||||
|
|
||||||
Although you _could_ copy Spring Boot jars, we generally recommend that you use a
|
Although you _could_ copy Spring Boot jars, we generally recommend that you use a build
|
||||||
build tool that supports dependency management (such as Maven or Gradle).
|
tool that supports dependency management (such as Maven or Gradle).
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
[[getting-started-maven-installation]]
|
[[getting-started-maven-installation]]
|
||||||
==== Maven Installation
|
==== Maven Installation
|
||||||
Spring Boot is compatible with Apache Maven 3.2 or above. If you do not already have Maven
|
Spring Boot is compatible with Apache Maven 3.2 or above. If you do not already have
|
||||||
installed, you can follow the instructions at http://maven.apache.org.
|
Maven installed, you can follow the instructions at http://maven.apache.org.
|
||||||
|
|
||||||
TIP: On many operating systems, Maven can be installed with a package manager. If you
|
TIP: On many operating systems, Maven can be installed with a package manager. If you use
|
||||||
use OSX Homebrew, try `brew install maven`. Ubuntu users can run
|
OSX Homebrew, try `brew install maven`. Ubuntu users can run
|
||||||
`sudo apt-get install maven`. Windows users with Chocolatey can run `choco install maven`
|
`sudo apt-get install maven`. Windows users with Chocolatey can run `choco install maven`
|
||||||
from an elevated (administrator) prompt.
|
from an elevated (administrator) prompt.
|
||||||
|
|
||||||
Spring Boot dependencies use the `org.springframework.boot` `groupId`. Typically, your
|
Spring Boot dependencies use the `org.springframework.boot` `groupId`. Typically, your
|
||||||
Maven POM file inherits from the `spring-boot-starter-parent` project and declares
|
Maven POM file inherits from the `spring-boot-starter-parent` project and declares
|
||||||
dependencies to one or more <<using-spring-boot.adoc#using-boot-starter,
|
dependencies to one or more <<using-spring-boot.adoc#using-boot-starter,"`Starters`">>.
|
||||||
"`Starters`">>. Spring Boot also provides an optional
|
Spring Boot also provides an optional
|
||||||
<<build-tool-plugins.adoc#build-tool-plugins-maven-plugin, Maven plugin>> to create
|
<<build-tool-plugins.adoc#build-tool-plugins-maven-plugin, Maven plugin>> to create
|
||||||
executable jars.
|
executable jars.
|
||||||
|
|
||||||
|
@ -176,9 +178,9 @@ endif::[]
|
||||||
</project>
|
</project>
|
||||||
----
|
----
|
||||||
|
|
||||||
TIP: The `spring-boot-starter-parent` is a great way to use Spring Boot, but it might
|
TIP: The `spring-boot-starter-parent` is a great way to use Spring Boot, but it might not
|
||||||
not be suitable all of the time. Sometimes you may need to inherit from a different
|
be suitable all of the time. Sometimes you may need to inherit from a different parent
|
||||||
parent POM, or you might not like our default settings. In those cases, see
|
POM, or you might not like our default settings. In those cases, see
|
||||||
<<using-boot-maven-without-a-parent>> for an alternative solution that uses an `import`
|
<<using-boot-maven-without-a-parent>> for an alternative solution that uses an `import`
|
||||||
scope.
|
scope.
|
||||||
|
|
||||||
|
@ -199,11 +201,11 @@ jars.
|
||||||
.Gradle Wrapper
|
.Gradle Wrapper
|
||||||
****
|
****
|
||||||
The Gradle Wrapper provides a nice way of "`obtaining`" Gradle when you need to build a
|
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
|
project. It is a small script and library that you commit alongside your code to
|
||||||
the build process. See {gradle-user-guide}/gradle_wrapper.html for details.
|
bootstrap the build process. See {gradle-user-guide}/gradle_wrapper.html for details.
|
||||||
****
|
****
|
||||||
|
|
||||||
Here is a typical `build.gradle` file:
|
The following example shows a typical `build.gradle` file:
|
||||||
|
|
||||||
[source,groovy,indent=0,subs="verbatim,attributes"]
|
[source,groovy,indent=0,subs="verbatim,attributes"]
|
||||||
----
|
----
|
||||||
|
@ -257,8 +259,8 @@ The Spring Boot CLI (Command Line Interface) is a command line tool that you can
|
||||||
quickly prototype with Spring. It lets you run http://groovy-lang.org/[Groovy] scripts,
|
quickly prototype with Spring. It lets you run http://groovy-lang.org/[Groovy] scripts,
|
||||||
which means that you have a familiar Java-like syntax without so much boilerplate code.
|
which means that you have a familiar Java-like syntax without so much boilerplate code.
|
||||||
|
|
||||||
You do not need to use the CLI to work with Spring Boot, but it is definitely the quickest
|
You do not need to use the CLI to work with Spring Boot, but it is definitely the
|
||||||
way to get a Spring application off the ground.
|
quickest way to get a Spring application off the ground.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -269,10 +271,12 @@ You can download the Spring CLI distribution from the Spring software repository
|
||||||
* http://repo.spring.io/{spring-boot-repo}/org/springframework/boot/spring-boot-cli/{spring-boot-version}/spring-boot-cli-{spring-boot-version}-bin.zip[spring-boot-cli-{spring-boot-version}-bin.zip]
|
* http://repo.spring.io/{spring-boot-repo}/org/springframework/boot/spring-boot-cli/{spring-boot-version}/spring-boot-cli-{spring-boot-version}-bin.zip[spring-boot-cli-{spring-boot-version}-bin.zip]
|
||||||
* http://repo.spring.io/{spring-boot-repo}/org/springframework/boot/spring-boot-cli/{spring-boot-version}/spring-boot-cli-{spring-boot-version}-bin.tar.gz[spring-boot-cli-{spring-boot-version}-bin.tar.gz]
|
* http://repo.spring.io/{spring-boot-repo}/org/springframework/boot/spring-boot-cli/{spring-boot-version}/spring-boot-cli-{spring-boot-version}-bin.tar.gz[spring-boot-cli-{spring-boot-version}-bin.tar.gz]
|
||||||
|
|
||||||
Cutting edge http://repo.spring.io/snapshot/org/springframework/boot/spring-boot-cli/[snapshot
|
Cutting edge
|
||||||
|
http://repo.spring.io/snapshot/org/springframework/boot/spring-boot-cli/[snapshot
|
||||||
distributions] are also available.
|
distributions] are also available.
|
||||||
|
|
||||||
Once downloaded, follow the {github-raw}/spring-boot-project/spring-boot-cli/src/main/content/INSTALL.txt[INSTALL.txt]
|
Once downloaded, follow the
|
||||||
|
{github-raw}/spring-boot-project/spring-boot-cli/src/main/content/INSTALL.txt[INSTALL.txt]
|
||||||
instructions from the unpacked archive. In summary, there is a `spring` script
|
instructions from the unpacked archive. In summary, there is a `spring` script
|
||||||
(`spring.bat` for Windows) in a `bin/` directory in the `.zip` file. Alternatively, you
|
(`spring.bat` for Windows) in a `bin/` directory in the `.zip` file. Alternatively, you
|
||||||
can use `java -jar` with the `.jar` file (the script helps you to be sure that the
|
can use `java -jar` with the `.jar` file (the script helps you to be sure that the
|
||||||
|
@ -282,9 +286,10 @@ classpath is set correctly).
|
||||||
|
|
||||||
[[getting-started-sdkman-cli-installation]]
|
[[getting-started-sdkman-cli-installation]]
|
||||||
==== Installation with SDKMAN!
|
==== Installation with SDKMAN!
|
||||||
SDKMAN! (The Software Development Kit Manager) can be used for managing multiple versions of
|
SDKMAN! (The Software Development Kit Manager) can be used for managing multiple versions
|
||||||
various binary SDKs, including Groovy and the Spring Boot CLI.
|
of various binary SDKs, including Groovy and the Spring Boot CLI.
|
||||||
Get SDKMAN! from http://sdkman.io and install Spring Boot by using the following commands:
|
Get SDKMAN! from http://sdkman.io and install Spring Boot by using the following
|
||||||
|
commands:
|
||||||
|
|
||||||
[indent=0,subs="verbatim,quotes,attributes"]
|
[indent=0,subs="verbatim,quotes,attributes"]
|
||||||
----
|
----
|
||||||
|
@ -293,8 +298,8 @@ Get SDKMAN! from http://sdkman.io and install Spring Boot by using the following
|
||||||
Spring Boot v{spring-boot-version}
|
Spring Boot v{spring-boot-version}
|
||||||
----
|
----
|
||||||
|
|
||||||
If you are developing features for the CLI and want easy access to the version you
|
If you are developing features for the CLI and want easy access to the version you built,
|
||||||
built, use the following commands:
|
use the following commands:
|
||||||
|
|
||||||
[indent=0,subs="verbatim,quotes,attributes"]
|
[indent=0,subs="verbatim,quotes,attributes"]
|
||||||
----
|
----
|
||||||
|
@ -304,9 +309,9 @@ built, use the following commands:
|
||||||
Spring CLI v{spring-boot-version}
|
Spring CLI v{spring-boot-version}
|
||||||
----
|
----
|
||||||
|
|
||||||
The preceding instructions install a local instance of `spring` called the `dev` instance.
|
The preceding instructions install a local instance of `spring` called the `dev`
|
||||||
It points at your target build location, so every time you rebuild Spring
|
instance. It points at your target build location, so every time you rebuild Spring Boot,
|
||||||
Boot, `spring` is up-to-date.
|
`spring` is up-to-date.
|
||||||
|
|
||||||
You can see it by running the following command:
|
You can see it by running the following command:
|
||||||
|
|
||||||
|
@ -331,8 +336,8 @@ You can see it by running the following command:
|
||||||
|
|
||||||
[[getting-started-homebrew-cli-installation]]
|
[[getting-started-homebrew-cli-installation]]
|
||||||
==== OSX Homebrew Installation
|
==== OSX Homebrew Installation
|
||||||
If you are on a Mac and use http://brew.sh/[Homebrew], you can install
|
If you are on a Mac and use http://brew.sh/[Homebrew], you can install the Spring Boot
|
||||||
the Spring Boot CLI by using the following commands:
|
CLI by using the following commands:
|
||||||
|
|
||||||
[indent=0]
|
[indent=0]
|
||||||
----
|
----
|
||||||
|
@ -342,15 +347,15 @@ the Spring Boot CLI by using the following commands:
|
||||||
|
|
||||||
Homebrew installs `spring` to `/usr/local/bin`.
|
Homebrew installs `spring` to `/usr/local/bin`.
|
||||||
|
|
||||||
NOTE: If you do not see the formula, your installation of brew might be out-of-date.
|
NOTE: If you do not see the formula, your installation of brew might be out-of-date. In
|
||||||
In that case, run `brew update` and try again.
|
that case, run `brew update` and try again.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
[[getting-started-macports-cli-installation]]
|
[[getting-started-macports-cli-installation]]
|
||||||
==== MacPorts Installation
|
==== MacPorts Installation
|
||||||
If you are on a Mac and use http://www.macports.org/[MacPorts], you can
|
If you are on a Mac and use http://www.macports.org/[MacPorts], you can install the
|
||||||
install the Spring Boot CLI by using the following command:
|
Spring Boot CLI by using the following command:
|
||||||
|
|
||||||
[indent=0]
|
[indent=0]
|
||||||
----
|
----
|
||||||
|
@ -361,8 +366,8 @@ install the Spring Boot CLI by using the following command:
|
||||||
|
|
||||||
[[getting-started-cli-command-line-completion]]
|
[[getting-started-cli-command-line-completion]]
|
||||||
==== Command-line Completion
|
==== Command-line Completion
|
||||||
The Spring Boot CLI includes scripts that provide command completion for
|
The Spring Boot CLI includes scripts that provide command completion for the
|
||||||
the http://en.wikipedia.org/wiki/Bash_%28Unix_shell%29[BASH] and
|
http://en.wikipedia.org/wiki/Bash_%28Unix_shell%29[BASH] and
|
||||||
http://en.wikipedia.org/wiki/Zsh[zsh] shells. You can `source` the script (also named
|
http://en.wikipedia.org/wiki/Zsh[zsh] shells. You can `source` the script (also named
|
||||||
`spring`) in any shell or put it in your personal or system-wide bash completion
|
`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
|
initialization. On a Debian system, the system-wide scripts are in
|
||||||
|
@ -384,8 +389,8 @@ completion scripts are automatically registered with your shell.
|
||||||
|
|
||||||
[[getting-started-cli-example]]
|
[[getting-started-cli-example]]
|
||||||
==== Quick-start Spring CLI Example
|
==== Quick-start Spring CLI Example
|
||||||
You can use the following web application to test your installation. To start, create
|
You can use the following web application to test your installation. To start, create a
|
||||||
a file called `app.groovy`, as follows:
|
file called `app.groovy`, as follows:
|
||||||
|
|
||||||
[source,groovy,indent=0,subs="verbatim,quotes,attributes"]
|
[source,groovy,indent=0,subs="verbatim,quotes,attributes"]
|
||||||
----
|
----
|
||||||
|
@ -407,8 +412,8 @@ Then run it from a shell, as follows:
|
||||||
$ spring run app.groovy
|
$ spring run app.groovy
|
||||||
----
|
----
|
||||||
|
|
||||||
NOTE: The first run of your application is slow, as dependencies are
|
NOTE: The first run of your application is slow, as dependencies are downloaded.
|
||||||
downloaded. Subsequent runs are much quicker.
|
Subsequent runs are much quicker.
|
||||||
|
|
||||||
Open http://localhost:8080 in your favorite web browser. You should see the following
|
Open http://localhost:8080 in your favorite web browser. You should see the following
|
||||||
output:
|
output:
|
||||||
|
@ -423,13 +428,13 @@ output:
|
||||||
[[getting-started-upgrading-from-an-earlier-version]]
|
[[getting-started-upgrading-from-an-earlier-version]]
|
||||||
=== Upgrading from an Earlier Version of Spring Boot
|
=== Upgrading from an Earlier Version of Spring Boot
|
||||||
If you are upgrading from an earlier release of Spring Boot check the "`release notes`"
|
If you are upgrading from an earlier release of Spring Boot check the "`release notes`"
|
||||||
hosted on the {github-wiki}[project wiki]. You'll find upgrade instructions along with
|
hosted on the {github-wiki}[project wiki]. You'll find upgrade instructions along with a
|
||||||
a list of "`new and noteworthy`" features for each release.
|
list of "`new and noteworthy`" features for each release.
|
||||||
|
|
||||||
To upgrade an existing CLI installation use the appropriate package manager command
|
To upgrade an existing CLI installation use the appropriate package manager command (for
|
||||||
(for example `brew upgrade`) or, if you manually installed the CLI, follow the
|
example, `brew upgrade`) or, if you manually installed the CLI, follow the
|
||||||
<<getting-started-manual-cli-installation, standard instructions>> remembering to
|
<<getting-started-manual-cli-installation, standard instructions>> remembering to update
|
||||||
update your `PATH` environment variable to remove any older references.
|
your `PATH` environment variable to remove any older references.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -441,14 +446,15 @@ most IDEs support it.
|
||||||
|
|
||||||
[TIP]
|
[TIP]
|
||||||
====
|
====
|
||||||
The http://spring.io[spring.io] web site contains many "`Getting Started`" http://spring.io/guides[guides]
|
The http://spring.io[spring.io] web site contains many "`Getting Started`"
|
||||||
that use Spring Boot. If you need to solve a specific problem, check there first.
|
http://spring.io/guides[guides] that use Spring Boot. If you need to solve a specific
|
||||||
|
problem, check there first.
|
||||||
|
|
||||||
You can shortcut the steps below by going to https://start.spring.io and choosing the
|
You can shortcut the steps below by going to https://start.spring.io and choosing the
|
||||||
"Web" starter from the dependencies searcher. Doing so generates a new
|
"Web" starter from the dependencies searcher. Doing so generates a new project structure
|
||||||
project structure so that you can <<getting-started-first-application-code,start coding
|
so that you can <<getting-started-first-application-code,start coding right away>>. Check
|
||||||
right away>>. Check the https://github.com/spring-io/initializr[Spring Initializr
|
the https://github.com/spring-io/initializr[Spring Initializr documentation] for more
|
||||||
documentation] for more details.
|
details.
|
||||||
====
|
====
|
||||||
|
|
||||||
Before we begin, open a terminal and run the following commands to ensure that you have
|
Before we begin, open a terminal and run the following commands to ensure that you have
|
||||||
|
@ -477,8 +483,8 @@ that you have created a suitable folder and that it is your "`current directory`
|
||||||
|
|
||||||
[[getting-started-first-application-pom]]
|
[[getting-started-first-application-pom]]
|
||||||
=== Creating the POM
|
=== Creating the POM
|
||||||
We need to start by creating a Maven `pom.xml` file. The `pom.xml` is the recipe that
|
We need to start by creating a Maven `pom.xml` file. The `pom.xml` is the recipe that is
|
||||||
is used to build your project. Open your favorite text editor and add the following:
|
used to build your project. Open your favorite text editor and add the following:
|
||||||
|
|
||||||
[source,xml,indent=0,subs="verbatim,quotes,attributes"]
|
[source,xml,indent=0,subs="verbatim,quotes,attributes"]
|
||||||
----
|
----
|
||||||
|
@ -526,29 +532,29 @@ endif::[]
|
||||||
</project>
|
</project>
|
||||||
----
|
----
|
||||||
|
|
||||||
The preceding listing should give you a working build. You can test it by running
|
The preceding listing should give you a working build. You can test it by running `mvn
|
||||||
`mvn package` (for now, you can ignore the "`jar will be empty - no content was marked for
|
package` (for now, you can ignore the "`jar will be empty - no content was marked for
|
||||||
inclusion!`" warning).
|
inclusion!`" warning).
|
||||||
|
|
||||||
NOTE: At this point, you could import the project into an IDE (most modern Java IDEs
|
NOTE: At this point, you could import the project into an IDE (most modern Java IDEs
|
||||||
include built-in support for Maven). For simplicity, we continue to use a plain
|
include built-in support for Maven). For simplicity, we continue to use a plain text
|
||||||
text editor for this example.
|
editor for this example.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
[[getting-started-first-application-dependencies]]
|
[[getting-started-first-application-dependencies]]
|
||||||
=== Adding Classpath Dependencies
|
=== Adding Classpath Dependencies
|
||||||
Spring Boot provides a number of "`Starters`" that let you add jars to your
|
Spring Boot provides a number of "`Starters`" that let you add jars to your classpath.
|
||||||
classpath. Our sample application has already used `spring-boot-starter-parent` in the
|
Our sample application has already used `spring-boot-starter-parent` in the `parent`
|
||||||
`parent` section of the POM. The `spring-boot-starter-parent` is a special starter
|
section of the POM. The `spring-boot-starter-parent` is a special starter that provides
|
||||||
that provides useful Maven defaults. It also provides a
|
useful Maven defaults. It also provides a
|
||||||
<<using-spring-boot.adoc#using-boot-dependency-management,`dependency-management`>>
|
<<using-spring-boot.adoc#using-boot-dependency-management,`dependency-management`>>
|
||||||
section so that you can omit `version` tags for "`blessed`" dependencies.
|
section so that you can omit `version` tags for "`blessed`" dependencies.
|
||||||
|
|
||||||
Other "`Starters`" provide dependencies that you are likely to need when
|
Other "`Starters`" provide dependencies that you are likely to need when developing a
|
||||||
developing a specific type of application. Since we are developing a web application, we
|
specific type of application. Since we are developing a web application, we add a
|
||||||
add a `spring-boot-starter-web` dependency. Before that, we can look at what we
|
`spring-boot-starter-web` dependency. Before that, we can look at what we currently have
|
||||||
currently have by running the following command:
|
by running the following command:
|
||||||
|
|
||||||
[indent=0]
|
[indent=0]
|
||||||
----
|
----
|
||||||
|
@ -558,8 +564,8 @@ currently have by running the following command:
|
||||||
----
|
----
|
||||||
|
|
||||||
The `mvn dependency:tree` command prints a tree representation of your project
|
The `mvn dependency:tree` command prints a tree representation of your project
|
||||||
dependencies. You can see that `spring-boot-starter-parent` provides no
|
dependencies. You can see that `spring-boot-starter-parent` provides no dependencies by
|
||||||
dependencies by itself. To add the necessary dependencies, edit your `pom.xml` and add the
|
itself. To add the necessary dependencies, edit your `pom.xml` and add the
|
||||||
`spring-boot-starter-web` dependency immediately below the `parent` section:
|
`spring-boot-starter-web` dependency immediately below the `parent` section:
|
||||||
|
|
||||||
[source,xml,indent=0,subs="verbatim,quotes,attributes"]
|
[source,xml,indent=0,subs="verbatim,quotes,attributes"]
|
||||||
|
@ -572,8 +578,8 @@ dependencies by itself. To add the necessary dependencies, edit your `pom.xml` a
|
||||||
</dependencies>
|
</dependencies>
|
||||||
----
|
----
|
||||||
|
|
||||||
If you run `mvn dependency:tree` again, you see that there are now a number of
|
If you run `mvn dependency:tree` again, you see that there are now a number of additional
|
||||||
additional dependencies, including the Tomcat web server and Spring Boot itself.
|
dependencies, including the Tomcat web server and Spring Boot itself.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -617,10 +623,10 @@ _stereotype_ annotation. It provides hints for people reading the code and for S
|
||||||
that the class plays a specific role. In this case, our class is a web `@Controller`, so
|
that the class plays a specific role. In this case, our class is a web `@Controller`, so
|
||||||
Spring considers it when handling incoming web requests.
|
Spring considers it when handling incoming web requests.
|
||||||
|
|
||||||
The `@RequestMapping` annotation provides "`routing`" information. It tells Spring
|
The `@RequestMapping` annotation provides "`routing`" information. It tells Spring that
|
||||||
that any HTTP request with the `/` path should be mapped to the `home` method. The
|
any HTTP request with the `/` path should be mapped to the `home` method. The
|
||||||
`@RestController` annotation tells Spring to render the resulting string directly
|
`@RestController` annotation tells Spring to render the resulting string directly back to
|
||||||
back to the caller.
|
the caller.
|
||||||
|
|
||||||
TIP: The `@RestController` and `@RequestMapping` annotations are Spring MVC annotations.
|
TIP: The `@RestController` and `@RequestMapping` annotations are Spring MVC annotations.
|
||||||
(They are not specific to Spring Boot.) See the {spring-reference}web.html#mvc[MVC
|
(They are not specific to Spring Boot.) See the {spring-reference}web.html#mvc[MVC
|
||||||
|
@ -631,15 +637,15 @@ section] in the Spring Reference Documentation for more details.
|
||||||
[[getting-started-first-application-auto-configuration]]
|
[[getting-started-first-application-auto-configuration]]
|
||||||
==== The @EnableAutoConfiguration Annotation
|
==== The @EnableAutoConfiguration Annotation
|
||||||
The second class-level annotation is `@EnableAutoConfiguration`. This annotation tells
|
The second class-level annotation is `@EnableAutoConfiguration`. This annotation tells
|
||||||
Spring Boot to "`guess`" how you want to configure Spring, based on the jar
|
Spring Boot to "`guess`" how you want to configure Spring, based on the jar dependencies
|
||||||
dependencies that you have added. Since `spring-boot-starter-web` added Tomcat and
|
that you have added. Since `spring-boot-starter-web` added Tomcat and Spring MVC, the
|
||||||
Spring MVC, the auto-configuration assumes that you are developing a web application
|
auto-configuration assumes that you are developing a web application and sets up Spring
|
||||||
and sets up Spring accordingly.
|
accordingly.
|
||||||
|
|
||||||
.Starters and Auto-Configuration
|
.Starters and Auto-Configuration
|
||||||
****
|
****
|
||||||
Auto-configuration is designed to work well with "`Starters`", but the two concepts
|
Auto-configuration is designed to work well with "`Starters`", but the two concepts are
|
||||||
are not directly tied. You are free to pick-and-choose jar dependencies outside of the
|
not directly tied. You are free to pick and choose jar dependencies outside of the
|
||||||
starters and Spring Boot still does its best to auto-configure your application.
|
starters and Spring Boot still does its best to auto-configure your application.
|
||||||
****
|
****
|
||||||
|
|
||||||
|
@ -648,12 +654,12 @@ starters and Spring Boot still does its best to auto-configure your application.
|
||||||
[[getting-started-first-application-main-method]]
|
[[getting-started-first-application-main-method]]
|
||||||
==== The "`main`" Method
|
==== The "`main`" Method
|
||||||
The final part of our application is the `main` method. This is just a standard method
|
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
|
that follows the Java convention for an application entry point. Our main method
|
||||||
to Spring Boot's `SpringApplication` class by calling `run`. `SpringApplication`
|
delegates to Spring Boot's `SpringApplication` class by calling `run`.
|
||||||
bootstraps our application, starting Spring, which, in turn, starts the auto-configured
|
`SpringApplication` bootstraps our application, starting Spring, which, in turn, starts
|
||||||
Tomcat web server. We need to pass `Example.class` as an argument to the `run` method to
|
the auto-configured Tomcat web server. We need to pass `Example.class` as an argument to
|
||||||
tell `SpringApplication` which is the primary Spring component. The `args` array is also
|
the `run` method to tell `SpringApplication` which is the primary Spring component. The
|
||||||
passed through to expose any command-line arguments.
|
`args` array is also passed through to expose any command-line arguments.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -705,10 +711,10 @@ Java does not provide a standard way to load nested jar files (jar files that ar
|
||||||
themselves contained within a jar). This can be problematic if you are looking to
|
themselves contained within a jar). This can be problematic if you are looking to
|
||||||
distribute a self-contained application.
|
distribute a self-contained application.
|
||||||
|
|
||||||
To solve this problem, many developers use "`uber`" jars. An uber jar packages
|
To solve this problem, many developers use "`uber`" jars. An uber jar packages all the
|
||||||
all the classes from all the application's dependencies into a single archive. The problem
|
classes from all the application's dependencies into a single archive. The problem with
|
||||||
with this approach is that it becomes hard to see which libraries are in your application.
|
this approach is that it becomes hard to see which libraries are in your application. It
|
||||||
It can also be problematic if the same filename is used (but with different content) in
|
can also be problematic if the same filename is used (but with different content) in
|
||||||
multiple jars.
|
multiple jars.
|
||||||
|
|
||||||
Spring Boot takes a <<appendix-executable-jar-format.adoc#executable-jar, different
|
Spring Boot takes a <<appendix-executable-jar-format.adoc#executable-jar, different
|
||||||
|
@ -730,9 +736,9 @@ To create an executable jar, we need to add the `spring-boot-maven-plugin` to ou
|
||||||
</build>
|
</build>
|
||||||
----
|
----
|
||||||
|
|
||||||
NOTE: The `spring-boot-starter-parent` POM includes `<executions>` configuration to
|
NOTE: The `spring-boot-starter-parent` POM includes `<executions>` configuration to bind
|
||||||
bind the `repackage` goal. If you do not use the parent POM, you need to declare
|
the `repackage` goal. If you do not use the parent POM, you need to declare this
|
||||||
this configuration yourself. See the {spring-boot-maven-plugin-site}/usage.html[plugin
|
configuration yourself. See the {spring-boot-maven-plugin-site}/usage.html[plugin
|
||||||
documentation] for details.
|
documentation] for details.
|
||||||
|
|
||||||
Save your `pom.xml` and run `mvn package` from the command line, as follows:
|
Save your `pom.xml` and run `mvn package` from the command line, as follows:
|
||||||
|
@ -757,16 +763,16 @@ Save your `pom.xml` and run `mvn package` from the command line, as follows:
|
||||||
----
|
----
|
||||||
|
|
||||||
If you look in the `target` directory, you should see `myproject-0.0.1-SNAPSHOT.jar`. The
|
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
|
file should be around 10 MB in size. If you want to peek inside, you can use `jar tvf`,
|
||||||
follows:
|
as follows:
|
||||||
|
|
||||||
[indent=0]
|
[indent=0]
|
||||||
----
|
----
|
||||||
$ jar tvf target/myproject-0.0.1-SNAPSHOT.jar
|
$ 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`
|
You should also see a much smaller file named `myproject-0.0.1-SNAPSHOT.jar.original` in
|
||||||
in the `target` directory. This is the original jar file that Maven created before it was
|
the `target` directory. This is the original jar file that Maven created before it was
|
||||||
repackaged by Spring Boot.
|
repackaged by Spring Boot.
|
||||||
|
|
||||||
To run that application, use the `java -jar` command, as follows:
|
To run that application, use the `java -jar` command, as follows:
|
||||||
|
@ -794,17 +800,17 @@ As before, to exit the application, press `ctrl-c`.
|
||||||
|
|
||||||
[[getting-started-whats-next]]
|
[[getting-started-whats-next]]
|
||||||
== What to Read Next
|
== What to Read Next
|
||||||
Hopefully, this section provided some of the Spring Boot basics and got you
|
Hopefully, this section provided some of the Spring Boot basics and got you on your way
|
||||||
on your way to writing your own applications. If you are a task-oriented type of
|
to writing your own applications. If you are a task-oriented type of developer, you might
|
||||||
developer, you might want to jump over to http://spring.io and check out some of the
|
want to jump over to http://spring.io and check out some of the
|
||||||
http://spring.io/guides/[getting started] guides that solve specific
|
http://spring.io/guides/[getting started] guides that solve specific "`How do I do that
|
||||||
"`How do I do that with Spring?`" problems. We also have Spring Boot-specific
|
with Spring?`" problems. We also have Spring Boot-specific
|
||||||
_<<howto.adoc#howto, How-to>>_ reference documentation.
|
"`<<howto.adoc#howto, How-to>>`" reference documentation.
|
||||||
|
|
||||||
The http://github.com/{github-repo}[Spring Boot repository] also has a
|
The http://github.com/{github-repo}[Spring Boot repository] also has a
|
||||||
{github-code}/spring-boot-samples[bunch of samples] you can run. The samples are
|
{github-code}/spring-boot-samples[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
|
independent of the rest of the code (that is, you do not need to build the rest to run or
|
||||||
or use the samples).
|
use the samples).
|
||||||
|
|
||||||
Otherwise, the next logical step is to read _<<using-spring-boot.adoc#using-boot>>_. If
|
Otherwise, the next logical step is to read _<<using-spring-boot.adoc#using-boot>>_. If
|
||||||
you are really impatient, you could also jump ahead and read about
|
you are really impatient, you could also jump ahead and read about
|
||||||
|
|
|
@ -57,10 +57,10 @@ Actuator endpoints let you monitor and interact with your application. Spring Bo
|
||||||
includes a number of built-in endpoints and lets you add your own. For example, the
|
includes a number of built-in endpoints and lets you add your own. For example, the
|
||||||
`health` endpoint provides basic application health information.
|
`health` endpoint provides basic application health information.
|
||||||
|
|
||||||
The way that endpoints are exposed depends on the type of technology that you choose. Most
|
The way that endpoints are exposed depends on the type of technology that you choose.
|
||||||
applications choose HTTP monitoring, where the ID of the endpoint along with a prefix of
|
Most applications choose HTTP monitoring, where the ID of the endpoint along with a
|
||||||
`/application` is mapped to a URL. For example, by default, the `health` endpoint is
|
prefix of `/application` is mapped to a URL. For example, by default, the `health`
|
||||||
mapped to `/application/health`.
|
endpoint is mapped to `/application/health`.
|
||||||
|
|
||||||
The following technology-agnostic endpoints are available:
|
The following technology-agnostic endpoints are available:
|
||||||
|
|
||||||
|
@ -171,8 +171,8 @@ user authentication. When '`Spring Security`' is added, by default, '`basic`'
|
||||||
authentication is used. The username is`user` and the password is a random generated
|
authentication is used. The username is`user` and the password is a random generated
|
||||||
password (which is printed on the console when the application starts).
|
password (which is printed on the console when the application starts).
|
||||||
|
|
||||||
TIP: Generated passwords are logged as the application starts. To find the password in the
|
TIP: Generated passwords are logged as the application starts. To find the password in
|
||||||
console, search for '`Using default security password`'.
|
the console, search for '`Using default security password`'.
|
||||||
|
|
||||||
You can use Spring properties to change the username and password and to change the
|
You can use Spring properties to change the username and password and to change the
|
||||||
security role(s) required to access the endpoints. For example, you might set the
|
security role(s) required to access the endpoints. For example, you might set the
|
||||||
|
@ -187,8 +187,8 @@ following properties in your `application.properties`:
|
||||||
|
|
||||||
If your application has custom security configuration and you want all your actuator
|
If your application has custom security configuration and you want all your actuator
|
||||||
endpoints to be accessible without authentication, you need to explicitly configure that
|
endpoints to be accessible without authentication, you need to explicitly configure that
|
||||||
in your security configuration. Also, you need to change the `management.security.enabled`
|
in your security configuration. Also, you need to change the
|
||||||
property to `false`.
|
`management.security.enabled` property to `false`.
|
||||||
|
|
||||||
If your custom security configuration secures your actuator endpoints, you also need to
|
If your custom security configuration secures your actuator endpoints, you also need to
|
||||||
ensure that the authenticated user has the roles specified under
|
ensure that the authenticated user has the roles specified under
|
||||||
|
@ -203,11 +203,11 @@ additional role check.
|
||||||
|
|
||||||
[[production-ready-customizing-endpoints]]
|
[[production-ready-customizing-endpoints]]
|
||||||
=== Customizing Endpoints
|
=== Customizing Endpoints
|
||||||
Endpoints can be customized by using Spring properties. You can change whether an endpoint
|
Endpoints can be customized by using Spring properties. You can change whether an
|
||||||
is `enabled` and its `id`.
|
endpoint is `enabled` and its `id`.
|
||||||
|
|
||||||
For example, the following `application.properties` changes the id of the `beans` endpoint
|
For example, the following `application.properties` changes the id of the `beans`
|
||||||
and also enables `shutdown`:
|
endpoint and also enables `shutdown`:
|
||||||
|
|
||||||
[source,properties,indent=0]
|
[source,properties,indent=0]
|
||||||
----
|
----
|
||||||
|
@ -218,9 +218,9 @@ and also enables `shutdown`:
|
||||||
NOTE: The prefix ‟`endpoints` + `.` + `name`” is used to uniquely identify the endpoint
|
NOTE: The prefix ‟`endpoints` + `.` + `name`” is used to uniquely identify the endpoint
|
||||||
that is being configured.
|
that is being configured.
|
||||||
|
|
||||||
By default, all endpoints except for `shutdown` are enabled. If you prefer to specifically
|
By default, all endpoints except for `shutdown` are enabled. If you prefer to
|
||||||
"`opt-in`" endpoint enablement, you can use the `endpoints.default.enabled` property. For
|
specifically "`opt-in`" endpoint enablement, you can use the `endpoints.default.enabled`
|
||||||
example, the following settings disables _all_ endpoints except for `info`:
|
property. For example, the following settings disables _all_ endpoints except for `info`:
|
||||||
|
|
||||||
[source,properties,indent=0]
|
[source,properties,indent=0]
|
||||||
----
|
----
|
||||||
|
@ -272,29 +272,29 @@ application, over HTTP as well.
|
||||||
|
|
||||||
TIP: If you do this as a library feature, consider adding a configuration class annotated
|
TIP: If you do this as a library feature, consider adding a configuration class annotated
|
||||||
with `@ManagementContextConfiguration` to `/META-INF/spring.factories` under the key,
|
with `@ManagementContextConfiguration` to `/META-INF/spring.factories` under the key,
|
||||||
`org.springframework.boot.actuate.autoconfigure.ManagementContextConfiguration`. If you do
|
`org.springframework.boot.actuate.autoconfigure.ManagementContextConfiguration`. If you
|
||||||
so and if your users ask for a separate management port or address, the endpoint moves to
|
do so and if your users ask for a separate management port or address, the endpoint moves
|
||||||
a child context with all the other web endpoints.
|
to a child context with all the other web endpoints.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
[[production-ready-health]]
|
[[production-ready-health]]
|
||||||
=== Health Information
|
=== Health Information
|
||||||
You can use health information to check the status of your running application. It is
|
You can use health information to check the status of your running application. It is
|
||||||
often used by monitoring software to alert someone when a production system goes down. The
|
often used by monitoring software to alert someone when a production system goes down.
|
||||||
default information exposed by the `health` endpoint depends on how it is accessed. For an
|
The default information exposed by the `health` endpoint depends on how it is accessed.
|
||||||
unauthenticated connection in a secure application, a simple '`status`' message is
|
For an unauthenticated connection in a secure application, a simple '`status`' message is
|
||||||
returned. For an authenticated connection, additional details are also displayed. (See
|
returned. For an authenticated connection, additional details are also displayed. (See
|
||||||
<<production-ready-health-access-restrictions>> for HTTP details.)
|
<<production-ready-health-access-restrictions>> for HTTP details.)
|
||||||
|
|
||||||
Health information is collected from all
|
Health information is collected from all
|
||||||
{sc-spring-boot-actuator}/health/HealthIndicator.{sc-ext}[`HealthIndicator`] beans defined
|
{sc-spring-boot-actuator}/health/HealthIndicator.{sc-ext}[`HealthIndicator`] beans
|
||||||
in your `ApplicationContext`. Spring Boot includes a number of auto-configured
|
defined in your `ApplicationContext`. Spring Boot includes a number of auto-configured
|
||||||
`HealthIndicators`, and you can also write your own. By default, the final system state is
|
`HealthIndicators`, and you can also write your own. By default, the final system state
|
||||||
derived by the `HealthAggregator`, which sorts the statuses from each `HealthIndicator`
|
is derived by the `HealthAggregator`, which sorts the statuses from each
|
||||||
based on an ordered list of statuses. The first status in the sorted list is used as the
|
`HealthIndicator` based on an ordered list of statuses. The first status in the sorted
|
||||||
overall health status. If no `HealthIndicator` returns a status that is known to the
|
list is used as the overall health status. If no `HealthIndicator` returns a status that
|
||||||
`HealthAggregator`, an `UNKNOWN` status is used.
|
is known to the `HealthAggregator`, an `UNKNOWN` status is used.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -373,12 +373,13 @@ NOTE: The identifier for a given `HealthIndicator` is the name of the bean witho
|
||||||
`HealthIndicator` suffix, if it exists. In the preceding example, the health information
|
`HealthIndicator` suffix, if it exists. In the preceding example, the health information
|
||||||
is available in an entry named `my`.
|
is available in an entry named `my`.
|
||||||
|
|
||||||
In addition to Spring Boot's predefined {sc-spring-boot-actuator}/health/Status.{sc-ext}[`Status`]
|
In addition to Spring Boot's predefined
|
||||||
types, it is also possible for `Health` to return a custom `Status` that represents a new
|
{sc-spring-boot-actuator}/health/Status.{sc-ext}[`Status`] types, it is also possible for
|
||||||
system state. In such cases, a custom implementation of the
|
`Health` to return a custom `Status` that represents a new system state. In such cases, a
|
||||||
|
custom implementation of the
|
||||||
{sc-spring-boot-actuator}/health/HealthAggregator.{sc-ext}[`HealthAggregator`] interface
|
{sc-spring-boot-actuator}/health/HealthAggregator.{sc-ext}[`HealthAggregator`] interface
|
||||||
also needs to be provided, or the default implementation has to be configured by using the
|
also needs to be provided, or the default implementation has to be configured by using
|
||||||
`management.health.status.order` configuration property.
|
the `management.health.status.order` configuration property.
|
||||||
|
|
||||||
For example, assume a new `Status` with code `FATAL` is being used in one of your
|
For example, assume a new `Status` with code `FATAL` is being used in one of your
|
||||||
`HealthIndicator` implementations. To configure the severity order, add the following
|
`HealthIndicator` implementations. To configure the severity order, add the following
|
||||||
|
@ -389,8 +390,8 @@ to your application properties:
|
||||||
management.health.status.order=FATAL, DOWN, OUT_OF_SERVICE, UNKNOWN, UP
|
management.health.status.order=FATAL, DOWN, OUT_OF_SERVICE, UNKNOWN, UP
|
||||||
----
|
----
|
||||||
|
|
||||||
The HTTP status code in the response reflects the overall health status (for example, `UP`
|
The HTTP status code in the response reflects the overall health status (for example,
|
||||||
maps to 200, while `OUT_OF_SERVICE` and `DOWN` map to 503). You might also want to
|
`UP` maps to 200, while `OUT_OF_SERVICE` and `DOWN` map to 503). You might also want to
|
||||||
register custom status mappings if you access the health endpoint over HTTP. For example,
|
register custom status mappings if you access the health endpoint over HTTP. For example,
|
||||||
the following property maps `FATAL` to 503 (service unavailable):
|
the following property maps `FATAL` to 503 (service unavailable):
|
||||||
|
|
||||||
|
@ -454,8 +455,8 @@ property.
|
||||||
[[production-ready-application-info-env]]
|
[[production-ready-application-info-env]]
|
||||||
==== Custom Application Information
|
==== Custom Application Information
|
||||||
You can customize the data exposed by the `info` endpoint by setting `+info.*+` Spring
|
You can customize the data exposed by the `info` endpoint by setting `+info.*+` Spring
|
||||||
properties. All `Environment` properties under the info key are automatically exposed. For
|
properties. All `Environment` properties under the info key are automatically exposed.
|
||||||
example, you could add the following settings to your `application.properties` file:
|
For example, you could add the following settings to your `application.properties` file:
|
||||||
|
|
||||||
[source,properties,indent=0]
|
[source,properties,indent=0]
|
||||||
----
|
----
|
||||||
|
@ -485,8 +486,8 @@ Assuming you use Maven, you could rewrite the preceding example as follows:
|
||||||
==== Git Commit Information
|
==== Git Commit Information
|
||||||
Another useful feature of the `info` endpoint is its ability to publish information about
|
Another useful feature of the `info` endpoint is its ability to publish information about
|
||||||
the state of your `git` source code repository when the project was built. If a
|
the state of your `git` source code repository when the project was built. If a
|
||||||
`GitProperties` bean is available, the `git.branch`, `git.commit.id` and `git.commit.time`
|
`GitProperties` bean is available, the `git.branch`, `git.commit.id` and
|
||||||
properties are exposed.
|
`git.commit.time` properties are exposed.
|
||||||
|
|
||||||
TIP: A `GitProperties` bean is auto-configured if a `git.properties` file is available at
|
TIP: A `GitProperties` bean is auto-configured if a `git.properties` file is available at
|
||||||
the root of the classpath. See
|
the root of the classpath. See
|
||||||
|
@ -504,9 +505,9 @@ If you want to display the full git information (that is, the full content of
|
||||||
|
|
||||||
[[production-ready-application-info-build]]
|
[[production-ready-application-info-build]]
|
||||||
==== Build Information
|
==== Build Information
|
||||||
If a `BuildProperties` bean is available, the `info` endpoint can also publish information
|
If a `BuildProperties` bean is available, the `info` endpoint can also publish
|
||||||
about your build. This happens if a `META-INF/build-info.properties` file is available in
|
information about your build. This happens if a `META-INF/build-info.properties` file is
|
||||||
the classpath.
|
available in the classpath.
|
||||||
|
|
||||||
TIP: The Maven and Gradle plugins can both generate that file. See
|
TIP: The Maven and Gradle plugins can both generate that file. See
|
||||||
"<<howto.adoc#howto-build-info,Generate build information>>" for more details.
|
"<<howto.adoc#howto-build-info,Generate build information>>" for more details.
|
||||||
|
@ -564,10 +565,10 @@ exposed as `/application/health`.
|
||||||
|
|
||||||
[[production-ready-customizing-management-server-context-path]]
|
[[production-ready-customizing-management-server-context-path]]
|
||||||
=== Customizing the Management Endpoint Paths
|
=== Customizing the Management Endpoint Paths
|
||||||
Sometimes, it is useful to customize the prefix for the management endpoints. For example,
|
Sometimes, it is useful to customize the prefix for the management endpoints. For
|
||||||
your application might already use `/application` for another purpose. You can use the
|
example, your application might already use `/application` for another purpose. You can
|
||||||
`management.endpoints.web.base-path` property to change the prefix for your management
|
use the `management.endpoints.web.base-path` property to change the prefix for your
|
||||||
endpoint, as shown in the following example:
|
management endpoint, as shown in the following example:
|
||||||
|
|
||||||
[source,properties,indent=0]
|
[source,properties,indent=0]
|
||||||
----
|
----
|
||||||
|
@ -591,18 +592,18 @@ Exposing management endpoints by using the default HTTP port is a sensible choic
|
||||||
cloud based deployments. If, however, your application runs inside your own data center,
|
cloud based deployments. If, however, your application runs inside your own data center,
|
||||||
you may prefer to expose endpoints by using a different HTTP port.
|
you may prefer to expose endpoints by using a different HTTP port.
|
||||||
|
|
||||||
You can set the `management.server.port` property to change the HTTP port, as shown in the
|
You can set the `management.server.port` property to change the HTTP port, as shown in
|
||||||
following example:
|
the following example:
|
||||||
|
|
||||||
[source,properties,indent=0]
|
[source,properties,indent=0]
|
||||||
----
|
----
|
||||||
management.server.port=8081
|
management.server.port=8081
|
||||||
----
|
----
|
||||||
|
|
||||||
Since your management port is often protected by a firewall and not exposed to the public,
|
Since your management port is often protected by a firewall and not exposed to the
|
||||||
you might not need security on the management endpoints, even if your main application is
|
public, you might not need security on the management endpoints, even if your main
|
||||||
secure. In that case, you should have Spring Security on the classpath, and you can
|
application is secure. In that case, you should have Spring Security on the classpath,
|
||||||
disable management security as follows:
|
and you can disable management security as follows:
|
||||||
|
|
||||||
[source,properties,indent=0]
|
[source,properties,indent=0]
|
||||||
----
|
----
|
||||||
|
@ -682,10 +683,10 @@ If you do not want to expose endpoints over HTTP, you can set the management por
|
||||||
|
|
||||||
[[production-ready-health-access-restrictions]]
|
[[production-ready-health-access-restrictions]]
|
||||||
=== HTTP Health Endpoint Format and Access Restrictions
|
=== HTTP Health Endpoint Format and Access Restrictions
|
||||||
The information exposed by the health endpoint varies, depending on whether it is accessed
|
The information exposed by the health endpoint varies, depending on whether it is
|
||||||
anonymously and whether the enclosing application is secure. By default, when accessed
|
accessed anonymously and whether the enclosing application is secure. By default, when
|
||||||
anonymously in a secure application, any details about the server's health are hidden and
|
accessed anonymously in a secure application, any details about the server's health are
|
||||||
the endpoint indicates whether the server is up or down.
|
hidden and the endpoint indicates whether the server is up or down.
|
||||||
|
|
||||||
The following example shows a summarized HTTP response (default for anonymous request):
|
The following example shows a summarized HTTP response (default for anonymous request):
|
||||||
|
|
||||||
|
@ -745,8 +746,8 @@ The following example shows a detailed HTTP response:
|
||||||
[[production-ready-jmx]]
|
[[production-ready-jmx]]
|
||||||
== Monitoring and Management over JMX
|
== Monitoring and Management over JMX
|
||||||
Java Management Extensions (JMX) provide a standard mechanism to monitor and manage
|
Java Management Extensions (JMX) provide a standard mechanism to monitor and manage
|
||||||
applications. By default, Spring Boot exposes management endpoints as JMX MBeans under the
|
applications. By default, Spring Boot exposes management endpoints as JMX MBeans under
|
||||||
`org.springframework.boot` domain.
|
the `org.springframework.boot` domain.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -831,9 +832,9 @@ If you use Jolokia but do not want Spring Boot to configure it, set the
|
||||||
== Loggers
|
== Loggers
|
||||||
Spring Boot Actuator includes the ability to view and configure the log levels of your
|
Spring Boot Actuator includes the ability to view and configure the log levels of your
|
||||||
application at runtime. You can view either the entire list or an individual logger's
|
application at runtime. You can view either the entire list or an individual logger's
|
||||||
configuration, which is made up of both the explicitly configured logging level as well as
|
configuration, which is made up of both the explicitly configured logging level as well
|
||||||
the effective logging level given to it by the logging framework. These levels can be one
|
as the effective logging level given to it by the logging framework. These levels can be
|
||||||
of:
|
one of:
|
||||||
|
|
||||||
* `TRACE`
|
* `TRACE`
|
||||||
* `DEBUG`
|
* `DEBUG`
|
||||||
|
@ -879,9 +880,9 @@ monitoring systems:
|
||||||
- https://prometheus.io[Prometheus]
|
- https://prometheus.io[Prometheus]
|
||||||
|
|
||||||
Micrometer provides a separate module for each supported monitoring system. Depending on
|
Micrometer provides a separate module for each supported monitoring system. Depending on
|
||||||
one (or more) of these modules is sufficient to get started with Micrometer in your Spring
|
one (or more) of these modules is sufficient to get started with Micrometer in your
|
||||||
Boot application. To learn more about Micrometer's capabilities, please refer to its
|
Spring Boot application. To learn more about Micrometer's capabilities, please refer to
|
||||||
https://micrometer.io/docs[reference documentation].
|
its https://micrometer.io/docs[reference documentation].
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -889,8 +890,8 @@ https://micrometer.io/docs[reference documentation].
|
||||||
=== Spring MVC Metrics
|
=== Spring MVC Metrics
|
||||||
Auto-configuration enables the instrumentation of requests handled by Spring MVC. When
|
Auto-configuration enables the instrumentation of requests handled by Spring MVC. When
|
||||||
`spring.metrics.web.server.auto-time-requests` is `true`, this instrumentation occurs for
|
`spring.metrics.web.server.auto-time-requests` is `true`, this instrumentation occurs for
|
||||||
all requests. Alternatively, when set to `false`, you can enable instrumentation by adding
|
all requests. Alternatively, when set to `false`, you can enable instrumentation by
|
||||||
`@Timed` to a request-handling method.
|
adding `@Timed` to a request-handling method.
|
||||||
|
|
||||||
By default, metrics are generated with the name, `http.server.requests`. The name can be
|
By default, metrics are generated with the name, `http.server.requests`. The name can be
|
||||||
customized by setting the `spring.metrics.web.server.requests-metrics-name` property.
|
customized by setting the `spring.metrics.web.server.requests-metrics-name` property.
|
||||||
|
@ -970,10 +971,10 @@ following information:
|
||||||
[[production-ready-metrics-jdbc]]
|
[[production-ready-metrics-jdbc]]
|
||||||
=== DataSource metrics
|
=== DataSource metrics
|
||||||
Auto-configuration will enable the instrumentation of all available `DataSources` with a
|
Auto-configuration will enable the instrumentation of all available `DataSources` with a
|
||||||
metric named `data.source`. Data source instrumentation results in gauges representing the
|
metric named `data.source`. Data source instrumentation results in gauges representing
|
||||||
currently active, maximum allowed, and minimum allowed connections in the pool. Each of
|
the currently active, maximum allowed, and minimum allowed connections in the pool. Each
|
||||||
these gauges has a name which is prefixed by `data.source` by default. The prefix can be
|
of these gauges has a name which is prefixed by `data.source` by default. The prefix can
|
||||||
be customized using the `spring.metrics.jdbc.datasource-metric-name` property.
|
be customized by using the `spring.metrics.jdbc.datasource-metric-name` property.
|
||||||
|
|
||||||
Metrics will also be tagged by the name of the `DataSource` computed based on the bean
|
Metrics will also be tagged by the name of the `DataSource` computed based on the bean
|
||||||
name.
|
name.
|
||||||
|
@ -1126,8 +1127,8 @@ and logged.
|
||||||
|
|
||||||
By default, an `InMemoryTraceRepository` that stores the last 100 events is used. If you
|
By default, an `InMemoryTraceRepository` that stores the last 100 events is used. If you
|
||||||
need to expand the capacity, you can define your own instance of the
|
need to expand the capacity, you can define your own instance of the
|
||||||
`InMemoryTraceRepository` bean. You can also create your own alternative `TraceRepository`
|
`InMemoryTraceRepository` bean. You can also create your own alternative
|
||||||
implementation.
|
`TraceRepository` implementation.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -1136,11 +1137,11 @@ implementation.
|
||||||
In the `spring-boot` module, you can find two classes to create files that are often
|
In the `spring-boot` module, you can find two classes to create files that are often
|
||||||
useful for process monitoring:
|
useful for process monitoring:
|
||||||
|
|
||||||
* `ApplicationPidFileWriter` creates a file containing the application PID (by default, in
|
* `ApplicationPidFileWriter` creates a file containing the application PID (by default,
|
||||||
the application directory with the file name, `application.pid`).
|
in the application directory with the file name, `application.pid`).
|
||||||
* `EmbeddedServerPortFileWriter` creates a file (or files) containing the ports of the
|
* `EmbeddedServerPortFileWriter` creates a file (or files) containing the ports of the
|
||||||
embedded server (by default, in the application directory with the file name
|
embedded server (by default, in the application directory with the file name
|
||||||
`application.port`).
|
`application.port`).
|
||||||
|
|
||||||
By default, these writers are not activated, but you can enable them in one of the ways
|
By default, these writers are not activated, but you can enable them in one of the ways
|
||||||
described in the next section.
|
described in the next section.
|
||||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -4,9 +4,9 @@
|
||||||
[partintro]
|
[partintro]
|
||||||
--
|
--
|
||||||
This section goes into more detail about how you should use Spring Boot. It covers topics
|
This section goes into more detail about how you should use Spring Boot. It covers topics
|
||||||
such as build systems, auto-configuration, and how to run your applications. We also cover
|
such as build systems, auto-configuration, and how to run your applications. We also
|
||||||
some Spring Boot best practices. Although there is nothing particularly special about
|
cover some Spring Boot best practices. Although there is nothing particularly special
|
||||||
Spring Boot (it is just another library that you can consume), there are a few
|
about Spring Boot (it is just another library that you can consume), there are a few
|
||||||
recommendations that, when followed, make your development process a little easier.
|
recommendations that, when followed, make your development process a little easier.
|
||||||
|
|
||||||
If you are starting out with Spring Boot, you should probably read the
|
If you are starting out with Spring Boot, you should probably read the
|
||||||
|
@ -21,23 +21,23 @@ section.
|
||||||
It is strongly recommended that you choose a build system that supports
|
It is strongly recommended that you choose a build system that supports
|
||||||
<<using-boot-dependency-management,_dependency management_>> and that can consume
|
<<using-boot-dependency-management,_dependency management_>> and that can consume
|
||||||
artifacts published to the "`Maven Central`" repository. We would recommend that you
|
artifacts published to the "`Maven Central`" repository. We would recommend that you
|
||||||
choose Maven or Gradle. It is possible to get Spring Boot to work with other build systems
|
choose Maven or Gradle. It is possible to get Spring Boot to work with other build
|
||||||
(Ant, for example), but they are not particularly well supported.
|
systems (Ant, for example), but they are not particularly well supported.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
[[using-boot-dependency-management]]
|
[[using-boot-dependency-management]]
|
||||||
=== Dependency Management
|
=== Dependency Management
|
||||||
Each release of Spring Boot provides a curated list of dependencies that it supports. In
|
Each release of Spring Boot provides a curated list of dependencies that it supports. In
|
||||||
practice, you do not need to provide a version for any of these dependencies in your build
|
practice, you do not need to provide a version for any of these dependencies in your
|
||||||
configuration, as Spring Boot is managing that for you. When you upgrade Spring Boot
|
build configuration, as Spring Boot is managing that for you. When you upgrade Spring
|
||||||
itself, these dependencies are upgraded as well in a consistent way.
|
Boot itself, these dependencies are upgraded as well in a consistent way.
|
||||||
|
|
||||||
NOTE: You can still specify a version and override Spring Boot's recommendations if you
|
NOTE: You can still specify a version and override Spring Boot's recommendations if you
|
||||||
need to do so.
|
need to do so.
|
||||||
|
|
||||||
The curated list contains all the spring modules that you can use with Spring Boot as well
|
The curated list contains all the spring modules that you can use with Spring Boot as
|
||||||
as a refined list of third party libraries. The list is available as a standard
|
well as a refined list of third party libraries. The list is available as a standard
|
||||||
<<using-boot-maven-without-a-parent,Bills of Materials (`spring-boot-dependencies`)>>
|
<<using-boot-maven-without-a-parent,Bills of Materials (`spring-boot-dependencies`)>>
|
||||||
that can be used with both <<using-boot-maven-parent-pom,Maven>> and
|
that can be used with both <<using-boot-maven-parent-pom,Maven>> and
|
||||||
<<using-boot-gradle,Gradle>>.
|
<<using-boot-gradle,Gradle>>.
|
||||||
|
@ -55,17 +55,18 @@ defaults. The parent project provides the following features:
|
||||||
* Java 1.8 as the default compiler level.
|
* Java 1.8 as the default compiler level.
|
||||||
* UTF-8 source encoding.
|
* UTF-8 source encoding.
|
||||||
* A <<using-boot-dependency-management,Dependency Management section>>, inherited from
|
* A <<using-boot-dependency-management,Dependency Management section>>, inherited from
|
||||||
the spring-boot-dependencies pom, that manages the versions of common dependencies. This
|
the spring-boot-dependencies pom, that manages the versions of common dependencies. This
|
||||||
dependency management lets you omit <version> tags for those dependencies when used in
|
dependency management lets you omit <version> tags for those dependencies when used in
|
||||||
your own pom.
|
your own pom.
|
||||||
* Sensible https://maven.apache.org/plugins/maven-resources-plugin/examples/filter.html[resource
|
* Sensible
|
||||||
filtering].
|
https://maven.apache.org/plugins/maven-resources-plugin/examples/filter.html[resource
|
||||||
|
filtering].
|
||||||
* Sensible plugin configuration (http://www.mojohaus.org/exec-maven-plugin/[exec plugin],
|
* Sensible plugin configuration (http://www.mojohaus.org/exec-maven-plugin/[exec plugin],
|
||||||
https://github.com/ktoso/maven-git-commit-id-plugin[Git commit ID], and
|
https://github.com/ktoso/maven-git-commit-id-plugin[Git commit ID], and
|
||||||
http://maven.apache.org/plugins/maven-shade-plugin/[shade]).
|
http://maven.apache.org/plugins/maven-shade-plugin/[shade]).
|
||||||
* Sensible resource filtering for `application.properties` and `application.yml` including
|
* Sensible resource filtering for `application.properties` and `application.yml`
|
||||||
profile-specific files (for example, `application-foo.properties` and
|
including profile-specific files (for example, `application-foo.properties` and
|
||||||
`application-foo.yml`)
|
`application-foo.yml`)
|
||||||
|
|
||||||
Note that, since the `application.properties` and `application.yml` files accept Spring
|
Note that, since the `application.properties` and `application.yml` files accept Spring
|
||||||
style placeholders (`${...}`), the Maven filtering is changed to use `@..@` placeholders.
|
style placeholders (`${...}`), the Maven filtering is changed to use `@..@` placeholders.
|
||||||
|
@ -75,8 +76,8 @@ style placeholders (`${...}`), the Maven filtering is changed to use `@..@` plac
|
||||||
|
|
||||||
[[using-boot-maven-parent-pom]]
|
[[using-boot-maven-parent-pom]]
|
||||||
==== Inheriting the Starter Parent
|
==== Inheriting the Starter Parent
|
||||||
To configure your project to inherit from the `spring-boot-starter-parent` set
|
To configure your project to inherit from the `spring-boot-starter-parent` set the
|
||||||
the `parent`, as follows:
|
`parent`, as follows:
|
||||||
|
|
||||||
[source,xml,indent=0,subs="verbatim,quotes,attributes"]
|
[source,xml,indent=0,subs="verbatim,quotes,attributes"]
|
||||||
----
|
----
|
||||||
|
@ -102,20 +103,21 @@ would add the following to your `pom.xml`:
|
||||||
</properties>
|
</properties>
|
||||||
----
|
----
|
||||||
|
|
||||||
TIP: Check the {github-code}/spring-boot-project/spring-boot-dependencies/pom.xml[`spring-boot-dependencies` pom]
|
TIP: Check the
|
||||||
|
{github-code}/spring-boot-project/spring-boot-dependencies/pom.xml[`spring-boot-dependencies` pom]
|
||||||
for a list of supported properties.
|
for a list of supported properties.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
[[using-boot-maven-without-a-parent]]
|
[[using-boot-maven-without-a-parent]]
|
||||||
==== Using Spring Boot without the Parent POM
|
==== Using Spring Boot without the Parent POM
|
||||||
Not everyone likes inheriting from the `spring-boot-starter-parent` POM. You may have your
|
Not everyone likes inheriting from the `spring-boot-starter-parent` POM. You may have
|
||||||
own corporate standard parent that you need to use or you may prefer to explicitly
|
your own corporate standard parent that you need to use or you may prefer to explicitly
|
||||||
declare all your Maven configuration.
|
declare all your Maven configuration.
|
||||||
|
|
||||||
If you do not want to use the `spring-boot-starter-parent`, you can still keep the benefit
|
If you do not want to use the `spring-boot-starter-parent`, you can still keep the
|
||||||
of the dependency management (but not the plugin management) by using a `scope=import`
|
benefit of the dependency management (but not the plugin management) by using a
|
||||||
dependency, as follows:
|
`scope=import` dependency, as follows:
|
||||||
|
|
||||||
[source,xml,indent=0,subs="verbatim,quotes,attributes"]
|
[source,xml,indent=0,subs="verbatim,quotes,attributes"]
|
||||||
----
|
----
|
||||||
|
@ -136,8 +138,8 @@ dependency, as follows:
|
||||||
The preceding sample setup does not let you override individual dependencies by using a
|
The preceding sample setup does not let you override individual dependencies by using a
|
||||||
property, as explained above. To achieve the same result, you need to add an entry in the
|
property, as explained above. To achieve the same result, you need to add an entry in the
|
||||||
`dependencyManagement` of your project **before** the `spring-boot-dependencies` entry.
|
`dependencyManagement` of your project **before** the `spring-boot-dependencies` entry.
|
||||||
For instance, to upgrade to another Spring Data release train, you could add the following
|
For instance, to upgrade to another Spring Data release train, you could add the
|
||||||
element to your `pom.xml`:
|
following element to your `pom.xml`:
|
||||||
|
|
||||||
[source,xml,indent=0,subs="verbatim,quotes,attributes"]
|
[source,xml,indent=0,subs="verbatim,quotes,attributes"]
|
||||||
----
|
----
|
||||||
|
@ -197,7 +199,7 @@ To learn about using Spring Boot with Gradle, please refer to the documentation
|
||||||
Spring Boot's Gradle plugin:
|
Spring Boot's Gradle plugin:
|
||||||
|
|
||||||
* Reference ({spring-boot-gradle-plugin}/reference/html[HTML] and
|
* Reference ({spring-boot-gradle-plugin}/reference/html[HTML] and
|
||||||
{spring-boot-gradle-plugin}/reference/pdf/spring-boot-gradle-plugin-reference.pdf[PDF])
|
{spring-boot-gradle-plugin}/reference/pdf/spring-boot-gradle-plugin-reference.pdf[PDF])
|
||||||
* {spring-boot-gradle-plugin}/api[API]
|
* {spring-boot-gradle-plugin}/api[API]
|
||||||
|
|
||||||
[[using-boot-ant]]
|
[[using-boot-ant]]
|
||||||
|
@ -288,9 +290,9 @@ search dependencies by name. For example, with the appropriate Eclipse or STS pl
|
||||||
installed, you can simply hit `ctrl-space` in the POM editor and type
|
installed, you can simply hit `ctrl-space` in the POM editor and type
|
||||||
"`spring-boot-starter`" for a complete list.
|
"`spring-boot-starter`" for a complete list.
|
||||||
|
|
||||||
As explained in the <<spring-boot-features#boot-features-custom-starter,Creating Your Own
|
As explained in the "`<<spring-boot-features#boot-features-custom-starter,Creating Your
|
||||||
Starter>> section, third party starters should not start with `spring-boot`, as it is
|
Own Starter>>`" section, third party starters should not start with `spring-boot`, as it
|
||||||
reserved for official Spring Boot artifacts. Rather, a third-party starter typically
|
is reserved for official Spring Boot artifacts. Rather, a third-party starter typically
|
||||||
starts with the name of the project. For example, a third-party starter project called
|
starts with the name of the project. For example, a third-party starter project called
|
||||||
`thirdpartyproject` would typically be named `thirdpartyproject-spring-boot-starter`.
|
`thirdpartyproject` would typically be named `thirdpartyproject-spring-boot-starter`.
|
||||||
****
|
****
|
||||||
|
@ -308,8 +310,7 @@ _<<production-ready-features.adoc#production-ready, production ready>>_ features
|
||||||
include::../../../target/generated-resources/production-starters.adoc[]
|
include::../../../target/generated-resources/production-starters.adoc[]
|
||||||
|
|
||||||
Finally, Spring Boot also includes the following starters that can be used if you want to
|
Finally, Spring Boot also includes the following starters that can be used if you want to
|
||||||
exclude
|
exclude or swap specific technical facets:
|
||||||
or swap specific technical facets:
|
|
||||||
|
|
||||||
.Spring Boot technical starters
|
.Spring Boot technical starters
|
||||||
include::../../../target/generated-resources/technical-starters.adoc[]
|
include::../../../target/generated-resources/technical-starters.adoc[]
|
||||||
|
@ -331,9 +332,9 @@ best practices that help.
|
||||||
=== Using the "`default`" Package
|
=== Using the "`default`" Package
|
||||||
When a class does not include a `package` declaration, it is considered to be in the
|
When a class does not include a `package` declaration, it is considered to be in the
|
||||||
"`default package`". The use of the "`default package`" is generally discouraged and
|
"`default package`". The use of the "`default package`" is generally discouraged and
|
||||||
should be avoided. It can cause particular problems for Spring Boot applications that
|
should be avoided. It can cause particular problems for Spring Boot applications that use
|
||||||
use the `@ComponentScan`, `@EntityScan`, or `@SpringBootApplication` annotations, since
|
the `@ComponentScan`, `@EntityScan`, or `@SpringBootApplication` annotations, since every
|
||||||
every class from every jar is read.
|
class from every jar is read.
|
||||||
|
|
||||||
TIP: We recommend that you follow Java's recommended package naming conventions and use a
|
TIP: We recommend that you follow Java's recommended package naming conventions and use a
|
||||||
reversed domain name (for example, `com.example.project`).
|
reversed domain name (for example, `com.example.project`).
|
||||||
|
@ -403,9 +404,9 @@ The `Application.java` file would declare the `main` method, along with the basi
|
||||||
[[using-boot-configuration-classes]]
|
[[using-boot-configuration-classes]]
|
||||||
== Configuration Classes
|
== Configuration Classes
|
||||||
Spring Boot favors Java-based configuration. Although it is possible to use
|
Spring Boot favors Java-based configuration. Although it is possible to use
|
||||||
`SpringApplication` with XML sources, we generally recommend that your primary source be a
|
`SpringApplication` with XML sources, we generally recommend that your primary source be
|
||||||
single `@Configuration` class. Usually the class that defines the `main` method is a good
|
a single `@Configuration` class. Usually the class that defines the `main` method is a
|
||||||
candidate as the primary `@Configuration`.
|
good candidate as the primary `@Configuration`.
|
||||||
|
|
||||||
TIP: Many Spring configuration examples have been published on the Internet that use XML
|
TIP: Many Spring configuration examples have been published on the Internet that use XML
|
||||||
configuration. If possible, always try to use the equivalent Java-based configuration.
|
configuration. If possible, always try to use the equivalent Java-based configuration.
|
||||||
|
@ -433,9 +434,9 @@ XML configuration files.
|
||||||
[[using-boot-auto-configuration]]
|
[[using-boot-auto-configuration]]
|
||||||
== Auto-configuration
|
== Auto-configuration
|
||||||
Spring Boot auto-configuration attempts to automatically configure your Spring
|
Spring Boot auto-configuration attempts to automatically configure your Spring
|
||||||
application based on the jar dependencies that you have added. For example, if
|
application based on the jar dependencies that you have added. For example, if `HSQLDB`
|
||||||
`HSQLDB` is on your classpath, and you have not manually configured any database
|
is on your classpath, and you have not manually configured any database connection beans,
|
||||||
connection beans, then Spring Boot auto-configures an in-memory database.
|
then Spring Boot auto-configures an in-memory database.
|
||||||
|
|
||||||
You need to opt-in to auto-configuration by adding the `@EnableAutoConfiguration` or
|
You need to opt-in to auto-configuration by adding the `@EnableAutoConfiguration` or
|
||||||
`@SpringBootApplication` annotations to one of your `@Configuration` classes.
|
`@SpringBootApplication` annotations to one of your `@Configuration` classes.
|
||||||
|
@ -447,13 +448,13 @@ recommend that you add it to your primary `@Configuration` class.
|
||||||
|
|
||||||
[[using-boot-replacing-auto-configuration]]
|
[[using-boot-replacing-auto-configuration]]
|
||||||
=== Gradually Replacing Auto-configuration
|
=== Gradually Replacing Auto-configuration
|
||||||
Auto-configuration is noninvasive. At any point, you can start to define your own
|
Auto-configuration is non-invasive. At any point, you can start to define your own
|
||||||
configuration to replace specific parts of the auto-configuration. For example, if you add
|
configuration to replace specific parts of the auto-configuration. For example, if you
|
||||||
your own `DataSource` bean, the default embedded database support backs away.
|
add your own `DataSource` bean, the default embedded database support backs away.
|
||||||
|
|
||||||
If you need to find out what auto-configuration is currently being applied, and why, start
|
If you need to find out what auto-configuration is currently being applied, and why,
|
||||||
your application with the `--debug` switch. Doing so enables debug logs for a selection of
|
start your application with the `--debug` switch. Doing so enables debug logs for a
|
||||||
core loggers and logs an auto-configuration report to the console.
|
selection of core loggers and logs an auto-configuration report to the console.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -476,17 +477,18 @@ as shown in the following example:
|
||||||
----
|
----
|
||||||
|
|
||||||
If the class is not on the classpath, you can use the `excludeName` attribute of the
|
If the class is not on the classpath, you can use the `excludeName` attribute of the
|
||||||
annotation and specify the fully qualified name instead. Finally, you can also control the
|
annotation and specify the fully qualified name instead. Finally, you can also control
|
||||||
list of auto-configuration classes to exclude by using the `spring.autoconfigure.exclude`
|
the list of auto-configuration classes to exclude by using the
|
||||||
property.
|
`spring.autoconfigure.exclude` property.
|
||||||
|
|
||||||
TIP: You can define exclusions both at the annotation level and by using the property.
|
TIP: You can define exclusions both at the annotation level and by using the property.
|
||||||
|
|
||||||
[[using-boot-spring-beans-and-dependency-injection]]
|
[[using-boot-spring-beans-and-dependency-injection]]
|
||||||
== Spring Beans and Dependency Injection
|
== Spring Beans and Dependency Injection
|
||||||
You are free to use any of the standard Spring Framework techniques to define your beans
|
You are free to use any of the standard Spring Framework techniques to define your beans
|
||||||
and their injected dependencies. For simplicity, we often find that using `@ComponentScan`
|
and their injected dependencies. For simplicity, we often find that using
|
||||||
(to find your beans) and using `@Autowired` (to do constructor injection) works well.
|
`@ComponentScan` (to find your beans) and using `@Autowired` (to do constructor
|
||||||
|
injection) works well.
|
||||||
|
|
||||||
If you structure your code as suggested above (locating your application class in a root
|
If you structure your code as suggested above (locating your application class in a root
|
||||||
package), you can add `@ComponentScan` without any arguments. All of your application
|
package), you can add `@ComponentScan` without any arguments. All of your application
|
||||||
|
@ -545,10 +547,10 @@ TIP: Notice how using constructor injection lets the `riskAssessor` field be mar
|
||||||
[[using-boot-using-springbootapplication-annotation]]
|
[[using-boot-using-springbootapplication-annotation]]
|
||||||
== Using the @SpringBootApplication Annotation
|
== Using the @SpringBootApplication Annotation
|
||||||
Many Spring Boot developers always have their main class annotated with `@Configuration`,
|
Many Spring Boot developers always have their main class annotated with `@Configuration`,
|
||||||
`@EnableAutoConfiguration`, and `@ComponentScan`. Since these annotations are so frequently
|
`@EnableAutoConfiguration`, and `@ComponentScan`. Since these annotations are so
|
||||||
used together (especially if you follow the <<using-boot-structuring-your-code, best
|
frequently used together (especially if you follow the
|
||||||
practices>> above), Spring Boot provides a convenient `@SpringBootApplication`
|
<<using-boot-structuring-your-code, best practices>> above), Spring Boot provides a
|
||||||
alternative.
|
convenient `@SpringBootApplication` alternative.
|
||||||
|
|
||||||
The `@SpringBootApplication` annotation is equivalent to using `@Configuration`,
|
The `@SpringBootApplication` annotation is equivalent to using `@Configuration`,
|
||||||
`@EnableAutoConfiguration`, and `@ComponentScan` with their default attributes, as shown
|
`@EnableAutoConfiguration`, and `@ComponentScan` with their default attributes, as shown
|
||||||
|
@ -579,9 +581,10 @@ NOTE: `@SpringBootApplication` also provides aliases to customize the attributes
|
||||||
|
|
||||||
[[using-boot-running-your-application]]
|
[[using-boot-running-your-application]]
|
||||||
== Running Your Application
|
== Running Your Application
|
||||||
One of the biggest advantages of packaging your application as a jar and using an embedded
|
One of the biggest advantages of packaging your application as a jar and using an
|
||||||
HTTP server is that you can run your application as you would any other. Debugging Spring
|
embedded HTTP server is that you can run your application as you would any other.
|
||||||
Boot applications is also easy. You do not need any special IDE plugins or extensions.
|
Debugging Spring Boot applications is also easy. You do not need any special IDE plugins
|
||||||
|
or extensions.
|
||||||
|
|
||||||
NOTE: This section only covers jar based packaging. If you choose to package your
|
NOTE: This section only covers jar based packaging. If you choose to package your
|
||||||
application as a war file, you should refer to your server and IDE documentation.
|
application as a war file, you should refer to your server and IDE documentation.
|
||||||
|
@ -590,10 +593,10 @@ application as a war file, you should refer to your server and IDE documentation
|
||||||
|
|
||||||
[[using-boot-running-from-an-ide]]
|
[[using-boot-running-from-an-ide]]
|
||||||
=== Running from an IDE
|
=== Running from an IDE
|
||||||
You can run a Spring Boot application from your IDE as a simple Java application. However,
|
You can run a Spring Boot application from your IDE as a simple Java application.
|
||||||
you first need to import your project. Import steps vary depending on your IDE and build
|
However, you first need to import your project. Import steps vary depending on your IDE
|
||||||
system. Most IDEs can import Maven projects directly. For example, Eclipse users can
|
and build system. Most IDEs can import Maven projects directly. For example, Eclipse
|
||||||
select `Import...` -> `Existing Maven Projects` from the `File` menu.
|
users can select `Import...` -> `Existing Maven Projects` from the `File` menu.
|
||||||
|
|
||||||
If you cannot directly import your project into your IDE, you may be able to generate IDE
|
If you cannot directly import your project into your IDE, you may be able to generate IDE
|
||||||
metadata by using a build plugin. Maven includes plugins for
|
metadata by using a build plugin. Maven includes plugins for
|
||||||
|
@ -602,8 +605,8 @@ http://maven.apache.org/plugins/maven-idea-plugin/[IDEA]. Gradle offers plugins
|
||||||
{gradle-user-guide}/userguide.html[various IDEs].
|
{gradle-user-guide}/userguide.html[various IDEs].
|
||||||
|
|
||||||
TIP: If you accidentally run a web application twice, you see a "`Port already in use`"
|
TIP: If you accidentally run a web application twice, you see a "`Port already in use`"
|
||||||
error. STS users can use the `Relaunch` button rather than the `Run` button to ensure that
|
error. STS users can use the `Relaunch` button rather than the `Run` button to ensure
|
||||||
any existing instance is closed.
|
that any existing instance is closed.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -652,16 +655,16 @@ shown in the following example:
|
||||||
[[using-boot-running-with-the-gradle-plugin]]
|
[[using-boot-running-with-the-gradle-plugin]]
|
||||||
=== Using the Gradle Plugin
|
=== Using the Gradle Plugin
|
||||||
The Spring Boot Gradle plugin also includes a `bootRun` task that can be used to run your
|
The Spring Boot Gradle plugin also includes a `bootRun` task that can be used to run your
|
||||||
application in an exploded form. The `bootRun` task is added whenever you apply
|
application in an exploded form. The `bootRun` task is added whenever you apply the
|
||||||
the `org.springframework.boot` and `java` plugins and is shown in the following example:
|
`org.springframework.boot` and `java` plugins and is shown in the following example:
|
||||||
|
|
||||||
[indent=0,subs="attributes"]
|
[indent=0,subs="attributes"]
|
||||||
----
|
----
|
||||||
$ gradle bootRun
|
$ gradle bootRun
|
||||||
----
|
----
|
||||||
|
|
||||||
You might also want to use the `JAVA_OPTS` operating system environment variable, as shown
|
You might also want to use the `JAVA_OPTS` operating system environment variable, as
|
||||||
in the following example:
|
shown in the following example:
|
||||||
|
|
||||||
[indent=0,subs="attributes"]
|
[indent=0,subs="attributes"]
|
||||||
----
|
----
|
||||||
|
@ -752,9 +755,9 @@ TIP: For a complete list of the properties that are applied by the devtools, see
|
||||||
=== Automatic Restart
|
=== Automatic Restart
|
||||||
Applications that use `spring-boot-devtools` automatically restart whenever files on the
|
Applications that use `spring-boot-devtools` automatically restart whenever files on the
|
||||||
classpath change. This can be a useful feature when working in an IDE, as it gives a very
|
classpath change. This can be a useful feature when working in an IDE, as it gives a very
|
||||||
fast feedback loop for code changes. By default, any entry on the classpath that points to
|
fast feedback loop for code changes. By default, any entry on the classpath that points
|
||||||
a folder is monitored for changes. Note that certain resources, such as static assets and
|
to a folder is monitored for changes. Note that certain resources, such as static assets
|
||||||
view templates, <<using-boot-devtools-restart-exclude, do not need to restart the
|
and view templates, <<using-boot-devtools-restart-exclude, do not need to restart the
|
||||||
application>>.
|
application>>.
|
||||||
|
|
||||||
.Triggering a restart
|
.Triggering a restart
|
||||||
|
@ -762,16 +765,16 @@ application>>.
|
||||||
As DevTools monitors classpath resources, the only way to trigger a restart is to update
|
As DevTools monitors classpath resources, the only way to trigger a restart is to update
|
||||||
the classpath. The way in which you cause the classpath to be updated depends on the IDE
|
the classpath. The way in which you cause the classpath to be updated depends on the IDE
|
||||||
that you are using. In Eclipse, saving a modified file causes the classpath to be updated
|
that you are using. In Eclipse, saving a modified file causes the classpath to be updated
|
||||||
and triggers a restart. In IntelliJ IDEA, building the project (`Build +->+ Make Project`)
|
and triggers a restart. In IntelliJ IDEA, building the project
|
||||||
will have the same effect.
|
(`Build +->+ Make Project`) has the same effect.
|
||||||
****
|
****
|
||||||
|
|
||||||
[NOTE]
|
[NOTE]
|
||||||
====
|
====
|
||||||
As long as forking is enabled, you can also start your application by using the supported
|
As long as forking is enabled, you can also start your application by using the supported
|
||||||
build plugins (Maven and Gradle), since DevTools needs an isolated application classloader
|
build plugins (Maven and Gradle), since DevTools needs an isolated application
|
||||||
to operate properly. By default, Gradle and Maven do that when they detect DevTools on the
|
classloader to operate properly. By default, Gradle and Maven do that when they detect
|
||||||
classpath.
|
DevTools on the classpath.
|
||||||
|
|
||||||
====
|
====
|
||||||
|
|
||||||
|
@ -784,9 +787,10 @@ NOTE: DevTools relies on the application context's shutdown hook to close it dur
|
||||||
restart. It does not work correctly if you have disabled the shutdown hook
|
restart. It does not work correctly if you have disabled the shutdown hook
|
||||||
(`SpringApplication.setRegisterShutdownHook(false)`).
|
(`SpringApplication.setRegisterShutdownHook(false)`).
|
||||||
|
|
||||||
NOTE: When deciding if an entry on the classpath should trigger a restart when it changes,
|
NOTE: When deciding if an entry on the classpath should trigger a restart when it
|
||||||
DevTools automatically ignores projects named `spring-boot`, `spring-boot-devtools`,
|
changes, DevTools automatically ignores projects named `spring-boot`,
|
||||||
`spring-boot-autoconfigure`, `spring-boot-actuator`, and `spring-boot-starter`.
|
`spring-boot-devtools`, `spring-boot-autoconfigure`, `spring-boot-actuator`, and
|
||||||
|
`spring-boot-starter`.
|
||||||
|
|
||||||
NOTE: DevTools needs to customize the `ResourceLoader` used by the `ApplicationContext`.
|
NOTE: DevTools needs to customize the `ResourceLoader` used by the `ApplicationContext`.
|
||||||
If your application provides one already, it is going to be wrapped. Direct override of
|
If your application provides one already, it is going to be wrapped. Direct override of
|
||||||
|
@ -799,8 +803,8 @@ The restart technology provided by Spring Boot works by using two classloaders.
|
||||||
that do not change (for example, those from third-party jars) are loaded into a _base_
|
that do not change (for example, those from third-party jars) are loaded into a _base_
|
||||||
classloader. Classes that you are actively developing are loaded into a _restart_
|
classloader. Classes that you are actively developing are loaded into a _restart_
|
||||||
classloader. When the application is restarted, the _restart_ classloader is thrown away
|
classloader. When the application is restarted, the _restart_ classloader is thrown away
|
||||||
and a new one is created. This approach means that application restarts are typically much
|
and a new one is created. This approach means that application restarts are typically
|
||||||
faster than "`cold starts`", since the _base_ classloader is already available and
|
much faster than "`cold starts`", since the _base_ classloader is already available and
|
||||||
populated.
|
populated.
|
||||||
|
|
||||||
If you find that restarts are not quick enough for your applications or you encounter
|
If you find that restarts are not quick enough for your applications or you encounter
|
||||||
|
@ -817,9 +821,9 @@ Certain resources do not necessarily need to trigger a restart when they are cha
|
||||||
example, Thymeleaf templates can be edited in-place. By default, changing resources
|
example, Thymeleaf templates can be edited in-place. By default, changing resources
|
||||||
in `/META-INF/maven`, `/META-INF/resources`, `/resources`, `/static`, `/public`, or
|
in `/META-INF/maven`, `/META-INF/resources`, `/resources`, `/static`, `/public`, or
|
||||||
`/templates` does not trigger a restart but does trigger a
|
`/templates` does not trigger a restart but does trigger a
|
||||||
<<using-boot-devtools-livereload, live reload>>. If you want to customize these exclusions,
|
<<using-boot-devtools-livereload, live reload>>. If you want to customize these
|
||||||
you can use the `spring.devtools.restart.exclude` property. For example, to exclude only
|
exclusions, you can use the `spring.devtools.restart.exclude` property. For example, to
|
||||||
`/static` and `/public` you would set the following property:
|
exclude only `/static` and `/public` you would set the following property:
|
||||||
|
|
||||||
[indent=0]
|
[indent=0]
|
||||||
----
|
----
|
||||||
|
@ -834,8 +838,8 @@ TIP: If you want to keep those defaults and _add_ additional exclusions, use the
|
||||||
==== Watching Additional Paths
|
==== Watching Additional Paths
|
||||||
You may want your application to be restarted or reloaded when you make changes to files
|
You may want your application to be restarted or reloaded when you make changes to files
|
||||||
that are not on the classpath. To do so, use the
|
that are not on the classpath. To do so, use the
|
||||||
`spring.devtools.restart.additional-paths` property to configure additional paths to watch
|
`spring.devtools.restart.additional-paths` property to configure additional paths to
|
||||||
for changes. You can use the `spring.devtools.restart.exclude` property
|
watch for changes. You can use the `spring.devtools.restart.exclude` property
|
||||||
<<using-boot-devtools-restart-exclude, described above>> to control whether changes
|
<<using-boot-devtools-restart-exclude, described above>> to control whether changes
|
||||||
beneath the additional paths trigger a full restart or a
|
beneath the additional paths trigger a full restart or a
|
||||||
<<using-boot-devtools-livereload, live reload>>.
|
<<using-boot-devtools-livereload, live reload>>.
|
||||||
|
@ -845,14 +849,14 @@ beneath the additional paths trigger a full restart or a
|
||||||
[[using-boot-devtools-restart-disable]]
|
[[using-boot-devtools-restart-disable]]
|
||||||
==== Disabling Restart
|
==== Disabling Restart
|
||||||
If you do not want to use the restart feature, you can disable it by using the
|
If you do not want to use the restart feature, you can disable it by using the
|
||||||
`spring.devtools.restart.enabled` property. In most cases, you can set this property in your
|
`spring.devtools.restart.enabled` property. In most cases, you can set this property in
|
||||||
`application.properties` (doing so still initializes the restart classloader, but it does not
|
your `application.properties` (doing so still initializes the restart classloader, but it
|
||||||
watch for file changes).
|
does not watch for file changes).
|
||||||
|
|
||||||
If you need to _completely_ disable restart support (for example, because it doesn't work
|
If you need to _completely_ disable restart support (for example, because it doesn't work
|
||||||
with a specific library), you need to set the `spring.devtools.restart.enabled` `System`
|
with a specific library), you need to set the `spring.devtools.restart.enabled` `System`
|
||||||
property to `false` before calling `SpringApplication.run(...)`, as shown in the following
|
property to `false` before calling `SpringApplication.run(...)`, as shown in the
|
||||||
example:
|
following example:
|
||||||
|
|
||||||
[source,java,indent=0]
|
[source,java,indent=0]
|
||||||
----
|
----
|
||||||
|
@ -868,13 +872,13 @@ example:
|
||||||
==== Using a Trigger File
|
==== Using a Trigger File
|
||||||
If you work with an IDE that continuously compiles changed files, you might prefer to
|
If you work with an IDE that continuously compiles changed files, you might prefer to
|
||||||
trigger restarts only at specific times. To do so, you can use a "`trigger file`", which
|
trigger restarts only at specific times. To do so, you can use a "`trigger file`", which
|
||||||
is a special file that must be modified when you want to actually trigger a restart check.
|
is a special file that must be modified when you want to actually trigger a restart
|
||||||
Changing the file only triggers the check and the restart will only occur if Devtools has
|
check. Changing the file only triggers the check and the restart will only occur if
|
||||||
detected it has to do something. The trigger file can be updated manually or via an IDE
|
Devtools has detected it has to do something. The trigger file can be updated manually or
|
||||||
plugin.
|
with an IDE plugin.
|
||||||
|
|
||||||
To use a trigger file, set the `spring.devtools.restart.trigger-file` property to the path
|
To use a trigger file, set the `spring.devtools.restart.trigger-file` property to the
|
||||||
of your trigger file.
|
path of your trigger file.
|
||||||
|
|
||||||
TIP: You might want to set `spring.devtools.restart.trigger-file` as a
|
TIP: You might want to set `spring.devtools.restart.trigger-file` as a
|
||||||
<<using-boot-devtools-globalsettings,global setting>>, so that all your projects behave
|
<<using-boot-devtools-globalsettings,global setting>>, so that all your projects behave
|
||||||
|
@ -929,8 +933,8 @@ authors.
|
||||||
[[using-boot-devtools-livereload]]
|
[[using-boot-devtools-livereload]]
|
||||||
=== LiveReload
|
=== LiveReload
|
||||||
The `spring-boot-devtools` module includes an embedded LiveReload server that can be used
|
The `spring-boot-devtools` module includes an embedded LiveReload server that can be used
|
||||||
to trigger a browser refresh when a resource is changed. LiveReload browser extensions are
|
to trigger a browser refresh when a resource is changed. LiveReload browser extensions
|
||||||
freely available for Chrome, Firefox and Safari from
|
are freely available for Chrome, Firefox and Safari from
|
||||||
http://livereload.com/extensions/[livereload.com].
|
http://livereload.com/extensions/[livereload.com].
|
||||||
|
|
||||||
If you do not want to start the LiveReload server when your application runs, you can set
|
If you do not want to start the LiveReload server when your application runs, you can set
|
||||||
|
@ -963,8 +967,8 @@ property:
|
||||||
=== Remote Applications
|
=== Remote Applications
|
||||||
The Spring Boot developer tools are not just limited to local development. You can also
|
The Spring Boot developer tools are not just limited to local development. You can also
|
||||||
use several features when running applications remotely. Remote support is opt-in. To
|
use several features when running applications remotely. Remote support is opt-in. To
|
||||||
enable it, you need to make sure that `devtools` is included in the repackaged archive, as
|
enable it, you need to make sure that `devtools` is included in the repackaged archive,
|
||||||
shown in the following listing:
|
as shown in the following listing:
|
||||||
|
|
||||||
[source,xml,indent=0,subs="verbatim,quotes,attributes"]
|
[source,xml,indent=0,subs="verbatim,quotes,attributes"]
|
||||||
----
|
----
|
||||||
|
@ -981,8 +985,8 @@ shown in the following listing:
|
||||||
</build>
|
</build>
|
||||||
----
|
----
|
||||||
|
|
||||||
Then you need to set a `spring.devtools.remote.secret` property, as shown in the following
|
Then you need to set a `spring.devtools.remote.secret` property, as shown in the
|
||||||
example:
|
following example:
|
||||||
|
|
||||||
[source,properties,indent=0]
|
[source,properties,indent=0]
|
||||||
----
|
----
|
||||||
|
@ -994,8 +998,8 @@ should never enable support on a production deployment.
|
||||||
|
|
||||||
Remote devtools support is provided in two parts: a server-side endpoint that accepts
|
Remote devtools support is provided in two parts: a server-side endpoint that accepts
|
||||||
connections and a client application that you run in your IDE. The server component is
|
connections and a client application that you run in your IDE. The server component is
|
||||||
automatically enabled when the `spring.devtools.remote.secret` property is set. The client
|
automatically enabled when the `spring.devtools.remote.secret` property is set. The
|
||||||
component must be launched manually.
|
client component must be launched manually.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -1013,7 +1017,7 @@ you have deployed to Cloud Foundry, you would do the following:
|
||||||
* Browse for the `my-app` project.
|
* Browse for the `my-app` project.
|
||||||
* Use `org.springframework.boot.devtools.RemoteSpringApplication` as the main class.
|
* Use `org.springframework.boot.devtools.RemoteSpringApplication` as the main class.
|
||||||
* Add `+++https://myapp.cfapps.io+++` to the `Program arguments` (or whatever your remote
|
* Add `+++https://myapp.cfapps.io+++` to the `Program arguments` (or whatever your remote
|
||||||
URL is).
|
URL is).
|
||||||
|
|
||||||
A running remote client might resemble the following listing:
|
A running remote client might resemble the following listing:
|
||||||
|
|
||||||
|
@ -1074,6 +1078,6 @@ _<<production-ready-features.adoc#production-ready>>_ for details.
|
||||||
== What to Read Next
|
== What to Read Next
|
||||||
You should now understand how you can use Spring Boot and some best practices that you
|
You should now understand how you can use Spring Boot and some best practices that you
|
||||||
should follow. You can now go on to learn about specific
|
should follow. You can now go on to learn about specific
|
||||||
_<<spring-boot-features#boot-features, Spring Boot features>>_ in depth, or you could skip
|
_<<spring-boot-features#boot-features, Spring Boot features>>_ in depth, or you could
|
||||||
ahead and read about the "`<<production-ready-features#production-ready, production
|
skip ahead and read about the "`<<production-ready-features#production-ready, production
|
||||||
ready>>`" aspects of Spring Boot.
|
ready>>`" aspects of Spring Boot.
|
||||||
|
|
Loading…
Reference in New Issue