Migrate spring-boot-docs to Antora

See gh-33766
This commit is contained in:
Phillip Webb 2024-03-19 21:48:08 -07:00
parent 8ee20c8dae
commit 8dd89c1ac7
202 changed files with 6844 additions and 6593 deletions

View File

@ -1,6 +1,6 @@
plugins {
id "java"
id "org.asciidoctor.jvm.convert"
id "org.antora"
id "org.springframework.boot.conventions"
id "org.springframework.boot.deployed"
id 'org.jetbrains.kotlin.jvm'
@ -9,25 +9,12 @@ plugins {
description = "Spring Boot Docs"
configurations {
actuatorApiDocumentation
autoConfiguration
configurationProperties
gradlePluginDocumentation
mavenPluginDocumentation
remoteSpringApplicationExample
springApplicationExample
testSlices
asciidoctorExtensions {
resolutionStrategy {
eachDependency { dependency ->
// Downgrade SnakeYAML as Asciidoctor fails due to an incompatibility
// in the Pysch gem
if (dependency.requested.group.equals("org.yaml")) {
dependency.useVersion("1.33")
}
}
}
}
antoraContent
}
jar {
@ -53,16 +40,6 @@ plugins.withType(EclipsePlugin) {
}
dependencies {
actuatorApiDocumentation(project(path: ":spring-boot-project:spring-boot-actuator-autoconfigure", configuration: "documentation"))
asciidoctorExtensions("io.spring.asciidoctor:spring-asciidoctor-extensions-spring-boot")
asciidoctorExtensions("io.spring.asciidoctor:spring-asciidoctor-extensions-section-ids")
asciidoctorExtensions(project(path: ":spring-boot-project:spring-boot-actuator-autoconfigure"))
asciidoctorExtensions(project(path: ":spring-boot-project:spring-boot-autoconfigure"))
asciidoctorExtensions(project(path: ":spring-boot-project:spring-boot-devtools"))
asciidoctorExtensions(project(path: ":spring-boot-project:spring-boot-docker-compose"))
asciidoctorExtensions(project(path: ":spring-boot-project:spring-boot-testcontainers"))
autoConfiguration(project(path: ":spring-boot-project:spring-boot-autoconfigure", configuration: "autoConfigurationMetadata"))
autoConfiguration(project(path: ":spring-boot-project:spring-boot-actuator-autoconfigure", configuration: "autoConfigurationMetadata"))
autoConfiguration(project(path: ":spring-boot-project:spring-boot-devtools", configuration: "autoConfigurationMetadata"))
@ -77,8 +54,6 @@ dependencies {
configurationProperties(project(path: ":spring-boot-project:spring-boot-test-autoconfigure", configuration: "configurationPropertiesMetadata"))
configurationProperties(project(path: ":spring-boot-project:spring-boot-testcontainers", configuration: "configurationPropertiesMetadata"))
gradlePluginDocumentation(project(path: ":spring-boot-project:spring-boot-tools:spring-boot-gradle-plugin", configuration: "documentation"))
implementation(project(path: ":spring-boot-project:spring-boot-actuator"))
implementation(project(path: ":spring-boot-project:spring-boot-actuator-autoconfigure"))
implementation(project(path: ":spring-boot-project:spring-boot-autoconfigure"))
@ -184,8 +159,6 @@ dependencies {
implementation("org.junit.jupiter:junit-jupiter")
implementation("org.yaml:snakeyaml")
mavenPluginDocumentation(project(path: ":spring-boot-project:spring-boot-tools:spring-boot-maven-plugin", configuration: "documentation"))
remoteSpringApplicationExample(platform(project(":spring-boot-project:spring-boot-dependencies")))
remoteSpringApplicationExample(project(":spring-boot-project:spring-boot-devtools"))
remoteSpringApplicationExample(project(":spring-boot-project:spring-boot-starters:spring-boot-starter-logging"))
@ -194,6 +167,10 @@ dependencies {
springApplicationExample(platform(project(":spring-boot-project:spring-boot-dependencies")))
springApplicationExample(project(path: ":spring-boot-project:spring-boot-starters:spring-boot-starter-web"))
antoraContent(project(path: ":spring-boot-project:spring-boot-actuator-autoconfigure", configuration: "antoraContent"))
antoraContent(project(path: ":spring-boot-project:spring-boot-tools:spring-boot-gradle-plugin", configuration: "antoraContent"))
antoraContent(project(path: ":spring-boot-project:spring-boot-tools:spring-boot-maven-plugin", configuration: "antoraContent"))
testImplementation(project(":spring-boot-project:spring-boot-actuator-autoconfigure"))
testImplementation(project(":spring-boot-project:spring-boot-tools:spring-boot-test-support"))
testImplementation("org.assertj:assertj-core")
@ -206,16 +183,14 @@ dependencies {
testSlices(project(path: ":spring-boot-project:spring-boot-test-autoconfigure", configuration: "testSliceMetadata"))
}
task dependencyVersions(type: org.springframework.boot.build.constraints.ExtractVersionConstraints) {
enforcedPlatform(":spring-boot-project:spring-boot-dependencies")
}
task aggregatedJavadoc(type: Javadoc) {
dependsOn dependencyVersions
project.rootProject.gradle.projectsEvaluated {
Set<Project> publishedProjects = rootProject.subprojects.findAll { it != project }
.findAll { it.plugins.hasPlugin(JavaPlugin) && it.plugins.hasPlugin(MavenPublishPlugin) }
.findAll { !it.path.contains(":spring-boot-tools:") }
.findAll { !it.path.contains(":spring-boot-tools:") ||
it.path.contains(":spring-boot-tools:spring-boot-buildpack-platform") ||
it.path.contains(":spring-boot-tools:spring-boot-loader-tools") }
.findAll { !it.name.startsWith('spring-boot-starter') }
dependsOn publishedProjects.javadoc
source publishedProjects.javadoc.source
@ -247,33 +222,33 @@ task aggregatedJavadoc(type: Javadoc) {
task documentTestSlices(type: org.springframework.boot.build.test.autoconfigure.DocumentTestSlices) {
testSlices = configurations.testSlices
outputFile = file("${buildDir}/docs/generated/test-auto-configuration/documented-slices.adoc")
outputFile = file("${buildDir}/generated/docs/test-auto-configuration/documented-slices.adoc")
}
task documentStarters(type: org.springframework.boot.build.starters.DocumentStarters) {
outputDir = file("${buildDir}/docs/generated/using/starters/")
outputDir = file("${buildDir}/generated/docs/using/starters/")
}
task documentAutoConfigurationClasses(type: org.springframework.boot.build.autoconfigure.DocumentAutoConfigurationClasses) {
autoConfiguration = configurations.autoConfiguration
outputDir = file("${buildDir}/docs/generated/auto-configuration-classes/documented-auto-configuration-classes/")
outputDir = file("${buildDir}/generated/docs/auto-configuration-classes/documented-auto-configuration-classes/")
}
task documentDependencyVersions(type: org.springframework.boot.build.constraints.DocumentConstrainedVersions) {
task documentDependencyVersionCoordinates(type: org.springframework.boot.build.constraints.DocumentConstrainedVersions) {
dependsOn dependencyVersions
constrainedVersions.set(providers.provider { dependencyVersions.constrainedVersions })
outputFile = file("${buildDir}/docs/generated/dependency-versions/documented-coordinates.adoc")
outputFile = file("${buildDir}/generated/docs/dependency-versions/documented-coordinates.adoc")
}
task documentVersionProperties(type: org.springframework.boot.build.constraints.DocumentVersionProperties) {
task documentDependencyVersionProperties(type: org.springframework.boot.build.constraints.DocumentVersionProperties) {
dependsOn dependencyVersions
versionProperties.set(providers.provider { dependencyVersions.versionProperties})
outputFile = file("${buildDir}/docs/generated/dependency-versions/documented-properties.adoc")
outputFile = file("${buildDir}/generated/docs/dependency-versions/documented-properties.adoc")
}
task documentConfigurationProperties(type: org.springframework.boot.build.context.properties.DocumentConfigurationProperties) {
configurationPropertyMetadata = configurations.configurationProperties
outputDir = file("${buildDir}/docs/generated/")
outputDir = file("${buildDir}/generated/docs/application-properties")
}
task documentDevtoolsPropertyDefaults(type: org.springframework.boot.build.devtools.DocumentDevtoolsPropertyDefaults) {}
@ -306,203 +281,92 @@ task runLoggingFormatExample(type: org.springframework.boot.build.docs.Applicati
normalizeTomcatPort()
}
tasks.withType(org.asciidoctor.gradle.jvm.AbstractAsciidoctorTask) {
outputs.doNotCacheIf("This task uses log files as inputs which contain changing data (timestamp, pid)") { true }
dependsOn dependencyVersions
inputs.files(runRemoteSpringApplicationExample).withPropertyName("runRemoteSpringApplicationExample").withPathSensitivity(PathSensitivity.RELATIVE)
inputs.files(runSpringApplicationExample).withPropertyName("runSpringApplicationExample").withPathSensitivity(PathSensitivity.RELATIVE)
inputs.files(runLoggingFormatExample).withPropertyName("runLoggingFormatExample").withPathSensitivity(PathSensitivity.RELATIVE)
asciidoctorj {
fatalWarnings = ['^((?!successfully validated).)*$']
def getRelativeExamplesPath(var outputs) {
def fileName = outputs.files.singleFile.name
'example$example-output/' + fileName
}
def antoraRootAggregateContent = tasks.register("antoraRootAggregateContent", Zip) {
destinationDirectory = layout.buildDirectory.dir('generated/docs/antora-content')
archiveClassifier = "root-aggregate-content"
from("src/main") {
into "modules/ROOT/examples"
}
doFirst {
def versionConstraints = dependencyVersions.versionConstraints
def toAntoraVersion = version -> {
String formatted = version.split("\\.").take(2).join('.')
return version.endsWith("-SNAPSHOT") ? formatted + "-SNAPSHOT" : formatted
from(project.configurations.configurationProperties) {
eachFile {
it.path = it.file.toString()
.replaceFirst('/build/(?:classes|resources)/java/main/', '/')
.replaceFirst('^.*/([^/]+)/META-INF/(spring-configuration-metadata\\.json)$', 'modules/ROOT/partials/$1/$2')
}
attributes "hibernate-version": versionConstraints["org.hibernate.orm:hibernate-core"].split("\\.").take(2).join('.'),
"jetty-version": versionConstraints["org.eclipse.jetty:jetty-server"],
"jooq-version": versionConstraints["org.jooq:jooq"],
"lettuce-version": versionConstraints["io.lettuce:lettuce-core"],
"native-build-tools-version": nativeBuildToolsVersion,
"spring-amqp-version": versionConstraints["org.springframework.amqp:spring-amqp"],
"spring-batch-version": versionConstraints["org.springframework.batch:spring-batch-core"],
"spring-batch-version-antora": toAntoraVersion(versionConstraints["org.springframework.batch:spring-batch-core"]),
"spring-boot-version": project.version,
"spring-data-commons-version": versionConstraints["org.springframework.data:spring-data-commons"],
"spring-data-couchbase-version": versionConstraints["org.springframework.data:spring-data-couchbase"],
"spring-data-jdbc-version": versionConstraints["org.springframework.data:spring-data-jdbc"],
"spring-data-jpa-version": versionConstraints["org.springframework.data:spring-data-jpa"],
"spring-data-mongodb-version": versionConstraints["org.springframework.data:spring-data-mongodb"],
"spring-data-neo4j-version": versionConstraints["org.springframework.data:spring-data-neo4j"],
"spring-data-r2dbc-version": versionConstraints["org.springframework.data:spring-data-r2dbc"],
"spring-data-rest-version": versionConstraints["org.springframework.data:spring-data-rest-core"],
"spring-framework-version": versionConstraints["org.springframework:spring-core"],
"spring-framework-version-antora": toAntoraVersion(versionConstraints["org.springframework:spring-core"]),
"spring-graphql-version-antora": toAntoraVersion(versionConstraints["org.springframework.graphql:spring-graphql"]),
"spring-integration-version-antora": toAntoraVersion(versionConstraints["org.springframework.integration:spring-integration-core"]),
"spring-kafka-version": versionConstraints["org.springframework.kafka:spring-kafka"],
"spring-pulsar-version": versionConstraints["org.springframework.pulsar:spring-pulsar"],
"spring-security-version-antora": toAntoraVersion(versionConstraints["org.springframework.security:spring-security-core"]),
"spring-authorization-server-version-antora": toAntoraVersion(versionConstraints["org.springframework.security:spring-security-oauth2-authorization-server"]),
"spring-webservices-version": versionConstraints["org.springframework.ws:spring-ws-core"],
"tomcat-version": tomcatVersion.split("\\.").take(2).join('.'),
"remote-spring-application-output": runRemoteSpringApplicationExample.outputs.files.singleFile,
"spring-application-output": runSpringApplicationExample.outputs.files.singleFile,
"logging-format-output": runLoggingFormatExample.outputs.files.singleFile
}
from(runRemoteSpringApplicationExample) {
into "modules/ROOT/examples"
}
from(documentDevtoolsPropertyDefaults) {
into "modules/ROOT/partials/propertydefaults"
}
from(documentStarters) {
into "modules/ROOT/partials/starters"
}
from(documentTestSlices) {
into "modules/appendix/partials/slices"
}
from(runSpringApplicationExample) {
into "modules/ROOT/partials/application"
}
from(runLoggingFormatExample) {
into "modules/ROOT/partials/logging"
}
from(documentDependencyVersionCoordinates) {
into "modules/appendix/partials/dependency-versions"
}
from(documentDependencyVersionProperties) {
into "modules/appendix/partials/dependency-versions"
}
from(documentAutoConfigurationClasses) {
into "modules/appendix/partials/auto-configuration-classes"
}
from(documentConfigurationProperties) {
into "modules/appendix/partials/configuration-properties"
}
from(tasks.getByName("generateAntoraYml")) {
into "modules"
}
}
asciidoctor {
sources {
include "*.singleadoc"
}
}
task asciidoctorPdf(type: org.asciidoctor.gradle.jvm.AsciidoctorTask) {
sources {
include "*.singleadoc"
}
}
task asciidoctorMultipage(type: org.asciidoctor.gradle.jvm.AsciidoctorTask) {
sources {
include "*.adoc"
}
}
syncDocumentationSourceForAsciidoctor {
dependsOn documentTestSlices
dependsOn documentStarters
dependsOn documentAutoConfigurationClasses
dependsOn documentDependencyVersions
dependsOn documentVersionProperties
dependsOn documentConfigurationProperties
dependsOn documentDevtoolsPropertyDefaults
from("${buildDir}/docs/generated") {
into "asciidoc"
}
from("src/main/java") {
into "main/java"
}
from("src/test/java") {
into "test/java"
}
from("src/main/kotlin") {
into "main/kotlin"
}
from("src/main/groovy") {
into "main/groovy"
}
from("src/main/resources") {
into "main/resources"
}
}
syncDocumentationSourceForAsciidoctorMultipage {
dependsOn documentTestSlices
dependsOn documentStarters
dependsOn documentAutoConfigurationClasses
dependsOn documentDependencyVersions
dependsOn documentVersionProperties
dependsOn documentConfigurationProperties
dependsOn documentDevtoolsPropertyDefaults
from("${buildDir}/docs/generated") {
into "asciidoc"
}
from("src/main/java") {
into "main/java"
}
from("src/test/java") {
into "test/java"
}
from("src/main/kotlin") {
into "main/kotlin"
}
from("src/main/groovy") {
into "main/groovy"
}
from("src/main/resources") {
into "main/resources"
}
}
syncDocumentationSourceForAsciidoctorPdf {
dependsOn documentTestSlices
dependsOn documentStarters
dependsOn documentAutoConfigurationClasses
dependsOn documentDependencyVersions
dependsOn documentVersionProperties
dependsOn documentConfigurationProperties
dependsOn documentDevtoolsPropertyDefaults
from("${buildDir}/docs/generated") {
into "asciidoc"
}
from("src/main/java") {
into "main/java"
}
from("src/test/java") {
into "test/java"
}
from("src/main/kotlin") {
into "main/kotlin"
}
from("src/main/groovy") {
into "main/groovy"
}
from("src/main/resources") {
into "main/resources"
}
}
task zip(type: Zip) {
dependsOn asciidoctor,
asciidoctorMultipage,
asciidoctorPdf,
configurations.gradlePluginDocumentation,
configurations.actuatorApiDocumentation,
configurations.mavenPluginDocumentation
duplicatesStrategy "fail"
from(asciidoctor.outputDir) {
into "reference/htmlsingle"
}
from(asciidoctorPdf.outputDir) {
into "reference/pdf"
include "index.pdf"
rename { "spring-boot-reference.pdf" }
}
from(asciidoctorMultipage.outputDir) {
into "reference/html"
}
def antoraApiCatalogContent = tasks.register("antoraApiCatalogContent", Zip) {
destinationDirectory = layout.buildDirectory.dir('generated/docs/antora-content')
archiveClassifier = "api-catalog-content"
from(aggregatedJavadoc) {
into "api"
}
into("gradle-plugin") {
from {
zipTree(configurations.gradlePluginDocumentation.singleFile)
}
}
into("actuator-api") {
from {
zipTree(configurations.actuatorApiDocumentation.singleFile)
}
}
into("maven-plugin") {
from {
zipTree(configurations.mavenPluginDocumentation.singleFile)
}
into "java"
}
}
artifacts {
archives zip
def copyAntoraContentDependencies = tasks.register("copyAntoraContentDependencies", Copy) {
into layout.buildDirectory.dir('generated/docs/antora-dependencies-content')
from(configurations.antoraContent)
rename("spring-boot-actuator-autoconfigure", "spring-boot-docs")
rename("spring-boot-maven-plugin", "spring-boot-docs")
rename("spring-boot-gradle-plugin", "spring-boot-docs")
}
tasks.named("antora") {
inputs.files(antoraRootAggregateContent, antoraApiCatalogContent, copyAntoraContentDependencies)
}
gradle.projectsEvaluated {
def mavenPublication = publishing.publications.getByName("maven");
configurations.antoraContent.dependencies.forEach { dependency ->
dependency.dependencyProject.configurations.getByName(dependency.targetConfiguration)
.artifacts.forEach(mavenPublication::artifact)
}
}
publishing {
publications {
maven(MavenPublication) {
artifact zip
getByName("maven") {
artifact antoraRootAggregateContent
artifact antoraApiCatalogContent
}
}
}

View File

@ -0,0 +1,10 @@
name: spring-boot
ext:
zip_contents_collector:
include:
- name: root
classifier: aggregate-content
- name: api
classifier: catalog-content
module: api
destination: content-catalog

View File

@ -1,10 +1,9 @@
[[getting-help]]
= Getting Help
include::attributes.adoc[]
:navicon: question
= Community
If you have trouble with Spring Boot, we would like to help.
* Try the <<howto#howto, How-to documents>>.
* Try the xref:how-to:index.adoc[How-to documents].
They provide solutions to the most common questions.
* Learn the Spring basics.
Spring Boot builds on many other Spring projects.
@ -15,4 +14,4 @@ We monitor https://stackoverflow.com[stackoverflow.com] for questions tagged wit
* Report bugs with Spring Boot at https://github.com/spring-projects/spring-boot/issues.
NOTE: All of Spring Boot is open source, including the documentation.
If you find problems with the docs or if you want to improve them, please {spring-boot-code}[get involved].
If you find problems with the docs or if you want to improve them, please {url-github}[get involved].

View File

@ -0,0 +1,156 @@
:navtitle: Documentation
:navicon: book
[[documentation]]
= Documentation Overview
This section provides a brief overview of Spring Boot reference documentation.
It serves as a map for the rest of the document.
[[documentation.first-steps]]
== First Steps
If you are getting started with Spring Boot or 'Spring' in general, start with the following topics:
* *From scratch:* xref:index.adoc[Overview] | xref:system-requirements.adoc[Requirements] | xref:installing.adoc[Installation]
* *Tutorial:* xref:tutorial:first-application/index.adoc[Part 1] | xref:tutorial:first-application/index.adoc#getting-started.first-application.code[Part 2]
* *Running your example:* xref:tutorial:first-application/index.adoc#getting-started.first-application.run[Part 1] | xref:tutorial:first-application/index.adoc#getting-started.first-application.executable-jar[Part 2]
[[documentation.upgrading]]
== Upgrading From an Earlier Version
You should always ensure that you are running a {url-github-wiki}/Supported-Versions[supported version] of Spring Boot.
Depending on the version that you are upgrading to, you can find some additional tips here:
* *From 1.x:* xref:upgrading.adoc#upgrading.from-1x[Upgrading from 1.x]
* *To a new feature release:* xref:upgrading.adoc#upgrading.to-feature[Upgrading to New Feature Release]
* *Spring Boot CLI:* xref:upgrading.adoc#upgrading.cli[Upgrading the Spring Boot CLI]
[[documentation.using]]
== Developing With Spring Boot
Ready to actually start using Spring Boot? xref:reference:using/index.adoc[We have you covered]:
* *Build systems:* xref:reference:using/build-systems.adoc#using.build-systems.maven[Maven] | xref:reference:using/build-systems.adoc#using.build-systems.gradle[Gradle] | xref:reference:using/build-systems.adoc#using.build-systems.ant[Ant] | xref:reference:using/build-systems.adoc#using.build-systems.starters[Starters]
* *Best practices:* xref:reference:using/structuring-your-code.adoc[Code Structure] | xref:reference:using/configuration-classes.adoc[@Configuration] | xref:reference:using/auto-configuration.adoc[@EnableAutoConfiguration] | xref:reference:using/spring-beans-and-dependency-injection.adoc[Beans and Dependency Injection]
* *Running your code:* xref:reference:using/running-your-application.adoc#using.running-your-application.from-an-ide[IDE] | xref:reference:using/running-your-application.adoc#using.running-your-application.as-a-packaged-application[Packaged] | xref:reference:using/running-your-application.adoc#using.running-your-application.with-the-maven-plugin[Maven] | xref:reference:using/running-your-application.adoc#using.running-your-application.with-the-gradle-plugin[Gradle]
* *Packaging your app:* xref:reference:using/packaging-for-production.adoc[Production jars]
* *Spring Boot CLI:* xref:cli:index.adoc[Using the CLI]
[[documentation.features]]
== Learning About Spring Boot Features
Need more details about Spring Boot's core features?
xref:reference:features/index.adoc[The following content is for you]:
* *Spring Application:* xref:reference:features/spring-application.adoc[SpringApplication]
* *External Configuration:* xref:reference:features/external-config.adoc[External Configuration]
* *Profiles:* xref:reference:features/profiles.adoc[Profiles]
* *Logging:* xref:reference:features/logging.adoc[Logging]
[[documentation.web]]
== Web
If you develop Spring Boot web applications, take a look at the following content:
* *Servlet Web Applications:* xref:reference:web/servlet.adoc[Spring MVC, Jersey, Embedded Servlet Containers]
* *Reactive Web Applications:* xref:reference:web/reactive.adoc[Spring Webflux, Embedded Servlet Containers]
* *Graceful Shutdown:* xref:reference:web/graceful-shutdown.adoc[Graceful Shutdown]
* *Spring Security:* xref:reference:web/spring-security.adoc[Default Security Configuration, Auto-configuration for OAuth2, SAML]
* *Spring Session:* xref:reference:web/spring-session.adoc[Auto-configuration for Spring Session]
* *Spring HATEOAS:* xref:reference:web/spring-hateoas.adoc[Auto-configuration for Spring HATEOAS]
[[documentation.data]]
== Data
If your application deals with a datastore, you can see how to configure that here:
* *SQL:* xref:reference:data/sql.adoc[Configuring a SQL Datastore, Embedded Database support, Connection pools, and more.]
* *NOSQL:* xref:reference:data/nosql.adoc[Auto-configuration for NOSQL stores such as Redis, MongoDB, Neo4j, and others.]
[[documentation.messaging]]
== Messaging
If your application uses any messaging protocol, see one or more of the following sections:
* *JMS:* xref:reference:messaging/jms.adoc[Auto-configuration for ActiveMQ and Artemis, Sending and Receiving messages through JMS]
* *AMQP:* xref:reference:messaging/amqp.adoc[Auto-configuration for RabbitMQ]
* *Kafka:* xref:reference:messaging/kafka.adoc[Auto-configuration for Spring Kafka]
* *Pulsar:* xref:reference:messaging/pulsar.adoc[Auto-configuration for Spring for Apache Pulsar]
* *RSocket:* xref:reference:messaging/rsocket.adoc[Auto-configuration for Spring Framework's RSocket Support]
* *Spring Integration:* xref:reference:messaging/spring-integration.adoc[Auto-configuration for Spring Integration]
[[documentation.io]]
== IO
If your application needs IO capabilities, see one or more of the following sections:
* *Caching:* xref:reference:io/caching.adoc[Caching support with EhCache, Hazelcast, Infinispan, and more]
* *Quartz:* xref:reference:io/quartz.adoc[Quartz Scheduling]
* *Mail:* xref:reference:io/email.adoc[Sending Email]
* *Validation:* xref:reference:io/validation.adoc[JSR-303 Validation]
* *REST Clients:* xref:reference:io/rest-client.adoc[Calling REST Services with RestTemplate and WebClient]
* *Webservices:* xref:reference:io/webservices.adoc[Auto-configuration for Spring Web Services]
* *JTA:* xref:reference:io/jta.adoc[Distributed Transactions with JTA]
[[documentation.container-images]]
== Container Images
Spring Boot provides first-class support for building efficient container images. You can read more about it here:
* *Efficient Container Images:* xref:reference:container-images/efficient-images.adoc[Tips to optimize container images such as Docker images]
* *Dockerfiles:* xref:reference:container-images/dockerfiles.adoc[Building container images using dockerfiles]
* *Cloud Native Buildpacks:* xref:reference:container-images/cloud-native-buildpacks.adoc[Support for Cloud Native Buildpacks with Maven and Gradle]
[[documentation.actuator]]
== Moving to Production
When you are ready to push your Spring Boot application to production, we have xref:how-to:actuator.adoc[some tricks] that you might like:
* *Management endpoints:* xref:reference:actuator/endpoints.adoc[Overview]
* *Connection options:* xref:reference:actuator/monitoring.adoc[HTTP] | xref:reference:actuator/jmx.adoc[JMX]
* *Monitoring:* xref:reference:actuator/metrics.adoc[Metrics] | xref:reference:actuator/auditing.adoc[Auditing] | xref:reference:actuator/http-exchanges.adoc[HTTP Exchanges] | xref:reference:actuator/process-monitoring.adoc[Process]
[[documentation.native-images]]
== GraalVM Native Images
Spring Boot applications can be converted into native executables using GraalVM.
You can read more about our native image support here:
* *GraalVM Native Images:* xref:reference:native-image/introducing-graalvm-native-images.adoc[Introduction] | xref:reference:native-image/introducing-graalvm-native-images.adoc#native-image.introducing-graalvm-native-images.key-differences-with-jvm-deployments[Key Differences with the JVM] | xref:reference:native-image/introducing-graalvm-native-images.adoc#native-image.introducing-graalvm-native-images.understanding-aot-processing[Ahead-of-Time Processing]
* *Getting Started:* xref:reference:native-image/developing-your-first-application.adoc#native-image.developing-your-first-application.buildpacks[Buildpacks] | xref:reference:native-image/developing-your-first-application.adoc#native-image.developing-your-first-application.native-build-tools[Native Build Tools]
* *Testing:* xref:reference:native-image/testing-native-applications.adoc#native-image.testing.with-the-jvm[JVM] | xref:reference:native-image/testing-native-applications.adoc#native-image.testing.with-native-build-tools[Native Build Tools]
* *Advanced Topics:* xref:reference:native-image/advanced-topics.adoc#native-image.advanced.nested-configuration-properties[Nested Configuration Properties] | xref:reference:native-image/advanced-topics.adoc#native-image.advanced.converting-executable-jars[Converting JARs] | xref:reference:native-image/advanced-topics.adoc#native-image.advanced.known-limitations[Known Limitations]
[[documentation.advanced]]
== Advanced Topics
Finally, we have a few topics for more advanced users:
* *Spring Boot Applications Deployment:* xref:reference:deployment/cloud.adoc[Cloud Deployment] | xref:reference:deployment/installing.adoc[OS Service]
* *Build tool plugins:* xref:maven-plugin:index.adoc[Maven] | xref:gradle-plugin:index.adoc[Gradle]
* *Appendix:* xref:appendix:application-properties/index.adoc[Application Properties] | xref:specification:configuration-metadata/index.adoc[Configuration Metadata] | xref:appendix:auto-configuration-classes/index.adoc[Auto-configuration Classes] | xref:appendix:test-auto-configuration/index.adoc[Test Auto-configuration Annotations] | xref:specification:executable-jar/index.adoc[Executable Jars] | xref:appendix:dependency-versions/index.adoc[Dependency Versions]

View File

@ -1,5 +1,7 @@
[[getting-started.introducing-spring-boot]]
== Introducing Spring Boot
:navtitle: Overview
:navicon: home
= Spring Boot
Spring Boot helps you to create stand-alone, production-grade Spring-based applications that you can run.
We take an opinionated view of the Spring platform and third-party libraries, so that you can get started with minimum fuss.
Most Spring Boot applications need very little Spring configuration.

View File

@ -1,21 +1,24 @@
:navicon: gift
[[getting-started.installing]]
== Installing Spring Boot
= Installing Spring Boot
Spring Boot can be used with "`classic`" Java development tools or installed as a command line tool.
Either way, you need https://www.java.com[Java SDK v17] or higher.
Before you begin, you should check your current Java installation by using the following command:
[source,shell,indent=0,subs="verbatim"]
[source,shell]
----
$ java -version
$ java -version
----
If you are new to Java development or if you want to experiment with Spring Boot, you might want to try the <<getting-started#getting-started.installing.cli, Spring Boot CLI>> (Command Line Interface) first.
If you are new to Java development or if you want to experiment with Spring Boot, you might want to try the xref:installing.adoc#getting-started.installing.cli[Spring Boot CLI] (Command Line Interface) first.
Otherwise, read on for "`classic`" installation instructions.
[[getting-started.installing.java]]
=== Installation Instructions for the Java Developer
== Installation Instructions for the Java Developer
You can use Spring Boot in the same way as any standard Java library.
To do so, include the appropriate `+spring-boot-*.jar+` files on your classpath.
Spring Boot does not require any special tools integration, so you can use any IDE or text editor.
@ -26,7 +29,8 @@ Although you _could_ copy Spring Boot jars, we generally recommend that you use
[[getting-started.installing.java.maven]]
==== Maven Installation
=== Maven Installation
Spring Boot is compatible with Apache Maven 3.6.3 or later.
If you do not already have Maven installed, you can follow the instructions at https://maven.apache.org.
@ -36,35 +40,37 @@ Ubuntu users can run `sudo apt-get install maven`.
Windows users with https://chocolatey.org/[Chocolatey] can run `choco install maven` from an elevated (administrator) prompt.
Spring Boot dependencies use the `org.springframework.boot` group id.
Typically, your Maven POM file inherits from the `spring-boot-starter-parent` project and declares dependencies to one or more <<using#using.build-systems.starters,"`Starters`">>.
Spring Boot also provides an optional <<build-tool-plugins#build-tool-plugins.maven, Maven plugin>> to create executable jars.
Typically, your Maven POM file inherits from the `spring-boot-starter-parent` project and declares dependencies to one or more xref:reference:using/build-systems.adoc#using.build-systems.starters["`Starters`"].
Spring Boot also provides an optional xref:maven-plugin:index.adoc[Maven plugin] to create executable jars.
More details on getting started with Spring Boot and Maven can be found in the {spring-boot-maven-plugin-docs}#getting-started[Getting Started section] of the Maven plugin's reference guide.
More details on getting started with Spring Boot and Maven can be found in the xref:maven-plugin:getting-started.adoc[Getting Started section] of the Maven plugin's reference guide.
[[getting-started.installing.java.gradle]]
==== Gradle Installation
=== Gradle Installation
Spring Boot is compatible with Gradle 7.x (7.5 or later) and 8.x.
If you do not already have Gradle installed, you can follow the instructions at https://gradle.org.
Spring Boot dependencies can be declared by using the `org.springframework.boot` `group`.
Typically, your project declares dependencies to one or more <<using#using.build-systems.starters, "`Starters`">>.
Spring Boot provides a useful <<build-tool-plugins#build-tool-plugins.gradle, Gradle plugin>> that can be used to simplify dependency declarations and to create executable jars.
Typically, your project declares dependencies to one or more xref:reference:using/build-systems.adoc#using.build-systems.starters["`Starters`"].
Spring Boot provides a useful xref:gradle-plugin:index.adoc[Gradle plugin] that can be used to simplify dependency declarations and to create executable jars.
.Gradle Wrapper
****
The Gradle Wrapper provides a nice way of "`obtaining`" Gradle when you need to build a project.
It is a small script and library that you commit alongside your code to bootstrap the build process.
See {gradle-docs}/gradle_wrapper.html for details.
See {url-gradle-docs}/gradle_wrapper.html for details.
****
More details on getting started with Spring Boot and Gradle can be found in the {spring-boot-gradle-plugin-docs}#getting-started[Getting Started section] of the Gradle plugin's reference guide.
More details on getting started with Spring Boot and Gradle can be found in the xref:gradle-plugin:getting-started.adoc[Getting Started section] of the Gradle plugin's reference guide.
[[getting-started.installing.cli]]
=== Installing the Spring Boot CLI
== Installing the Spring Boot CLI
The Spring Boot CLI (Command Line Interface) is a command line tool that you can use to quickly prototype with Spring.
You do not need to use the CLI to work with Spring Boot, but it is a quick way to get a Spring application off the ground without an IDE.
@ -72,44 +78,46 @@ You do not need to use the CLI to work with Spring Boot, but it is a quick way t
[[getting-started.installing.cli.manual-installation]]
==== Manual Installation
=== Manual Installation
ifeval::["{artifact-release-type}" == "snapshot"]
You can download one of the `spring-boot-cli-\*-bin.zip` or `spring-boot-cli-*-bin.tar.gz` files from the {artifact-download-repo}/org/springframework/boot/spring-boot-cli/{spring-boot-version}/[Spring software repository].
You can download one of the `spring-boot-cli-\*-bin.zip` or `spring-boot-cli-*-bin.tar.gz` files from the {url-artifact-repository}/org/springframework/boot/spring-boot-cli/{version-spring-boot}/[Spring software repository].
endif::[]
ifeval::["{artifact-release-type}" != "snapshot"]
You can download the Spring CLI distribution from one of the following locations:
* {artifact-download-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]
* {artifact-download-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]
* {url-artifact-repository}/org/springframework/boot/spring-boot-cli/{version-spring-boot}/spring-boot-cli-{version-spring-boot}-bin.zip[spring-boot-cli-{version-spring-boot}-bin.zip]
* {url-artifact-repository}/org/springframework/boot/spring-boot-cli/{version-spring-boot}/spring-boot-cli-{version-spring-boot}-bin.tar.gz[spring-boot-cli-{version-spring-boot}-bin.tar.gz]
endif::[]
Once downloaded, follow the {github-raw}/spring-boot-project/spring-boot-tools/spring-boot-cli/src/main/content/INSTALL.txt[INSTALL.txt] instructions from the unpacked archive.
Once downloaded, follow the {url-github-raw}/spring-boot-project/spring-boot-tools/spring-boot-cli/src/main/content/INSTALL.txt[INSTALL.txt] 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 can use `java -jar` with the `.jar` file (the script helps you to be sure that the classpath is set correctly).
[[getting-started.installing.cli.sdkman]]
==== Installation with SDKMAN!
=== Installation with SDKMAN!
SDKMAN! (The Software Development Kit Manager) can be used for managing multiple versions of various binary SDKs, including Groovy and the Spring Boot CLI.
Get SDKMAN! from https://sdkman.io and install Spring Boot by using the following commands:
[source,shell,indent=0,subs="verbatim,attributes"]
[source,shell,subs="verbatim,attributes"]
----
$ sdk install springboot
$ spring --version
Spring CLI v{spring-boot-version}
$ sdk install springboot
$ spring --version
Spring CLI v{version-spring-boot}
----
If you develop features for the CLI and want access to the version you built, use the following commands:
[source,shell,indent=0,subs="verbatim,attributes"]
[source,shell,subs="verbatim,attributes"]
----
$ sdk install springboot dev /path/to/spring-boot/spring-boot-cli/target/spring-boot-cli-{spring-boot-version}-bin/spring-{spring-boot-version}/
$ sdk default springboot dev
$ spring --version
Spring CLI v{spring-boot-version}
$ sdk install springboot dev /path/to/spring-boot/spring-boot-cli/target/spring-boot-cli-{version-spring-boot}-bin/spring-{version-spring-boot}/
$ sdk default springboot dev
$ spring --version
Spring CLI v{version-spring-boot}
----
The preceding instructions install a local instance of `spring` called the `dev` instance.
@ -117,33 +125,34 @@ It points at your target build location, so every time you rebuild Spring Boot,
You can see it by running the following command:
[source,shell,indent=0,subs="verbatim,attributes"]
[source,shell,subs="verbatim,attributes"]
----
$ sdk ls springboot
$ sdk ls springboot
================================================================================
Available Springboot Versions
================================================================================
> + dev
* {spring-boot-version}
================================================================================
Available Springboot Versions
================================================================================
> + dev
* {version-spring-boot}
================================================================================
+ - local version
* - installed
> - currently in use
================================================================================
================================================================================
+ - local version
* - installed
> - currently in use
================================================================================
----
[[getting-started.installing.cli.homebrew]]
==== OSX Homebrew Installation
=== OSX Homebrew Installation
If you are on a Mac and use https://brew.sh/[Homebrew], you can install the Spring Boot CLI by using the following commands:
[source,shell,indent=0,subs="verbatim"]
[source,shell]
----
$ brew tap spring-io/tap
$ brew install spring-boot
$ brew tap spring-io/tap
$ brew install spring-boot
----
Homebrew installs `spring` to `/usr/local/bin`.
@ -154,28 +163,30 @@ In that case, run `brew update` and try again.
[[getting-started.installing.cli.macports]]
==== MacPorts Installation
=== MacPorts Installation
If you are on a Mac and use https://www.macports.org/[MacPorts], you can install the Spring Boot CLI by using the following command:
[source,shell,indent=0,subs="verbatim"]
[source,shell]
----
$ sudo port install spring-boot-cli
$ sudo port install spring-boot-cli
----
[[getting-started.installing.cli.completion]]
==== Command-line Completion
=== Command-line Completion
The Spring Boot CLI includes scripts that provide command completion for the https://en.wikipedia.org/wiki/Bash_%28Unix_shell%29[BASH] and https://en.wikipedia.org/wiki/Z_shell[zsh] shells.
You can `source` the script (also named `spring`) in any shell or put it in your personal or system-wide bash completion initialization.
On a Debian system, the system-wide scripts are in `<installation location>/shell-completion/bash` and all scripts in that directory are executed when a new shell starts.
For example, to run the script manually if you have installed by using SDKMAN!, use the following commands:
[source,shell,indent=0,subs="verbatim"]
[source,shell]
----
$ . ~/.sdkman/candidates/springboot/current/shell-completion/bash/spring
$ spring <HIT TAB HERE>
grab help jar run test version
$ . ~/.sdkman/candidates/springboot/current/shell-completion/bash/spring
$ spring <HIT TAB HERE>
grab help jar run test version
----
NOTE: If you install the Spring Boot CLI by using Homebrew or MacPorts, the command-line completion scripts are automatically registered with your shell.
@ -183,13 +194,14 @@ NOTE: If you install the Spring Boot CLI by using Homebrew or MacPorts, the comm
[[getting-started.installing.cli.scoop]]
==== Windows Scoop Installation
=== Windows Scoop Installation
If you are on a Windows and use https://scoop.sh/[Scoop], you can install the Spring Boot CLI by using the following commands:
[indent=0]
[source,shell]
----
> scoop bucket add extras
> scoop install springboot
$ scoop bucket add extras
$ scoop install springboot
----
Scoop installs `spring` to `~/scoop/apps/springboot/current/bin`.

View File

@ -1,7 +1,9 @@
:navicon: server
[[getting-started.system-requirements]]
== System Requirements
Spring Boot {spring-boot-version} requires https://www.java.com[Java 17] and is compatible up to and including Java 21.
{spring-framework-docs}/[Spring Framework {spring-framework-version}] or above is also required.
= System Requirements
Spring Boot {version-spring-boot} requires https://www.java.com[Java 17] and is compatible up to and including Java 21.
{url-spring-framework-docs}/[Spring Framework {version-spring-framework}] or above is also required.
Explicit build support is provided for the following build tools:
@ -18,7 +20,8 @@ Explicit build support is provided for the following build tools:
[[getting-started.system-requirements.servlet-containers]]
=== Servlet Containers
== Servlet Containers
Spring Boot supports the following embedded servlet containers:
|===
@ -39,8 +42,9 @@ You can also deploy Spring Boot applications to any servlet 5.0+ compatible cont
[[getting-started.system-requirements.graal]]
=== GraalVM Native Images
Spring Boot applications can be <<native-image#native-image.introducing-graalvm-native-images,converted into a Native Image>> using GraalVM {graal-version} or above.
== GraalVM Native Images
Spring Boot applications can be xref:reference:native-image/introducing-graalvm-native-images.adoc[converted into a Native Image] using GraalVM {version-graal} or above.
Images can be created using the https://github.com/graalvm/native-build-tools[native build tools] Gradle/Maven plugins or `native-image` tool provided by GraalVM.
You can also create native images using the https://github.com/paketo-buildpacks/native-image[native-image Paketo buildpack].
@ -51,8 +55,8 @@ The following versions are supported:
| Name | Version
| GraalVM Community
| {graal-version}
| {version-graal}
| Native Build Tools
| {native-build-tools-version}
| {version-native-build-tools}
|===

View File

@ -0,0 +1,47 @@
:navicon: rocket
[[upgrading]]
= Upgrading Spring Boot
Instructions for how to upgrade from earlier versions of Spring Boot are provided on the project {url-github-wiki}[wiki].
Follow the links in the {url-github-wiki}#release-notes[release notes] section to find the version that you want to upgrade to.
Upgrading instructions are always the first item in the release notes.
If you are more than one release behind, please make sure that you also review the release notes of the versions that you jumped.
[[upgrading.from-1x]]
== Upgrading From 1.x
If you are upgrading from the `1.x` release of Spring Boot, check the {url-github-wiki}/Spring-Boot-2.0-Migration-Guide["`migration guide`" on the project wiki] that provides detailed upgrade instructions.
Check also the {url-github-wiki}["`release notes`"] for a list of "`new and noteworthy`" features for each release.
[[upgrading.to-feature]]
== Upgrading to a New Feature Release
When upgrading to a new feature release, some properties may have been renamed or removed.
Spring Boot provides a way to analyze your application's environment and print diagnostics at startup, but also temporarily migrate properties at runtime for you.
To enable that feature, add the following dependency to your project:
[source,xml]
----
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-properties-migrator</artifactId>
<scope>runtime</scope>
</dependency>
----
WARNING: Properties that are added late to the environment, such as when using `@PropertySource`, will not be taken into account.
NOTE: Once you finish the migration, please make sure to remove this module from your project's dependencies.
[[upgrading.cli]]
== Upgrading the Spring Boot CLI
To upgrade an existing CLI installation, use the appropriate package manager command (for example, `brew upgrade`).
If you manually installed the CLI, follow the xref:installing.adoc#getting-started.installing.cli.manual-installation[standard instructions], remembering to update your `PATH` environment variable to remove any older references.

View File

@ -0,0 +1,6 @@
* xref:index.adoc[]
* xref:documentation.adoc[]
* xref:community.adoc[]
* xref:system-requirements.adoc[]
* xref:installing.adoc[]
* xref:upgrading.adoc[]

View File

@ -0,0 +1,4 @@
* Java APIs
** xref:api:java/index.html[Spring Boot]
** xref:gradle-plugin:api/java/index.html[Gradle Plugin]
** xref:maven-plugin:api/java/index.html[Maven Plugin]

View File

@ -0,0 +1,5 @@
* Rest APIs
+
--
include::api:partial$nav-actuator-rest-api.adoc[]
--

View File

@ -0,0 +1,48 @@
[appendix]
[[appendix.application-properties]]
= Common Application Properties
Various properties can be specified inside your `application.properties` file, inside your `application.yaml` file, or as command line switches.
This appendix provides a list of common Spring Boot properties and references to the underlying classes that consume them.
TIP: Spring Boot provides various conversion mechanism with advanced value formatting, make sure to review xref:reference:features/external-config.adoc#features.external-config.typesafe-configuration-properties.conversion[the properties conversion section].
NOTE: Property contributions can come from additional jar files on your classpath, so you should not consider this an exhaustive list.
Also, you can define your own properties.
include::partial$configuration-properties/core.adoc[]
include::partial$configuration-properties/cache.adoc[]
include::partial$configuration-properties/mail.adoc[]
include::partial$configuration-properties/json.adoc[]
include::partial$configuration-properties/data.adoc[]
include::partial$configuration-properties/transaction.adoc[]
include::partial$configuration-properties/data-migration.adoc[]
include::partial$configuration-properties/integration.adoc[]
include::partial$configuration-properties/web.adoc[]
include::partial$configuration-properties/templating.adoc[]
include::partial$configuration-properties/server.adoc[]
include::partial$configuration-properties/security.adoc[]
include::partial$configuration-properties/rsocket.adoc[]
include::partial$configuration-properties/actuator.adoc[]
include::partial$configuration-properties/devtools.adoc[]
include::partial$configuration-properties/docker-compose.adoc[]
include::partial$configuration-properties/testcontainers.adoc[]
include::partial$configuration-properties/testing.adoc[]

View File

@ -1,5 +1,6 @@
[[appendix.auto-configuration-classes.actuator]]
== spring-boot-actuator-autoconfigure
= spring-boot-actuator-autoconfigure
The following auto-configuration classes are from the `spring-boot-actuator-autoconfigure` module:
include::documented-auto-configuration-classes/spring-boot-actuator-autoconfigure.adoc[]
include::partial$/auto-configuration-classes/spring-boot-actuator-autoconfigure.adoc[]

View File

@ -1,5 +1,6 @@
[[appendix.auto-configuration-classes.core]]
== spring-boot-autoconfigure
= spring-boot-autoconfigure
The following auto-configuration classes are from the `spring-boot-autoconfigure` module:
include::documented-auto-configuration-classes/spring-boot-autoconfigure.adoc[]
include::partial$/auto-configuration-classes/spring-boot-autoconfigure.adoc[]

View File

@ -1,16 +1,7 @@
[appendix]
[[appendix.auto-configuration-classes]]
= Auto-configuration Classes
include::attributes.adoc[]
This appendix contains details of all of the auto-configuration classes provided by Spring Boot, with links to documentation and source code.
Remember to also look at the conditions report in your application for more details of which features are switched on.
(To do so, start the app with `--debug` or `-Ddebug` or, in an Actuator application, use the `conditions` endpoint).
include::auto-configuration-classes/core.adoc[]
include::auto-configuration-classes/actuator.adoc[]

View File

@ -1,7 +1,7 @@
[[appendix.dependency-versions.coordinates]]
== Managed Dependency Coordinates
= Managed Dependency Coordinates
The following table provides details of all of the dependency versions that are provided by Spring Boot in its CLI (Command Line Interface), Maven dependency management, and Gradle plugin.
When you declare a dependency on one of these artifacts without declaring a version, the version listed in the table is used.
include::documented-coordinates.adoc[]
include::partial$dependency-versions/documented-coordinates.adoc[]

View File

@ -1,14 +1,5 @@
[appendix]
[[appendix.dependency-versions]]
= Dependency Versions
include::attributes.adoc[]
This appendix provides details of the dependencies that are managed by Spring Boot.
include::dependency-versions/coordinates.adoc[]
include::dependency-versions/properties.adoc[]

View File

@ -1,8 +1,8 @@
[[appendix.dependency-versions.properties]]
== Version Properties
= Version Properties
The following table provides all properties that can be used to override the versions managed by Spring Boot.
Browse the {spring-boot-code}/spring-boot-project/spring-boot-dependencies/build.gradle[`spring-boot-dependencies` build.gradle] for a complete list of dependencies.
You can learn how to customize these versions in your application in the <<build-tool-plugins#build-tool-plugins,Build Tool Plugins documentation>>.
Browse the {code-spring-boot}/spring-boot-project/spring-boot-dependencies/build.gradle[`spring-boot-dependencies` build.gradle] for a complete list of dependencies.
You can learn how to customize these versions in your application in the xref:build-tool-plugin:index.adoc[Build Tool Plugins documentation].
include::documented-properties.adoc[]
include::partial$dependency-versions/documented-properties.adoc[]

View File

@ -1,12 +1,5 @@
[appendix]
[[appendix.test-auto-configuration]]
= Test Auto-configuration Annotations
include::attributes.adoc[]
This appendix describes the `@...Test` auto-configuration annotations that Spring Boot provides to test slices of your application.
include::test-auto-configuration/slices.adoc[]

View File

@ -1,6 +1,6 @@
[[appendix.test-auto-configuration.slices]]
== Test Slices
= Test Slices
The following table lists the various `@...Test` annotations that can be used to test slices of your application and the auto-configuration that they import by default:
include::documented-slices.adoc[]
include::partial$slices/documented-slices.adoc[]

View File

@ -0,0 +1,13 @@
* Appendix
** xref:appendix:application-properties/index.adoc[]
** xref:appendix:auto-configuration-classes/index.adoc[]
*** xref:appendix:auto-configuration-classes/core.adoc[]
*** xref:appendix:auto-configuration-classes/actuator.adoc[]
** xref:appendix:test-auto-configuration/index.adoc[]
*** xref:appendix:test-auto-configuration/slices.adoc[]
** xref:appendix:dependency-versions/index.adoc[]
*** xref:appendix:dependency-versions/coordinates.adoc[]
*** xref:appendix:dependency-versions/properties.adoc[]

View File

@ -1,40 +1,43 @@
[[build-tool-plugins.antlib]]
== Spring Boot AntLib Module
= Spring Boot AntLib Module
The Spring Boot AntLib module provides basic Spring Boot support for Apache Ant.
You can use the module to create executable jars.
To use the module, you need to declare an additional `spring-boot` namespace in your `build.xml`, as shown in the following example:
[source,xml,indent=0,subs="verbatim"]
[source,xml]
----
<project xmlns:ivy="antlib:org.apache.ivy.ant"
xmlns:spring-boot="antlib:org.springframework.boot.ant"
name="myapp" default="build">
...
</project>
<project xmlns:ivy="antlib:org.apache.ivy.ant"
xmlns:spring-boot="antlib:org.springframework.boot.ant"
name="myapp" default="build">
...
</project>
----
You need to remember to start Ant using the `-lib` option, as shown in the following example:
[source,shell,indent=0,subs="verbatim,attributes"]
[source,shell,subs="verbatim,attributes"]
----
$ ant -lib <directory containing spring-boot-antlib-{spring-boot-version}.jar>
$ ant -lib <directory containing spring-boot-antlib-{version-spring-boot}.jar>
----
TIP: The "`Using Spring Boot`" section includes a more complete example of <<using#using.build-systems.ant, using Apache Ant with `spring-boot-antlib`>>.
TIP: The "`Using Spring Boot`" section includes a more complete example of xref:reference:using/build-systems.adoc#using.build-systems.ant[using Apache Ant with `spring-boot-antlib`].
[[build-tool-plugins.antlib.tasks]]
=== Spring Boot Ant Tasks
== Spring Boot Ant Tasks
Once the `spring-boot-antlib` namespace has been declared, the following additional tasks are available:
* <<build-tool-plugins#build-tool-plugins.antlib.tasks.exejar>>
* <<build-tool-plugins#build-tool-plugins.antlib.findmainclass>>
* xref:antlib.adoc#build-tool-plugins.antlib.tasks.exejar[Using the "`exejar`" Task]
* xref:antlib.adoc#build-tool-plugins.antlib.findmainclass[Using the "`findmainclass`" Task]
[[build-tool-plugins.antlib.tasks.exejar]]
==== Using the "`exejar`" Task
=== Using the "`exejar`" Task
You can use the `exejar` task to create a Spring Boot executable jar.
The following attributes are supported by the task:
@ -62,46 +65,48 @@ The following nested elements can be used with the task:
| Element | Description
| `resources`
| One or more {ant-docs}/Types/resources.html#collection[Resource Collections] describing a set of {ant-docs}/Types/resources.html[Resources] that should be added to the content of the created +jar+ file.
| One or more {url-ant-docs}/Types/resources.html#collection[Resource Collections] describing a set of {url-ant-docs}/Types/resources.html[Resources] that should be added to the content of the created +jar+ file.
| `lib`
| One or more {ant-docs}/Types/resources.html#collection[Resource Collections] that should be added to the set of jar libraries that make up the runtime dependency classpath of the application.
| One or more {url-ant-docs}/Types/resources.html#collection[Resource Collections] that should be added to the set of jar libraries that make up the runtime dependency classpath of the application.
|====
[[build-tool-plugins.antlib.tasks.examples]]
==== Examples
=== Examples
This section shows two examples of Ant tasks.
.Specify +start-class+
[source,xml,indent=0,subs="verbatim"]
[source,xml]
----
<spring-boot:exejar destfile="target/my-application.jar"
classes="target/classes" start-class="com.example.MyApplication">
<resources>
<fileset dir="src/main/resources" />
</resources>
<lib>
<fileset dir="lib" />
</lib>
</spring-boot:exejar>
<spring-boot:exejar destfile="target/my-application.jar"
classes="target/classes" start-class="com.example.MyApplication">
<resources>
<fileset dir="src/main/resources" />
</resources>
<lib>
<fileset dir="lib" />
</lib>
</spring-boot:exejar>
----
.Detect +start-class+
[source,xml,indent=0,subs="verbatim"]
[source,xml]
----
<exejar destfile="target/my-application.jar" classes="target/classes">
<lib>
<fileset dir="lib" />
</lib>
</exejar>
<exejar destfile="target/my-application.jar" classes="target/classes">
<lib>
<fileset dir="lib" />
</lib>
</exejar>
----
[[build-tool-plugins.antlib.findmainclass]]
=== Using the "`findmainclass`" Task
== Using the "`findmainclass`" Task
The `findmainclass` task is used internally by `exejar` to locate a class declaring a `main`.
If necessary, you can also use this task directly in your build.
The following attributes are supported:
@ -126,23 +131,24 @@ The following attributes are supported:
[[build-tool-plugins.antlib.findmainclass.examples]]
==== Examples
=== Examples
This section contains three examples of using `findmainclass`.
.Find and log
[source,xml,indent=0,subs="verbatim"]
[source,xml]
----
<findmainclass classesroot="target/classes" />
<findmainclass classesroot="target/classes" />
----
.Find and set
[source,xml,indent=0,subs="verbatim"]
[source,xml]
----
<findmainclass classesroot="target/classes" property="main-class" />
<findmainclass classesroot="target/classes" property="main-class" />
----
.Override and set
[source,xml,indent=0,subs="verbatim"]
[source,xml]
----
<findmainclass mainclass="com.example.MainClass" property="main-class" />
<findmainclass mainclass="com.example.MainClass" property="main-class" />
----

View File

@ -0,0 +1,8 @@
[[build-tool-plugins]]
= Build Tool Plugins
Spring Boot provides build tool plugins for Maven and Gradle.
The plugins offer a variety of features, including the packaging of executable jars.
This section provides more details on both plugins as well as some help should you need to extend an unsupported build system.
If you are just getting started, you might want to read "`xref:reference:using/build-systems.adoc[Build Systems]`" from the "`xref:reference:using/index.adoc[Developing with Spring Boot]`" section first.

View File

@ -1,7 +1,8 @@
[[build-tool-plugins.other-build-systems]]
== Supporting Other Build Systems
= Supporting Other Build Systems
If you want to use a build tool other than Maven, Gradle, or Ant, you likely need to develop your own plugin.
Executable jars need to follow a specific format and certain entries need to be written in an uncompressed form (see the "`<<executable-jar#appendix.executable-jar, executable jar format>>`" section in the appendix for details).
Executable jars need to follow a specific format and certain entries need to be written in an uncompressed form (see the "`xref:specification:/executable-jar/index.adoc[executable jar format]`" section in the appendix for details).
The Spring Boot Maven and Gradle plugins both make use of `spring-boot-loader-tools` to actually generate jars.
If you need to, you may use this library directly.
@ -9,7 +10,8 @@ If you need to, you may use this library directly.
[[build-tool-plugins.other-build-systems.repackaging-archives]]
=== Repackaging Archives
== Repackaging Archives
To repackage an existing archive so that it becomes a self-contained executable archive, use `org.springframework.boot.loader.tools.Repackager`.
The `Repackager` class takes a single constructor argument that refers to an existing jar or war archive.
Use one of the two available `repackage()` methods to either replace the original file or write to a new destination.
@ -18,7 +20,8 @@ Various settings can also be configured on the repackager before it is run.
[[build-tool-plugins.other-build-systems.nested-libraries]]
=== Nested Libraries
== Nested Libraries
When repackaging an archive, you can include references to dependency files by using the `org.springframework.boot.loader.tools.Libraries` interface.
We do not provide any concrete implementations of `Libraries` here as they are usually build-system-specific.
@ -27,14 +30,16 @@ If your archive already includes libraries, you can use `Libraries.NONE`.
[[build-tool-plugins.other-build-systems.finding-main-class]]
=== Finding a Main Class
== Finding a Main Class
If you do not use `Repackager.setMainClass()` to specify a main class, the repackager uses https://asm.ow2.io/[ASM] to read class files and tries to find a suitable class with a `public static void main(String[] args)` method.
An exception is thrown if more than one candidate is found.
[[build-tool-plugins.other-build-systems.example-repackage-implementation]]
=== Example Repackage Implementation
== Example Repackage Implementation
The following example shows a typical repackage implementation:
include::code:MyBuildTool[]
include-code::MyBuildTool[]

View File

@ -0,0 +1,11 @@
* xref:build-tool-plugin:index.adoc[]
+
--
include::maven-plugin:partial$nav-maven-plugin.adoc[]
--
+
--
include::gradle-plugin:partial$nav-gradle-plugin.adoc[]
--
** xref:build-tool-plugin:antlib.adoc[]
** xref:build-tool-plugin:other-build-systems.adoc[]

View File

@ -1,12 +1,5 @@
[[cli]]
= Spring Boot CLI
include::attributes.adoc[]
The Spring Boot CLI is a command line tool that you can use to bootstrap a new project from https://start.spring.io or encode a password.
include::cli/installation.adoc[]
include::cli/using-the-cli.adoc[]

View File

@ -0,0 +1,5 @@
[[cli.installation]]
= Installing the CLI
The Spring Boot CLI (Command-Line Interface) can be installed manually by using SDKMAN! (the SDK Manager) or by using Homebrew or MacPorts if you are an OSX user.
See xref:ROOT:installing.adoc#getting-started.installing.cli[_Installing the Spring Boot CLI_] in the "`Getting started`" section for comprehensive installation instructions.

View File

@ -0,0 +1,177 @@
[[cli.using-the-cli]]
= Using the CLI
Once you have installed the CLI, you can run it by typing `spring` and pressing Enter at the command line.
If you run `spring` without any arguments, a help screen is displayed, as follows:
[source,shell]
----
$ spring
usage: spring [--help] [--version]
<command> [<args>]
Available commands are:
init [options] [location]
Initialize a new project using Spring Initializr (start.spring.io)
encodepassword [options] <password to encode>
Encode a password for use with Spring Security
shell
Start a nested shell
Common options:
--debug Verbose mode
Print additional status information for the command you are running
See 'spring help <command>' for more information on a specific command.
----
You can type `spring help` to get more details about any of the supported commands, as shown in the following example:
[source,shell]
----
$ spring help init
spring init - Initialize a new project using Spring Initializr (start.spring.io)
usage: spring init [options] [location]
Option Description
------ -----------
-a, --artifact-id <String> Project coordinates; infer archive name (for
example 'test')
-b, --boot-version <String> Spring Boot version (for example '1.2.0.RELEASE')
--build <String> Build system to use (for example 'maven' or
'gradle') (default: maven)
-d, --dependencies <String> Comma-separated list of dependency identifiers to
include in the generated project
--description <String> Project description
-f, --force Force overwrite of existing files
--format <String> Format of the generated content (for example
'build' for a build file, 'project' for a
project archive) (default: project)
-g, --group-id <String> Project coordinates (for example 'org.test')
-j, --java-version <String> Language level (for example '1.8')
-l, --language <String> Programming language (for example 'java')
--list List the capabilities of the service. Use it to
discover the dependencies and the types that are
available
-n, --name <String> Project name; infer application name
-p, --packaging <String> Project packaging (for example 'jar')
--package-name <String> Package name
-t, --type <String> Project type. Not normally needed if you use --
build and/or --format. Check the capabilities of
the service (--list) for more details
--target <String> URL of the service to use (default: https://start.
spring.io)
-v, --version <String> Project version (for example '0.0.1-SNAPSHOT')
-x, --extract Extract the project archive. Inferred if a
location is specified without an extension
examples:
To list all the capabilities of the service:
$ spring init --list
To creates a default project:
$ spring init
To create a web my-app.zip:
$ spring init -d=web my-app.zip
To create a web/data-jpa gradle project unpacked:
$ spring init -d=web,jpa --build=gradle my-dir
----
The `version` command provides a quick way to check which version of Spring Boot you are using, as follows:
[source,shell,subs="verbatim,attributes"]
----
$ spring version
Spring CLI v{version-spring-boot}
----
[[cli.using-the-cli.initialize-new-project]]
== Initialize a New Project
The `init` command lets you create a new project by using https://start.spring.io without leaving the shell, as shown in the following example:
[source,shell]
----
$ spring init --dependencies=web,data-jpa my-project
Using service at https://start.spring.io
Project extracted to '/Users/developer/example/my-project'
----
The preceding example creates a `my-project` directory with a Maven-based project that uses `spring-boot-starter-web` and `spring-boot-starter-data-jpa`.
You can list the capabilities of the service by using the `--list` flag, as shown in the following example:
[source,shell]
----
$ spring init --list
=======================================
Capabilities of https://start.spring.io
=======================================
Available dependencies:
-----------------------
actuator - Actuator: Production ready features to help you monitor and manage your application
...
web - Web: Support for full-stack web development, including Tomcat and spring-webmvc
websocket - Websocket: Support for WebSocket development
ws - WS: Support for Spring Web Services
Available project types:
------------------------
gradle-build - Gradle Config [format:build, build:gradle]
gradle-project - Gradle Project [format:project, build:gradle]
maven-build - Maven POM [format:build, build:maven]
maven-project - Maven Project [format:project, build:maven] (default)
...
----
The `init` command supports many options.
See the `help` output for more details.
For instance, the following command creates a Gradle project that uses Java 17 and `war` packaging:
[source,shell]
----
$ spring init --build=gradle --java-version=17 --dependencies=websocket --packaging=war sample-app.zip
Using service at https://start.spring.io
Content saved to 'sample-app.zip'
----
[[cli.using-the-cli.embedded-shell]]
== Using the Embedded Shell
Spring Boot includes command-line completion scripts for the BASH and zsh shells.
If you do not use either of these shells (perhaps you are a Windows user), you can use the `shell` command to launch an integrated shell, as shown in the following example:
[source,shell,subs="verbatim,quotes,attributes"]
----
$ spring shell
*Spring Boot* (v{version-spring-boot})
Hit TAB to complete. Type \'help' and hit RETURN for help, and \'exit' to quit.
----
From inside the embedded shell, you can run other commands directly:
[source,shell,subs="verbatim,attributes"]
----
$ version
Spring CLI v{version-spring-boot}
----
The embedded shell supports ANSI color output as well as `tab` completion.
If you need to run a native command, you can use the `!` prefix.
To exit the embedded shell, press `ctrl-c`.

View File

@ -0,0 +1,4 @@
* xref:cli:index.adoc[]
** xref:cli:installation.adoc[]
** xref:cli:using-the-cli.adoc[]

View File

@ -1,22 +1,25 @@
[[howto.actuator]]
== Actuator
= Actuator
Spring Boot includes the Spring Boot Actuator.
This section answers questions that often arise from its use.
[[howto.actuator.change-http-port-or-address]]
=== Change the HTTP Port or Address of the Actuator Endpoints
== Change the HTTP Port or Address of the Actuator Endpoints
In a standalone application, the Actuator HTTP port defaults to the same as the main HTTP port.
To make the application listen on a different port, set the external property: configprop:management.server.port[].
To listen on a completely different network address (such as when you have an internal network for management and an external one for user applications), you can also set `management.server.address` to a valid IP address to which the server is able to bind.
For more detail, see the {spring-boot-actuator-autoconfigure-module-code}/web/server/ManagementServerProperties.java[`ManagementServerProperties`] source code and "`<<actuator#actuator.monitoring.customizing-management-server-port>>`" in the "`Production-ready features`" section.
For more detail, see the {code-spring-boot-actuator-autoconfigure-src}/web/server/ManagementServerProperties.java[`ManagementServerProperties`] source code and "`xref:reference:actuator/monitoring.adoc#actuator.monitoring.customizing-management-server-port[Customizing the Management Server Port]`" in the "`Production-ready features`" section.
[[howto.actuator.customize-whitelabel-error-page]]
=== Customize the '`whitelabel`' Error Page
== Customize the '`whitelabel`' Error Page
Spring Boot installs a '`whitelabel`' error page that you see in a browser client if you encounter a server error (machine clients consuming JSON and other media types should see a sensible response with the right error code).
NOTE: Set `server.error.whitelabel.enabled=false` to switch the default error page off.
@ -28,14 +31,15 @@ For example, if you use Thymeleaf, you can add an `error.html` template.
If you use FreeMarker, you can add an `error.ftlh` template.
In general, you need a `View` that resolves with a name of `error` or a `@Controller` that handles the `/error` path.
Unless you replaced some of the default configuration, you should find a `BeanNameViewResolver` in your `ApplicationContext`, so a `@Bean` named `error` would be one way of doing that.
See {spring-boot-autoconfigure-module-code}/web/servlet/error/ErrorMvcAutoConfiguration.java[`ErrorMvcAutoConfiguration`] for more options.
See {code-spring-boot-autoconfigure-src}/web/servlet/error/ErrorMvcAutoConfiguration.java[`ErrorMvcAutoConfiguration`] for more options.
See also the section on "`<<web#web.servlet.spring-mvc.error-handling, Error Handling>>`" for details of how to register handlers in the servlet container.
See also the section on "`xref:reference:web/servlet.adoc#web.servlet.spring-mvc.error-handling[Error Handling]`" for details of how to register handlers in the servlet container.
[[howto.actuator.customizing-sanitization]]
=== Customizing Sanitization
== Customizing Sanitization
To take control over the sanitization, define a `SanitizingFunction` bean.
The `SanitizableData` with which the function is called provides access to the key and value as well as the `PropertySource` from which they came.
This allows you to, for example, sanitize every value that comes from a particular property source.
@ -44,7 +48,8 @@ Each `SanitizingFunction` is called in order until a function changes the value
[[howto.actuator.map-health-indicators-to-metrics]]
=== Map Health Indicators to Micrometer Metrics
== Map Health Indicators to Micrometer Metrics
Spring Boot health indicators return a `Status` type to indicate the overall system health.
If you want to monitor or alert on levels of health for a particular application, you can export these statuses as metrics with Micrometer.
By default, the status codes "`UP`", "`DOWN`", "`OUT_OF_SERVICE`" and "`UNKNOWN`" are used by Spring Boot.
@ -52,4 +57,4 @@ To export these, you will need to convert these states to some set of numbers so
The following example shows one way to write such an exporter:
include::code:MyHealthMetricsExportConfiguration[]
include-code::MyHealthMetricsExportConfiguration[]

View File

@ -0,0 +1,55 @@
[[howto.aot]]
= Ahead-of-time processing
A number of questions often arise when people use the ahead-of-time processing of Spring Boot applications.
This section addresses those questions.
[[howto.aot.conditions]]
== Conditions
Ahead-of-time processing optimizes the application and evaluates {url-spring-framework-javadoc}/org/springframework/context/annotation/Conditional.html[conditions] based on the environment at build time.
xref:reference:features/profiles.adoc[Profiles] are implemented through conditions and are therefore affected, too.
If you want beans that are created based on a condition in an ahead-of-time optimized application, you have to set up the environment when building the application.
The beans which are created while ahead-of-time processing at build time are then always created when running the application and can't be switched off.
To do this, you can set the profiles which should be used when building the application.
For Maven, this works by setting the `profiles` configuration of the `spring-boot-maven-plugin:process-aot` execution:
[source,xml]
----
<profile>
<id>native</id>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<id>process-aot</id>
<configuration>
<profiles>profile-a,profile-b</profiles>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
</build>
</profile>
----
For Gradle, you need to configure the `ProcessAot` task:
[source,gradle]
----
tasks.withType(org.springframework.boot.gradle.tasks.aot.ProcessAot).configureEach {
args('--spring.profiles.active=profile-a,profile-b')
}
----
Profiles which only change configuration properties that don't influence conditions are supported without limitations when running ahead-of-time optimized applications.

View File

@ -1,12 +1,14 @@
[[howto.application]]
== Spring Boot Application
= Spring Boot Application
This section includes topics relating directly to Spring Boot applications.
[[howto.application.failure-analyzer]]
=== Create Your Own FailureAnalyzer
{spring-boot-module-api}/diagnostics/FailureAnalyzer.html[`FailureAnalyzer`] is a great way to intercept an exception on startup and turn it into a human-readable message, wrapped in a {spring-boot-module-api}/diagnostics/FailureAnalysis.html[`FailureAnalysis`].
== Create Your Own FailureAnalyzer
xref:api:java/org/springframework/boot/diagnostics/FailureAnalyzer.html[`FailureAnalyzer`] is a great way to intercept an exception on startup and turn it into a human-readable message, wrapped in a xref:api:java/org/springframework/boot/diagnostics/FailureAnalysis.html[`FailureAnalysis`].
Spring Boot provides such an analyzer for application-context-related exceptions, JSR-303 validations, and more.
You can also create your own.
@ -17,10 +19,10 @@ If, for whatever reason, you cannot handle the exception, return `null` to give
`FailureAnalyzer` implementations must be registered in `META-INF/spring.factories`.
The following example registers `ProjectConstraintViolationFailureAnalyzer`:
[source,properties,indent=0,subs="verbatim"]
[source,properties]
----
org.springframework.boot.diagnostics.FailureAnalyzer=\
com.example.ProjectConstraintViolationFailureAnalyzer
org.springframework.boot.diagnostics.FailureAnalyzer=\
com.example.ProjectConstraintViolationFailureAnalyzer
----
NOTE: If you need access to the `BeanFactory` or the `Environment`, declare them as constructor arguments in your `FailureAnalyzer` implementation.
@ -28,12 +30,13 @@ NOTE: If you need access to the `BeanFactory` or the `Environment`, declare them
[[howto.application.troubleshoot-auto-configuration]]
=== Troubleshoot Auto-configuration
== Troubleshoot Auto-configuration
The Spring Boot auto-configuration tries its best to "`do the right thing`", but sometimes things fail, and it can be hard to tell why.
There is a really useful `ConditionEvaluationReport` available in any Spring Boot `ApplicationContext`.
You can see it if you enable `DEBUG` logging output.
If you use the `spring-boot-actuator` (see <<actuator#actuator,the Actuator chapter>>), there is also a `conditions` endpoint that renders the report in JSON.
If you use the `spring-boot-actuator` (see xref:actuator.adoc[the Actuator chapter]), there is also a `conditions` endpoint that renders the report in JSON.
Use that endpoint to debug the application and see what features have been added (and which have not been added) by Spring Boot at runtime.
Many more questions can be answered by looking at the source code and the Javadoc.
@ -43,7 +46,7 @@ When reading the code, remember the following rules of thumb:
Pay special attention to the `+@Conditional*+` annotations to find out what features they enable and when.
Add `--debug` to the command line or a System property `-Ddebug` to get a log on the console of all the auto-configuration decisions that were made in your app.
In a running application with actuator enabled, look at the `conditions` endpoint (`/actuator/conditions` or the JMX equivalent) for the same information.
* Look for classes that are `@ConfigurationProperties` (such as {spring-boot-autoconfigure-module-code}/web/ServerProperties.java[`ServerProperties`]) and read from there the available external configuration options.
* Look for classes that are `@ConfigurationProperties` (such as {code-spring-boot-autoconfigure-src}/web/ServerProperties.java[`ServerProperties`]) and read from there the available external configuration options.
The `@ConfigurationProperties` annotation has a `name` attribute that acts as a prefix to external properties.
Thus, `ServerProperties` has `prefix="server"` and its configuration properties are `server.port`, `server.address`, and others.
In a running application with actuator enabled, look at the `configprops` endpoint.
@ -55,7 +58,8 @@ When reading the code, remember the following rules of thumb:
[[howto.application.customize-the-environment-or-application-context]]
=== Customize the Environment or ApplicationContext Before It Starts
== Customize the Environment or ApplicationContext Before It Starts
A `SpringApplication` has `ApplicationListeners` and `ApplicationContextInitializers` that are used to apply customizations to the context or environment.
Spring Boot loads a number of such customizations for use internally from `META-INF/spring.factories`.
There is more than one way to register additional customizations:
@ -64,20 +68,20 @@ There is more than one way to register additional customizations:
* Declaratively, for all applications, by adding a `META-INF/spring.factories` and packaging a jar file that the applications all use as a library.
The `SpringApplication` sends some special `ApplicationEvents` to the listeners (some even before the context is created) and then registers the listeners for events published by the `ApplicationContext` as well.
See "`<<features#features.spring-application.application-events-and-listeners,Application Events and Listeners>>`" in the '`Spring Boot features`' section for a complete list.
See "`xref:reference:features/spring-application.adoc#features.spring-application.application-events-and-listeners[Application Events and Listeners]`" in the '`Spring Boot features`' section for a complete list.
It is also possible to customize the `Environment` before the application context is refreshed by using `EnvironmentPostProcessor`.
Each implementation should be registered in `META-INF/spring.factories`, as shown in the following example:
[indent=0]
[source]
----
org.springframework.boot.env.EnvironmentPostProcessor=com.example.YourEnvironmentPostProcessor
org.springframework.boot.env.EnvironmentPostProcessor=com.example.YourEnvironmentPostProcessor
----
The implementation can load arbitrary files and add them to the `Environment`.
For instance, the following example loads a YAML configuration file from the classpath:
include::code:MyEnvironmentPostProcessor[]
include-code::MyEnvironmentPostProcessor[]
TIP: The `Environment` has already been prepared with all the usual property sources that Spring Boot loads by default.
It is therefore possible to get the location of the file from the environment.
@ -91,14 +95,16 @@ This is too late to configure certain properties such as `+logging.*+` and `+spr
[[howto.application.context-hierarchy]]
=== Build an ApplicationContext Hierarchy (Adding a Parent or Root Context)
== Build an ApplicationContext Hierarchy (Adding a Parent or Root Context)
You can use the `ApplicationBuilder` class to create parent/child `ApplicationContext` hierarchies.
See "`<<features#features.spring-application.fluent-builder-api>>`" in the '`Spring Boot features`' section for more information.
See "`xref:reference:features/spring-application.adoc#features.spring-application.fluent-builder-api[Fluent Builder API]`" in the '`Spring Boot features`' section for more information.
[[howto.application.non-web-application]]
=== Create a Non-web Application
== Create a Non-web Application
Not all Spring applications have to be web applications (or web services).
If you want to execute some code in a `main` method but also bootstrap a Spring application to set up the infrastructure to use, you can use the `SpringApplication` features of Spring Boot.
A `SpringApplication` changes its `ApplicationContext` class, depending on whether it thinks it needs a web application or not.

View File

@ -1,61 +1,66 @@
[[howto.batch]]
== Batch Applications
= Batch Applications
A number of questions often arise when people use Spring Batch from within a Spring Boot application.
This section addresses those questions.
[[howto.batch.specifying-a-data-source]]
=== Specifying a Batch Data Source
== Specifying a Batch Data Source
By default, batch applications require a `DataSource` to store job details.
Spring Batch expects a single `DataSource` by default.
To have it use a `DataSource` other than the applications main `DataSource`, declare a `DataSource` bean, annotating its `@Bean` method with `@BatchDataSource`.
If you do so and want two data sources, remember to mark the other one `@Primary`.
To take greater control, add `@EnableBatchProcessing` to one of your `@Configuration` classes or extend `DefaultBatchConfiguration`.
See the Javadoc of {spring-batch-api}/core/configuration/annotation/EnableBatchProcessing.html[`@EnableBatchProcessing`]
and {spring-batch-api}/core/configuration/support/DefaultBatchConfiguration.html[`DefaultBatchConfiguration`] for more details.
See the Javadoc of {url-spring-batch-javadoc}/core/configuration/annotation/EnableBatchProcessing.html[`@EnableBatchProcessing`]
and {url-spring-batch-javadoc}/core/configuration/support/DefaultBatchConfiguration.html[`DefaultBatchConfiguration`] for more details.
For more info about Spring Batch, see the {spring-batch}[Spring Batch project page].
For more info about Spring Batch, see the {url-spring-batch-site}[Spring Batch project page].
[[howto.batch.specifying-a-transaction-manager]]
=== Specifying a Batch Transaction Manager
Similar to <<howto.batch.specifying-a-data-source>>, you can define a `PlatformTransactionManager` for use in the batch processing by marking it as `@BatchTransactionManager`.
== Specifying a Batch Transaction Manager
Similar to xref:batch.adoc#howto.batch.specifying-a-data-source[Specifying a Batch Data Source], you can define a `PlatformTransactionManager` for use in the batch processing by marking it as `@BatchTransactionManager`.
If you do so and want two transaction managers, remember to mark the other one as `@Primary`.
[[howto.batch.running-jobs-on-startup]]
=== Running Spring Batch Jobs on Startup
== Running Spring Batch Jobs on Startup
Spring Batch auto-configuration is enabled by adding `spring-boot-starter-batch` to your application's classpath.
If a single `Job` bean is found in the application context, it is executed on startup (see {spring-boot-autoconfigure-module-code}/batch/JobLauncherApplicationRunner.java[`JobLauncherApplicationRunner`] for details).
If a single `Job` bean is found in the application context, it is executed on startup (see {code-spring-boot-autoconfigure-src}/batch/JobLauncherApplicationRunner.java[`JobLauncherApplicationRunner`] for details).
If multiple `Job` beans are found, the job that should be executed must be specified using configprop:spring.batch.job.name[].
To disable running a `Job` found in the application context, set the configprop:spring.batch.job.enabled[] to `false`.
See {spring-boot-autoconfigure-module-code}/batch/BatchAutoConfiguration.java[BatchAutoConfiguration] for more details.
See {code-spring-boot-autoconfigure-src}/batch/BatchAutoConfiguration.java[BatchAutoConfiguration] for more details.
[[howto.batch.running-from-the-command-line]]
=== Running From the Command Line
Spring Boot converts any command line argument starting with `--` to a property to add to the `Environment`, see <<features#features.external-config.command-line-args,accessing command line properties>>.
== Running From the Command Line
Spring Boot converts any command line argument starting with `--` to a property to add to the `Environment`, see xref:reference:features/external-config.adoc#features.external-config.command-line-args[accessing command line properties].
This should not be used to pass arguments to batch jobs.
To specify batch arguments on the command line, use the regular format (that is without `--`), as shown in the following example:
[source,shell,indent=0,subs="verbatim"]
[source,shell]
----
$ java -jar myapp.jar someParameter=someValue anotherParameter=anotherValue
$ java -jar myapp.jar someParameter=someValue anotherParameter=anotherValue
----
If you specify a property of the `Environment` on the command line, it is ignored by the job.
Consider the following command:
[source,shell,indent=0,subs="verbatim"]
[source,shell]
----
$ java -jar myapp.jar --server.port=7070 someParameter=someValue
$ java -jar myapp.jar --server.port=7070 someParameter=someValue
----
This provides only one argument to the batch job: `someParameter=someValue`.
@ -63,7 +68,8 @@ This provides only one argument to the batch job: `someParameter=someValue`.
[[howto.batch.restarting-a-failed-job]]
=== Restarting a stopped or failed Job
== Restarting a stopped or failed Job
To restart a failed `Job`, all parameters (identifying and non-identifying) must be re-specified on the command line.
Non-identifying parameters are *not* copied from the previous execution.
This allows them to be modified or removed.
@ -73,7 +79,8 @@ NOTE: When you're using a custom `JobParametersIncrementer`, you have to gather
[[howto.batch.storing-job-repository]]
=== Storing the Job Repository
== Storing the Job Repository
Spring Batch requires a data store for the `Job` repository.
If you use Spring Boot, you must use an actual database.
Note that it can be an in-memory database, see {spring-batch-docs}/job.html#configuringJobRepository[Configuring a Job Repository].
Note that it can be an in-memory database, see {url-spring-batch-docs}/job.html#configuringJobRepository[Configuring a Job Repository].

View File

@ -1,79 +1,82 @@
[[howto.build]]
== Build
= Build
Spring Boot includes build plugins for Maven and Gradle.
This section answers common questions about these plugins.
[[howto.build.generate-info]]
=== Generate Build Information
== Generate Build Information
Both the Maven plugin and the Gradle plugin allow generating build information containing the coordinates, name, and version of the project.
The plugins can also be configured to add additional properties through configuration.
When such a file is present, Spring Boot auto-configures a `BuildProperties` bean.
To generate build information with Maven, add an execution for the `build-info` goal, as shown in the following example:
[source,xml,indent=0,subs="verbatim,attributes"]
[source,xml,subs="verbatim,attributes"]
----
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>{spring-boot-version}</version>
<executions>
<execution>
<goals>
<goal>build-info</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>{version-spring-boot}</version>
<executions>
<execution>
<goals>
<goal>build-info</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
----
TIP: See the {spring-boot-maven-plugin-docs}#goals-build-info[Spring Boot Maven Plugin documentation] for more details.
TIP: See the xref:maven-plugin:build-info.adoc[Spring Boot Maven Plugin documentation] for more details.
The following example does the same with Gradle:
[source,gradle,indent=0,subs="verbatim"]
[source,gradle]
----
springBoot {
buildInfo()
}
springBoot {
buildInfo()
}
----
TIP: See the {spring-boot-gradle-plugin-docs}#integrating-with-actuator-build-info[Spring Boot Gradle Plugin documentation] for more details.
TIP: See the xref:gradle-plugin:integrating-with-actuator.adoc[Spring Boot Gradle Plugin documentation] for more details.
[[howto.build.generate-git-info]]
=== Generate Git Information
== Generate Git Information
Both Maven and Gradle allow generating a `git.properties` file containing information about the state of your `git` source code repository when the project was built.
For Maven users, the `spring-boot-starter-parent` POM includes a pre-configured plugin to generate a `git.properties` file.
To use it, add the following declaration for the https://github.com/git-commit-id/git-commit-id-maven-plugin[`Git Commit Id Plugin`] to your POM:
[source,xml,indent=0,subs="verbatim"]
[source,xml]
----
<build>
<plugins>
<plugin>
<groupId>io.github.git-commit-id</groupId>
<artifactId>git-commit-id-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<build>
<plugins>
<plugin>
<groupId>io.github.git-commit-id</groupId>
<artifactId>git-commit-id-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
----
Gradle users can achieve the same result by using the https://plugins.gradle.org/plugin/com.gorylenko.gradle-git-properties[`gradle-git-properties`] plugin, as shown in the following example:
[source,gradle,indent=0,subs="verbatim"]
[source,gradle]
----
plugins {
id "com.gorylenko.gradle-git-properties" version "2.4.1"
}
plugins {
id "com.gorylenko.gradle-git-properties" version "2.4.1"
}
----
Both the Maven and Gradle plugins allow the properties that are included in `git.properties` to be configured.
@ -85,71 +88,74 @@ Using this format lets the time be parsed into a `Date` and its format, when ser
[[howto.build.customize-dependency-versions]]
=== Customize Dependency Versions
== Customize Dependency Versions
The `spring-boot-dependencies` POM manages the versions of common dependencies.
The Spring Boot plugins for Maven and Gradle allow these managed dependency versions to be customized using build properties.
WARNING: Each Spring Boot release is designed and tested against this specific set of third-party dependencies.
Overriding versions may cause compatibility issues.
To override dependency versions with Maven, see {spring-boot-maven-plugin-docs}#using[this section] of the Maven plugin's documentation.
To override dependency versions with Maven, see xref:maven-plugin:using.adoc[this section] of the Maven plugin's documentation.
To override dependency versions in Gradle, see {spring-boot-gradle-plugin-docs}#managing-dependencies-dependency-management-plugin-customizing[this section] of the Gradle plugin's documentation.
To override dependency versions in Gradle, see xref:gradle-plugin:managing-dependencies.adoc#managing-dependencies.dependency-management-plugin.customizing[this section] of the Gradle plugin's documentation.
[[howto.build.create-an-executable-jar-with-maven]]
=== Create an Executable JAR with Maven
== Create an Executable JAR with Maven
The `spring-boot-maven-plugin` can be used to create an executable "`fat`" JAR.
If you use the `spring-boot-starter-parent` POM, you can declare the plugin and your jars are repackaged as follows:
[source,xml,indent=0,subs="verbatim"]
[source,xml]
----
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
----
If you do not use the parent POM, you can still use the plugin.
However, you must additionally add an `<executions>` section, as follows:
[source,xml,indent=0,subs="verbatim,attributes"]
[source,xml,subs="verbatim,attributes"]
----
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>{spring-boot-version}</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>{version-spring-boot}</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
----
See the {spring-boot-maven-plugin-docs}#repackage[plugin documentation] for full usage details.
See the xref:maven-plugin:packaging.adoc#packaging.repackage-goal[plugin documentation] for full usage details.
[[howto.build.use-a-spring-boot-application-as-dependency]]
=== Use a Spring Boot Application as a Dependency
== Use a Spring Boot Application as a Dependency
Like a war file, a Spring Boot application is not intended to be used as a dependency.
If your application contains classes that you want to share with other projects, the recommended approach is to move that code into a separate module.
The separate module can then be depended upon by your application and other projects.
If you cannot rearrange your code as recommended above, Spring Boot's Maven and Gradle plugins must be configured to produce a separate artifact that is suitable for use as a dependency.
The executable archive cannot be used as a dependency as the <<executable-jar#appendix.executable-jar.nested-jars.jar-structure,executable jar format>> packages application classes in `BOOT-INF/classes`.
The executable archive cannot be used as a dependency as the xref:specification:executable-jar/nested-jars.adoc#appendix.executable-jar.nested-jars.jar-structure[executable jar format] packages application classes in `BOOT-INF/classes`.
This means that they cannot be found when the executable jar is used as a dependency.
To produce the two artifacts, one that can be used as a dependency and one that is executable, a classifier must be specified.
@ -157,25 +163,26 @@ This classifier is applied to the name of the executable archive, leaving the de
To configure a classifier of `exec` in Maven, you can use the following configuration:
[source,xml,indent=0,subs="verbatim"]
[source,xml]
----
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<classifier>exec</classifier>
</configuration>
</plugin>
</plugins>
</build>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<classifier>exec</classifier>
</configuration>
</plugin>
</plugins>
</build>
----
[[howto.build.extract-specific-libraries-when-an-executable-jar-runs]]
=== Extract Specific Libraries When an Executable Jar Runs
== Extract Specific Libraries When an Executable Jar Runs
Most nested libraries in an executable jar do not need to be unpacked in order to run.
However, certain libraries can have problems.
For example, JRuby includes its own nested jar support, which assumes that the `jruby-complete.jar` is always directly available as a file in its own right.
@ -187,77 +194,80 @@ WARNING: Care should be taken to ensure that your operating system is configured
For example, to indicate that JRuby should be flagged for unpacking by using the Maven Plugin, you would add the following configuration:
[source,xml,indent=0,subs="verbatim"]
[source,xml]
----
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<requiresUnpack>
<dependency>
<groupId>org.jruby</groupId>
<artifactId>jruby-complete</artifactId>
</dependency>
</requiresUnpack>
</configuration>
</plugin>
</plugins>
</build>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<requiresUnpack>
<dependency>
<groupId>org.jruby</groupId>
<artifactId>jruby-complete</artifactId>
</dependency>
</requiresUnpack>
</configuration>
</plugin>
</plugins>
</build>
----
[[howto.build.create-a-nonexecutable-jar]]
=== Create a Non-executable JAR with Exclusions
== Create a Non-executable JAR with Exclusions
Often, if you have an executable and a non-executable jar as two separate build products, the executable version has additional configuration files that are not needed in a library jar.
For example, the `application.yaml` configuration file might be excluded from the non-executable JAR.
In Maven, the executable jar must be the main artifact and you can add a classified jar for the library, as follows:
[source,xml,indent=0,subs="verbatim"]
[source,xml]
----
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<id>lib</id>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<classifier>lib</classifier>
<excludes>
<exclude>application.yaml</exclude>
</excludes>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<id>lib</id>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<classifier>lib</classifier>
<excludes>
<exclude>application.yaml</exclude>
</excludes>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
----
[[howto.build.remote-debug-maven]]
=== Remote Debug a Spring Boot Application Started with Maven
To attach a remote debugger to a Spring Boot application that was started with Maven, you can use the `jvmArguments` property of the {spring-boot-maven-plugin-docs}[maven plugin].
== Remote Debug a Spring Boot Application Started with Maven
See {spring-boot-maven-plugin-docs}#run-example-debug[this example] for more details.
To attach a remote debugger to a Spring Boot application that was started with Maven, you can use the `jvmArguments` property of the xref:maven-plugin:index.adoc[maven plugin].
See xref:maven-plugin:run.adoc#run.examples.debug[this example] for more details.
[[howto.build.build-an-executable-archive-with-ant-without-using-spring-boot-antlib]]
=== Build an Executable Archive From Ant without Using spring-boot-antlib
== Build an Executable Archive From Ant without Using spring-boot-antlib
To build with Ant, you need to grab dependencies, compile, and then create a jar or war archive.
To make it executable, you can either use the `spring-boot-antlib` module or you can follow these instructions:
@ -272,27 +282,27 @@ To make it executable, you can either use the `spring-boot-antlib` module or you
The following example shows how to build an executable archive with Ant:
[source,xml,indent=0,subs="verbatim"]
[source,xml]
----
<target name="build" depends="compile">
<jar destfile="target/${ant.project.name}-${spring-boot.version}.jar" compress="false">
<mappedresources>
<fileset dir="target/classes" />
<globmapper from="*" to="BOOT-INF/classes/*"/>
</mappedresources>
<mappedresources>
<fileset dir="src/main/resources" erroronmissingdir="false"/>
<globmapper from="*" to="BOOT-INF/classes/*"/>
</mappedresources>
<mappedresources>
<fileset dir="${lib.dir}/runtime" />
<globmapper from="*" to="BOOT-INF/lib/*"/>
</mappedresources>
<zipfileset src="${lib.dir}/loader/spring-boot-loader-jar-${spring-boot.version}.jar" />
<manifest>
<attribute name="Main-Class" value="org.springframework.boot.loader.launch.JarLauncher" />
<attribute name="Start-Class" value="${start-class}" />
</manifest>
</jar>
</target>
<target name="build" depends="compile">
<jar destfile="target/${ant.project.name}-${spring-boot.version}.jar" compress="false">
<mappedresources>
<fileset dir="target/classes" />
<globmapper from="*" to="BOOT-INF/classes/*"/>
</mappedresources>
<mappedresources>
<fileset dir="src/main/resources" erroronmissingdir="false"/>
<globmapper from="*" to="BOOT-INF/classes/*"/>
</mappedresources>
<mappedresources>
<fileset dir="${lib.dir}/runtime" />
<globmapper from="*" to="BOOT-INF/lib/*"/>
</mappedresources>
<zipfileset src="${lib.dir}/loader/spring-boot-loader-jar-${spring-boot.version}.jar" />
<manifest>
<attribute name="Main-Class" value="org.springframework.boot.loader.launch.JarLauncher" />
<attribute name="Start-Class" value="${start-class}" />
</manifest>
</jar>
</target>
----

View File

@ -1,29 +1,31 @@
[[howto.data-access]]
== Data Access
= Data Access
Spring Boot includes a number of starters for working with data sources.
This section answers questions related to doing so.
[[howto.data-access.configure-custom-datasource]]
=== Configure a Custom DataSource
== Configure a Custom DataSource
To configure your own `DataSource`, define a `@Bean` of that type in your configuration.
Spring Boot reuses your `DataSource` anywhere one is required, including database initialization.
If you need to externalize some settings, you can bind your `DataSource` to the environment (see "`<<features#features.external-config.typesafe-configuration-properties.third-party-configuration>>`").
If you need to externalize some settings, you can bind your `DataSource` to the environment (see "`xref:reference:features/external-config.adoc#features.external-config.typesafe-configuration-properties.third-party-configuration[Third-party Configuration]`").
The following example shows how to define a data source in a bean:
include::code:custom/MyDataSourceConfiguration[]
include-code::custom/MyDataSourceConfiguration[]
The following example shows how to define a data source by setting properties:
[source,yaml,indent=0,subs="verbatim",configblocks]
[configprops%novalidate,yaml]
----
app:
datasource:
url: "jdbc:h2:mem:mydb"
username: "sa"
pool-size: 30
app:
datasource:
url: "jdbc:h2:mem:mydb"
username: "sa"
pool-size: 30
----
Assuming that `SomeDataSource` has regular JavaBean properties for the URL, the username, and the pool size, these settings are bound automatically before the `DataSource` is made available to other components.
@ -34,7 +36,7 @@ It also auto-detects the driver based on the JDBC URL.
The following example shows how to create a data source by using a `DataSourceBuilder`:
include::code:builder/MyDataSourceConfiguration[]
include-code::builder/MyDataSourceConfiguration[]
To run an app with that `DataSource`, all you need is the connection information.
Pool-specific settings can also be provided.
@ -42,14 +44,14 @@ Check the implementation that is going to be used at runtime for more details.
The following example shows how to define a JDBC data source by setting properties:
[source,yaml,indent=0,subs="verbatim",configblocks]
[configprops%novalidate,yaml]
----
app:
datasource:
url: "jdbc:mysql://localhost/test"
username: "dbuser"
password: "dbpass"
pool-size: 30
app:
datasource:
url: "jdbc:mysql://localhost/test"
username: "dbuser"
password: "dbpass"
pool-size: 30
----
However, there is a catch.
@ -57,14 +59,14 @@ Because the actual type of the connection pool is not exposed, no keys are gener
Also, if you happen to have Hikari on the classpath, this basic setup does not work, because Hikari has no `url` property (but does have a `jdbcUrl` property).
In that case, you must rewrite your configuration as follows:
[source,yaml,indent=0,subs="verbatim",configblocks]
[configprops%novalidate,yaml]
----
app:
datasource:
jdbc-url: "jdbc:mysql://localhost/test"
username: "dbuser"
password: "dbpass"
pool-size: 30
app:
datasource:
jdbc-url: "jdbc:mysql://localhost/test"
username: "dbuser"
password: "dbpass"
pool-size: 30
----
You can fix that by forcing the connection pool to use and return a dedicated implementation rather than `DataSource`.
@ -72,27 +74,27 @@ You cannot change the implementation at runtime, but the list of options will be
The following example shows how create a `HikariDataSource` with `DataSourceBuilder`:
include::code:simple/MyDataSourceConfiguration[]
include-code::simple/MyDataSourceConfiguration[]
You can even go further by leveraging what `DataSourceProperties` does for you -- that is, by providing a default embedded database with a sensible username and password if no URL is provided.
You can easily initialize a `DataSourceBuilder` from the state of any `DataSourceProperties` object, so you could also inject the DataSource that Spring Boot creates automatically.
However, that would split your configuration into two namespaces: `url`, `username`, `password`, `type`, and `driver` on `spring.datasource` and the rest on your custom namespace (`app.datasource`).
To avoid that, you can redefine a custom `DataSourceProperties` on your custom namespace, as shown in the following example:
include::code:configurable/MyDataSourceConfiguration[]
include-code::configurable/MyDataSourceConfiguration[]
This setup puts you _in sync_ with what Spring Boot does for you by default, except that a dedicated connection pool is chosen (in code) and its settings are exposed in the `app.datasource.configuration` sub namespace.
Because `DataSourceProperties` is taking care of the `url`/`jdbcUrl` translation for you, you can configure it as follows:
[source,yaml,indent=0,subs="verbatim",configblocks]
[configprops%novalidate,yaml]
----
app:
datasource:
url: "jdbc:mysql://localhost/test"
username: "dbuser"
password: "dbpass"
configuration:
maximum-pool-size: 30
app:
datasource:
url: "jdbc:mysql://localhost/test"
username: "dbuser"
password: "dbpass"
configuration:
maximum-pool-size: 30
----
TIP: Spring Boot will expose Hikari-specific settings to `spring.datasource.hikari`.
@ -101,46 +103,47 @@ This example uses a more generic `configuration` sub namespace as the example do
NOTE: Because your custom configuration chooses to go with Hikari, `app.datasource.type` has no effect.
In practice, the builder is initialized with whatever value you might set there and then overridden by the call to `.type()`.
See "`<<data#data.sql.datasource>>`" in the "`Spring Boot features`" section and the {spring-boot-autoconfigure-module-code}/jdbc/DataSourceAutoConfiguration.java[`DataSourceAutoConfiguration`] class for more details.
See "`xref:reference:data/sql.adoc#data.sql.datasource[Configure a DataSource]`" in the "`Spring Boot features`" section and the {code-spring-boot-autoconfigure-src}/jdbc/DataSourceAutoConfiguration.java[`DataSourceAutoConfiguration`] class for more details.
[[howto.data-access.configure-two-datasources]]
=== Configure Two DataSources
== Configure Two DataSources
If you need to configure multiple data sources, you can apply the same tricks that are described in the previous section.
You must, however, mark one of the `DataSource` instances as `@Primary`, because various auto-configurations down the road expect to be able to get one by type.
If you create your own `DataSource`, the auto-configuration backs off.
In the following example, we provide the _exact_ same feature set as the auto-configuration provides on the primary data source:
include::code:MyDataSourcesConfiguration[]
include-code::MyDataSourcesConfiguration[]
TIP: `firstDataSourceProperties` has to be flagged as `@Primary` so that the database initializer feature uses your copy (if you use the initializer).
Both data sources are also bound for advanced customizations.
For instance, you could configure them as follows:
[source,yaml,indent=0,subs="verbatim",configblocks]
[configprops%novalidate,yaml]
----
app:
datasource:
first:
url: "jdbc:mysql://localhost/first"
username: "dbuser"
password: "dbpass"
configuration:
maximum-pool-size: 30
app:
datasource:
first:
url: "jdbc:mysql://localhost/first"
username: "dbuser"
password: "dbpass"
configuration:
maximum-pool-size: 30
second:
url: "jdbc:mysql://localhost/second"
username: "dbuser"
password: "dbpass"
max-total: 30
second:
url: "jdbc:mysql://localhost/second"
username: "dbuser"
password: "dbpass"
max-total: 30
----
You can apply the same concept to the secondary `DataSource` as well, as shown in the following example:
include::code:MyCompleteDataSourcesConfiguration[]
include-code::MyCompleteDataSourcesConfiguration[]
The preceding example configures two data sources on custom namespaces with the same logic as Spring Boot would use in auto-configuration.
Note that each `configuration` sub namespace provides advanced settings based on the chosen implementation.
@ -148,32 +151,35 @@ Note that each `configuration` sub namespace provides advanced settings based on
[[howto.data-access.spring-data-repositories]]
=== Use Spring Data Repositories
== Use Spring Data Repositories
Spring Data can create implementations of `@Repository` interfaces of various flavors.
Spring Boot handles all of that for you, as long as those `@Repository` annotations are included in one of the <<using#using.auto-configuration.packages,auto-configuration packages>>, typically the package (or a sub-package) of your main application class that is annotated with `@SpringBootApplication` or `@EnableAutoConfiguration`.
Spring Boot handles all of that for you, as long as those `@Repository` annotations are included in one of the xref:reference:using/auto-configuration.adoc#using.auto-configuration.packages[auto-configuration packages], typically the package (or a sub-package) of your main application class that is annotated with `@SpringBootApplication` or `@EnableAutoConfiguration`.
For many applications, all you need is to put the right Spring Data dependencies on your classpath.
There is a `spring-boot-starter-data-jpa` for JPA, `spring-boot-starter-data-mongodb` for Mongodb, and various other starters for supported technologies.
To get started, create some repository interfaces to handle your `@Entity` objects.
Spring Boot determines the location of your `@Repository` definitions by scanning the <<using#using.auto-configuration.packages,auto-configuration packages>>.
Spring Boot determines the location of your `@Repository` definitions by scanning the xref:reference:using/auto-configuration.adoc#using.auto-configuration.packages[auto-configuration packages].
For more control, use the `@Enable…Repositories` annotations from Spring Data.
For more about Spring Data, see the {spring-data}[Spring Data project page].
For more about Spring Data, see the {url-spring-data-site}[Spring Data project page].
[[howto.data-access.separate-entity-definitions-from-spring-configuration]]
=== Separate @Entity Definitions from Spring Configuration
Spring Boot determines the location of your `@Entity` definitions by scanning the <<using#using.auto-configuration.packages,auto-configuration packages>>.
== Separate @Entity Definitions from Spring Configuration
Spring Boot determines the location of your `@Entity` definitions by scanning the xref:reference:using/auto-configuration.adoc#using.auto-configuration.packages[auto-configuration packages].
For more control, use the `@EntityScan` annotation, as shown in the following example:
include::code:MyApplication[]
include-code::MyApplication[]
[[howto.data-access.jpa-properties]]
=== Configure JPA Properties
== Configure JPA Properties
Spring Data JPA already provides some vendor-independent configuration options (such as those for SQL logging), and Spring Boot exposes those options and a few more for Hibernate as external configuration properties.
Some of them are automatically detected according to the context so you should not have to set them.
@ -186,14 +192,14 @@ If you prefer to set the dialect yourself, set the configprop:spring.jpa.databas
The most common options to set are shown in the following example:
[source,yaml,indent=0,subs="verbatim",configprops,configblocks]
[configprops,yaml]
----
spring:
jpa:
hibernate:
naming:
physical-strategy: "com.example.MyPhysicalNamingStrategy"
show-sql: true
spring:
jpa:
hibernate:
naming:
physical-strategy: "com.example.MyPhysicalNamingStrategy"
show-sql: true
----
In addition, all properties in `+spring.jpa.properties.*+` are passed through as normal JPA properties (with the prefix stripped) when the local `EntityManagerFactory` is created.
@ -213,8 +219,9 @@ This takes precedence to anything that is applied by the auto-configuration.
[[howto.data-access.configure-hibernate-naming-strategy]]
=== Configure Hibernate Naming Strategy
Hibernate uses {hibernate-docs}#naming[two different naming strategies] to map names from the object model to the corresponding database names.
== Configure Hibernate Naming Strategy
Hibernate uses {url-hibernate-userguide}#naming[two different naming strategies] to map names from the object model to the corresponding database names.
The fully qualified class name of the physical and the implicit strategy implementations can be configured by setting the `spring.jpa.hibernate.naming.physical-strategy` and `spring.jpa.hibernate.naming.implicit-strategy` properties, respectively.
Alternatively, if `ImplicitNamingStrategy` or `PhysicalNamingStrategy` beans are available in the application context, Hibernate will be automatically configured to use them.
@ -224,41 +231,47 @@ Additionally, by default, all table names are generated in lower case.
For example, a `TelephoneNumber` entity is mapped to the `telephone_number` table.
If your schema requires mixed-case identifiers, define a custom `CamelCaseToUnderscoresNamingStrategy` bean, as shown in the following example:
include::code:spring/MyHibernateConfiguration[]
include-code::spring/MyHibernateConfiguration[]
If you prefer to use Hibernate's default instead, set the following property:
[indent=0,properties,subs="verbatim"]
[configprops,yaml]
----
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
spring:
jpa:
hibernate:
naming:
physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
----
Alternatively, you can configure the following bean:
include::code:standard/MyHibernateConfiguration[]
include-code::standard/MyHibernateConfiguration[]
See {spring-boot-autoconfigure-module-code}/orm/jpa/HibernateJpaAutoConfiguration.java[`HibernateJpaAutoConfiguration`] and {spring-boot-autoconfigure-module-code}/orm/jpa/JpaBaseConfiguration.java[`JpaBaseConfiguration`] for more details.
See {code-spring-boot-autoconfigure-src}/orm/jpa/HibernateJpaAutoConfiguration.java[`HibernateJpaAutoConfiguration`] and {code-spring-boot-autoconfigure-src}/orm/jpa/JpaBaseConfiguration.java[`JpaBaseConfiguration`] for more details.
[[howto.data-access.configure-hibernate-second-level-caching]]
=== Configure Hibernate Second-Level Caching
Hibernate {hibernate-docs}#caching[second-level cache] can be configured for a range of cache providers.
== Configure Hibernate Second-Level Caching
Hibernate {url-hibernate-userguide}#caching[second-level cache] can be configured for a range of cache providers.
Rather than configuring Hibernate to lookup the cache provider again, it is better to provide the one that is available in the context whenever possible.
To do this with JCache, first make sure that `org.hibernate.orm:hibernate-jcache` is available on the classpath.
Then, add a `HibernatePropertiesCustomizer` bean as shown in the following example:
include::code:MyHibernateSecondLevelCacheConfiguration[]
include-code::MyHibernateSecondLevelCacheConfiguration[]
This customizer will configure Hibernate to use the same `CacheManager` as the one that the application uses.
It is also possible to use separate `CacheManager` instances.
For details, see {hibernate-docs}#caching-provider-jcache[the Hibernate user guide].
For details, see {url-hibernate-userguide}#caching-provider-jcache[the Hibernate user guide].
[[howto.data-access.dependency-injection-in-hibernate-components]]
=== Use Dependency Injection in Hibernate Components
== Use Dependency Injection in Hibernate Components
By default, Spring Boot registers a `BeanContainer` implementation that uses the `BeanFactory` so that converters and entity listeners can use regular dependency injection.
You can disable or tune this behavior by registering a `HibernatePropertiesCustomizer` that removes or changes the `hibernate.resource.beans.container` property.
@ -266,20 +279,21 @@ You can disable or tune this behavior by registering a `HibernatePropertiesCusto
[[howto.data-access.use-custom-entity-manager]]
=== Use a Custom EntityManagerFactory
== Use a Custom EntityManagerFactory
To take full control of the configuration of the `EntityManagerFactory`, you need to add a `@Bean` named '`entityManagerFactory`'.
Spring Boot auto-configuration switches off its entity manager in the presence of a bean of that type.
[[howto.data-access.use-multiple-entity-managers]]
[[howto.data-access.use-multiple-entity-managers]]
=== Using Multiple EntityManagerFactories
== Using Multiple EntityManagerFactories
If you need to use JPA against multiple data sources, you likely need one `EntityManagerFactory` per data source.
The `LocalContainerEntityManagerFactoryBean` from Spring ORM allows you to configure an `EntityManagerFactory` for your needs.
You can also reuse `JpaProperties` to bind settings for each `EntityManagerFactory`, as shown in the following example:
include::code:MyEntityManagerFactoryConfiguration[]
include-code::MyEntityManagerFactoryConfiguration[]
The example above creates an `EntityManagerFactory` using a `DataSource` bean named `firstDataSource`.
It scans entities located in the same package as `Order`.
@ -295,23 +309,25 @@ Alternatively, you might be able to use a JTA transaction manager that spans bot
If you use Spring Data, you need to configure `@EnableJpaRepositories` accordingly, as shown in the following examples:
include::code:OrderConfiguration[]
include-code::OrderConfiguration[]
include::code:CustomerConfiguration[]
include-code::CustomerConfiguration[]
[[howto.data-access.use-traditional-persistence-xml]]
=== Use a Traditional persistence.xml File
== Use a Traditional persistence.xml File
Spring Boot will not search for or use a `META-INF/persistence.xml` by default.
If you prefer to use a traditional `persistence.xml`, you need to define your own `@Bean` of type `LocalEntityManagerFactoryBean` (with an ID of '`entityManagerFactory`') and set the persistence unit name there.
See {spring-boot-autoconfigure-module-code}/orm/jpa/JpaBaseConfiguration.java[`JpaBaseConfiguration`] for the default settings.
See {code-spring-boot-autoconfigure-src}/orm/jpa/JpaBaseConfiguration.java[`JpaBaseConfiguration`] for the default settings.
[[howto.data-access.use-spring-data-jpa-and-mongo-repositories]]
=== Use Spring Data JPA and Mongo Repositories
== Use Spring Data JPA and Mongo Repositories
Spring Data JPA and Spring Data Mongo can both automatically create `Repository` implementations for you.
If they are both present on the classpath, you might have to do some extra configuration to tell Spring Boot which repositories to create.
The most explicit way to do that is to use the standard Spring Data `+@EnableJpaRepositories+` and `+@EnableMongoRepositories+` annotations and provide the location of your `Repository` interfaces.
@ -325,7 +341,8 @@ To work with them, change the names of the annotations and flags accordingly.
[[howto.data-access.customize-spring-data-web-support]]
=== Customize Spring Data's Web Support
== Customize Spring Data's Web Support
Spring Data provides web support that simplifies the use of Spring Data repositories in a web application.
Spring Boot provides properties in the `spring.data.web` namespace for customizing its configuration.
Note that if you are using Spring Data REST, you must use the properties in the `spring.data.rest` namespace instead.
@ -333,12 +350,13 @@ Note that if you are using Spring Data REST, you must use the properties in the
[[howto.data-access.exposing-spring-data-repositories-as-rest]]
=== Expose Spring Data Repositories as REST Endpoint
== Expose Spring Data Repositories as REST Endpoint
Spring Data REST can expose the `Repository` implementations as REST endpoints for you,
provided Spring MVC has been enabled for the application.
Spring Boot exposes a set of useful properties (from the `spring.data.rest` namespace) that customize the {spring-data-rest-api}/core/config/RepositoryRestConfiguration.html[`RepositoryRestConfiguration`].
If you need to provide additional customization, you should use a {spring-data-rest-api}/webmvc/config/RepositoryRestConfigurer.html[`RepositoryRestConfigurer`] bean.
Spring Boot exposes a set of useful properties (from the `spring.data.rest` namespace) that customize the {url-spring-data-rest-javadoc}/org/springframework/data/rest/core/config/RepositoryRestConfiguration.html[`RepositoryRestConfiguration`].
If you need to provide additional customization, you should use a {url-spring-data-rest-javadoc}/org/springframework/data/rest/webmvc/config/RepositoryRestConfigurer.html[`RepositoryRestConfigurer`] bean.
NOTE: If you do not specify any order on your custom `RepositoryRestConfigurer`, it runs after the one Spring Boot uses internally.
If you need to specify an order, make sure it is higher than 0.
@ -346,7 +364,8 @@ If you need to specify an order, make sure it is higher than 0.
[[howto.data-access.configure-a-component-that-is-used-by-jpa]]
=== Configure a Component that is Used by JPA
== Configure a Component that is Used by JPA
If you want to configure a component that JPA uses, then you need to ensure that the component is initialized before JPA.
When the component is auto-configured, Spring Boot takes care of this for you.
For example, when Flyway is auto-configured, Hibernate is configured to depend upon Flyway so that Flyway has a chance to initialize the database before Hibernate tries to use it.
@ -354,13 +373,14 @@ For example, when Flyway is auto-configured, Hibernate is configured to depend u
If you are configuring a component yourself, you can use an `EntityManagerFactoryDependsOnPostProcessor` subclass as a convenient way of setting up the necessary dependencies.
For example, if you use Hibernate Search with Elasticsearch as its index manager, any `EntityManagerFactory` beans must be configured to depend on the `elasticsearchClient` bean, as shown in the following example:
include::code:ElasticsearchEntityManagerFactoryDependsOnPostProcessor[]
include-code::ElasticsearchEntityManagerFactoryDependsOnPostProcessor[]
[[howto.data-access.configure-jooq-with-multiple-datasources]]
=== Configure jOOQ with Two DataSources
== Configure jOOQ with Two DataSources
If you need to use jOOQ with multiple data sources, you should create your own `DSLContext` for each one.
See {spring-boot-autoconfigure-module-code}/jooq/JooqAutoConfiguration.java[JooqAutoConfiguration] for more details.
See {code-spring-boot-autoconfigure-src}/jooq/JooqAutoConfiguration.java[JooqAutoConfiguration] for more details.
TIP: In particular, `JooqExceptionTranslator` and `SpringTransactionProvider` can be reused to provide similar features to what the auto-configuration does with a single `DataSource`.

View File

@ -1,5 +1,6 @@
[[howto.data-initialization]]
== Database Initialization
= Database Initialization
An SQL database can be initialized in different ways depending on what your stack is.
Of course, you can also do it manually, provided the database is a separate process.
It is recommended to use a single mechanism for schema generation.
@ -7,7 +8,8 @@ It is recommended to use a single mechanism for schema generation.
[[howto.data-initialization.using-jpa]]
=== Initialize a Database Using JPA
== Initialize a Database Using JPA
JPA has features for DDL generation, and these can be set up to run on startup against the database.
This is controlled through two external properties:
@ -18,7 +20,8 @@ This is controlled through two external properties:
[[howto.data-initialization.using-hibernate]]
=== Initialize a Database Using Hibernate
== Initialize a Database Using Hibernate
You can set `spring.jpa.hibernate.ddl-auto` explicitly to one of the standard Hibernate property values which are `none`, `validate`, `update`, `create`, and `create-drop`.
Spring Boot chooses a default value for you based on whether it thinks your database is embedded.
It defaults to `create-drop` if no schema manager has been detected or `none` in all other cases.
@ -28,7 +31,7 @@ Be careful when switching from in-memory to a '`real`' database that you do not
You either have to set `ddl-auto` explicitly or use one of the other mechanisms to initialize the database.
NOTE: You can output the schema creation by enabling the `org.hibernate.SQL` logger.
This is done for you automatically if you enable the <<features#features.logging.console-output,debug mode>>.
This is done for you automatically if you enable the xref:reference:features/logging.adoc#features.logging.console-output[debug mode].
In addition, a file named `import.sql` in the root of the classpath is executed on startup if Hibernate creates the schema from scratch (that is, if the `ddl-auto` property is set to `create` or `create-drop`).
This can be useful for demos and for testing if you are careful but is probably not something you want to be on the classpath in production.
@ -37,7 +40,8 @@ It is a Hibernate feature (and has nothing to do with Spring).
[[howto.data-initialization.using-basic-sql-scripts]]
=== Initialize a Database Using Basic SQL Scripts
== Initialize a Database Using Basic SQL Scripts
Spring Boot can automatically create the schema (DDL scripts) of your JDBC `DataSource` or R2DBC `ConnectionFactory` and initialize its data (DML scripts).
By default, it loads schema scripts from `optional:classpath*:schema.sql` and data scripts from `optional:classpath*:data.sql`.
@ -65,26 +69,27 @@ This will defer data source initialization until after any `EntityManagerFactory
NOTE: The initialization scripts support `--` for single line comments and `/++*++ ++*++/` for block comments.
Other comment formats are not supported.
If you are using a <<howto#howto.data-initialization.migration-tool,Higher-level Database Migration Tool>>, like Flyway or Liquibase, you should use them alone to create and initialize the schema.
If you are using a xref:data-initialization.adoc#howto.data-initialization.migration-tool[Higher-level Database Migration Tool], like Flyway or Liquibase, you should use them alone to create and initialize the schema.
Using the basic `schema.sql` and `data.sql` scripts alongside Flyway or Liquibase is not recommended and support will be removed in a future release.
If you need to initialize test data using a higher-level database migration tool, please see the sections about <<howto#howto.data-initialization.migration-tool.flyway-tests, Flyway>> and <<howto#howto.data-initialization.migration-tool.liquibase-tests, Liquibase>>.
If you need to initialize test data using a higher-level database migration tool, please see the sections about xref:data-initialization.adoc#howto.data-initialization.migration-tool.flyway-tests[Flyway] and xref:data-initialization.adoc#howto.data-initialization.migration-tool.liquibase-tests[Liquibase].
[[howto.data-initialization.batch]]
=== Initialize a Spring Batch Database
== Initialize a Spring Batch Database
If you use Spring Batch, it comes pre-packaged with SQL initialization scripts for most popular database platforms.
Spring Boot can detect your database type and execute those scripts on startup.
If you use an embedded database, this happens by default.
You can also enable it for any database type, as shown in the following example:
[source,yaml,indent=0,subs="verbatim",configprops,configblocks]
[configprops,yaml]
----
spring:
batch:
jdbc:
initialize-schema: "always"
spring:
batch:
jdbc:
initialize-schema: "always"
----
You can also switch off the initialization explicitly by setting `spring.batch.jdbc.initialize-schema` to `never`.
@ -92,13 +97,15 @@ You can also switch off the initialization explicitly by setting `spring.batch.j
[[howto.data-initialization.migration-tool]]
=== Use a Higher-level Database Migration Tool
== Use a Higher-level Database Migration Tool
Spring Boot supports two higher-level migration tools: https://flywaydb.org/[Flyway] and https://www.liquibase.org/[Liquibase].
[[howto.data-initialization.migration-tool.flyway]]
==== Execute Flyway Database Migrations on Startup
=== Execute Flyway Database Migrations on Startup
To automatically run Flyway database migrations on startup, add the `org.flywaydb:flyway-core` to your classpath.
Typically, migrations are scripts in the form `V<VERSION>__<NAME>.sql` (with `<VERSION>` an underscore-separated version, such as '`1`' or '`2_1`').
@ -106,34 +113,34 @@ By default, they are in a directory called `classpath:db/migration`, but you can
This is a comma-separated list of one or more `classpath:` or `filesystem:` locations.
For example, the following configuration would search for scripts in both the default classpath location and the `/opt/migration` directory:
[source,yaml,indent=0,subs="verbatim",configprops,configblocks]
[configprops,yaml]
----
spring:
flyway:
locations: "classpath:db/migration,filesystem:/opt/migration"
spring:
flyway:
locations: "classpath:db/migration,filesystem:/opt/migration"
----
You can also add a special `\{vendor}` placeholder to use vendor-specific scripts.
Assume the following:
[source,yaml,indent=0,subs="verbatim",configprops,configblocks]
[configprops,yaml]
----
spring:
flyway:
locations: "classpath:db/migration/{vendor}"
spring:
flyway:
locations: "classpath:db/migration/{vendor}"
----
Rather than using `db/migration`, the preceding configuration sets the directory to use according to the type of the database (such as `db/migration/mysql` for MySQL).
The list of supported databases is available in {spring-boot-module-code}/jdbc/DatabaseDriver.java[`DatabaseDriver`].
The list of supported databases is available in {code-spring-boot-src}/jdbc/DatabaseDriver.java[`DatabaseDriver`].
Migrations can also be written in Java.
Flyway will be auto-configured with any beans that implement `JavaMigration`.
{spring-boot-autoconfigure-module-code}/flyway/FlywayProperties.java[`FlywayProperties`] provides most of Flyway's settings and a small set of additional properties that can be used to disable the migrations or switch off the location checking.
{code-spring-boot-autoconfigure-src}/flyway/FlywayProperties.java[`FlywayProperties`] provides most of Flyway's settings and a small set of additional properties that can be used to disable the migrations or switch off the location checking.
If you need more control over the configuration, consider registering a `FlywayConfigurationCustomizer` bean.
Spring Boot calls `Flyway.migrate()` to perform the database migration.
If you would like more control, provide a `@Bean` that implements {spring-boot-autoconfigure-module-code}/flyway/FlywayMigrationStrategy.java[`FlywayMigrationStrategy`].
If you would like more control, provide a `@Bean` that implements {code-spring-boot-autoconfigure-src}/flyway/FlywayMigrationStrategy.java[`FlywayMigrationStrategy`].
Flyway supports SQL and Java https://flywaydb.org/documentation/concepts/callbacks[callbacks].
To use SQL-based callbacks, place the callback scripts in the `classpath:db/migration` directory.
@ -154,11 +161,11 @@ For example, you can place test-specific migrations in `src/test/resources` and
Also, you can use profile-specific configuration to customize `spring.flyway.locations` so that certain migrations run only when a particular profile is active.
For example, in `application-dev.properties`, you might specify the following setting:
[source,yaml,indent=0,subs="verbatim",configprops,configblocks]
[configprops,yaml]
----
spring:
flyway:
locations: "classpath:/db/migration,classpath:/dev/db/migration"
spring:
flyway:
locations: "classpath:/db/migration,classpath:/dev/db/migration"
----
With that setup, migrations in `dev/db/migration` run only when the `dev` profile is active.
@ -166,7 +173,8 @@ With that setup, migrations in `dev/db/migration` run only when the `dev` profil
[[howto.data-initialization.migration-tool.liquibase]]
==== Execute Liquibase Database Migrations on Startup
=== Execute Liquibase Database Migrations on Startup
To automatically run Liquibase database migrations on startup, add the `org.liquibase:liquibase-core` to your classpath.
[NOTE]
@ -186,12 +194,13 @@ Alternatively, you can use Liquibase's native `DataSource` by setting `spring.li
Setting either `spring.liquibase.url` or `spring.liquibase.user` is sufficient to cause Liquibase to use its own `DataSource`.
If any of the three properties has not been set, the value of its equivalent `spring.datasource` property will be used.
See {spring-boot-autoconfigure-module-code}/liquibase/LiquibaseProperties.java[`LiquibaseProperties`] for details about available settings such as contexts, the default schema, and others.
See {code-spring-boot-autoconfigure-src}/liquibase/LiquibaseProperties.java[`LiquibaseProperties`] for details about available settings such as contexts, the default schema, and others.
[[howto.data-initialization.migration-tool.flyway-tests]]
==== Use Flyway for test-only migrations
=== Use Flyway for test-only migrations
If you want to create Flyway migrations which populate your test database, place them in `src/test/resources/db/migration`.
A file named, for example, `src/test/resources/db/migration/V9999__test-data.sql` will be executed after your production migrations and only if you're running the tests.
You can use this file to create the needed test data.
@ -200,14 +209,15 @@ This file will not be packaged in your uber jar or your container.
[[howto.data-initialization.migration-tool.liquibase-tests]]
==== Use Liquibase for test-only migrations
=== Use Liquibase for test-only migrations
If you want to create Liquibase migrations which populate your test database, you have to create a test changelog which also includes the production changelog.
First, you need to configure Liquibase to use a different changelog when running the tests.
One way to do this is to create a Spring Boot `test` profile and put the Liquibase properties in there.
For that, create a file named `src/test/resources/application-test.properties` and put the following property in there:
[source,yaml,indent=0,subs="verbatim",configprops,configblocks]
[configprops,yaml]
----
spring:
liquibase:
@ -218,7 +228,7 @@ This configures Liquibase to use a different changelog when running in the `test
Now create the changelog file at `src/test/resources/db/changelog/db.changelog-test.yaml`:
[source,yaml,indent=0,subs="verbatim"]
[source,yaml]
----
databaseChangeLog:
- include:
@ -240,7 +250,8 @@ To do this, you can add the `@ActiveProfiles("test")` annotation to your `@Sprin
[[howto.data-initialization.dependencies]]
=== Depend Upon an Initialized Database
== Depend Upon an Initialized Database
Database initialization is performed while the application is starting up as part of application context refresh.
To allow an initialized database to be accessed during startup, beans that act as database initializers and beans that require that database to have been initialized are detected automatically.
Beans whose initialization depends upon the database having been initialized are configured to depend upon those that initialize it.
@ -249,7 +260,8 @@ If, during startup, your application tries to access the database and it has not
[[howto.data-initialization.dependencies.initializer-detection]]
==== Detect a Database Initializer
=== Detect a Database Initializer
Spring Boot will automatically detect beans of the following types that initialize an SQL database:
- `DataSourceScriptDatabaseInitializer`
@ -265,7 +277,8 @@ To have other beans be detected, register an implementation of `DatabaseInitiali
[[howto.data-initialization.dependencies.depends-on-initialization-detection]]
==== Detect a Bean That Depends On Database Initialization
=== Detect a Bean That Depends On Database Initialization
Spring Boot will automatically detect beans of the following types that depends upon database initialization:
- `AbstractEntityManagerFactoryBean` (unless configprop:spring.jpa.defer-datasource-initialization[] is set to `true`)

View File

@ -1,16 +1,18 @@
[[howto.docker-compose]]
== Docker Compose
= Docker Compose
This section includes topics relating to the Docker Compose support in Spring Boot.
[[howto.docker-compose.jdbc-url]]
=== Customizing the JDBC URL
== Customizing the JDBC URL
When using `JdbcConnectionDetails` with Docker Compose, the parameters of the JDBC URL
can be customized by applying the `org.springframework.boot.jdbc.parameters` label to the
service. For example:
[source,yaml,indent=0]
[source,yaml]
----
services:
postgres:
@ -30,7 +32,7 @@ With this Docker Compose file in place, the JDBC URL used is `jdbc:postgresql://
[[howto.docker-compose.sharing-services]]
=== Sharing services between multiple applications
== Sharing services between multiple applications
If you want to share services between multiple applications, create the `compose.yaml` file in one of the applications and then use the configuration property configprop:spring.docker.compose.file[] in the other applications to reference the `compose.yaml` file.
You should also set configprop:spring.docker.compose.lifecycle-management[] to `start-only`, as it defaults to `start-and-stop` and stopping one application would shut down the shared services for the other still running applications as well.

View File

@ -1,67 +1,75 @@
[[howto.hotswapping]]
== Hot Swapping
= Hot Swapping
Spring Boot supports hot swapping.
This section answers questions about how it works.
[[howto.hotswapping.reload-static-content]]
=== Reload Static Content
== Reload Static Content
There are several options for hot reloading.
The recommended approach is to use <<using#using.devtools,`spring-boot-devtools`>>, as it provides additional development-time features, such as support for fast application restarts and LiveReload as well as sensible development-time configuration (such as template caching).
The recommended approach is to use xref:reference:using/devtools.adoc[`spring-boot-devtools`], as it provides additional development-time features, such as support for fast application restarts and LiveReload as well as sensible development-time configuration (such as template caching).
Devtools works by monitoring the classpath for changes.
This means that static resource changes must be "built" for the change to take effect.
By default, this happens automatically in Eclipse when you save your changes.
In IntelliJ IDEA, the Make Project command triggers the necessary build.
Due to the <<using#using.devtools.restart.excluding-resources, default restart exclusions>>, changes to static resources do not trigger a restart of your application.
Due to the xref:reference:using/devtools.adoc#using.devtools.restart.excluding-resources[default restart exclusions], changes to static resources do not trigger a restart of your application.
They do, however, trigger a live reload.
Alternatively, running in an IDE (especially with debugging on) is a good way to do development (all modern IDEs allow reloading of static resources and usually also allow hot-swapping of Java class changes).
Finally, the <<build-tool-plugins#build-tool-plugins, Maven and Gradle plugins>> can be configured (see the `addResources` property) to support running from the command line with reloading of static files directly from source.
Finally, the xref:build-tool-plugin:index.adoc[Maven and Gradle plugins] can be configured (see the `addResources` property) to support running from the command line with reloading of static files directly from source.
You can use that with an external css/js compiler process if you are writing that code with higher-level tools.
[[howto.hotswapping.reload-templates]]
=== Reload Templates without Restarting the Container
== Reload Templates without Restarting the Container
Most of the templating technologies supported by Spring Boot include a configuration option to disable caching (described later in this document).
If you use the `spring-boot-devtools` module, these properties are <<using#using.devtools.property-defaults,automatically configured>> for you at development time.
If you use the `spring-boot-devtools` module, these properties are xref:reference:using/devtools.adoc#using.devtools.property-defaults[automatically configured] for you at development time.
[[howto.hotswapping.reload-templates.thymeleaf]]
==== Thymeleaf Templates
=== Thymeleaf Templates
If you use Thymeleaf, set `spring.thymeleaf.cache` to `false`.
See {spring-boot-autoconfigure-module-code}/thymeleaf/ThymeleafAutoConfiguration.java[`ThymeleafAutoConfiguration`] for other Thymeleaf customization options.
See {code-spring-boot-autoconfigure-src}/thymeleaf/ThymeleafAutoConfiguration.java[`ThymeleafAutoConfiguration`] for other Thymeleaf customization options.
[[howto.hotswapping.reload-templates.freemarker]]
==== FreeMarker Templates
=== FreeMarker Templates
If you use FreeMarker, set `spring.freemarker.cache` to `false`.
See {spring-boot-autoconfigure-module-code}/freemarker/FreeMarkerAutoConfiguration.java[`FreeMarkerAutoConfiguration`] for other FreeMarker customization options.
See {code-spring-boot-autoconfigure-src}/freemarker/FreeMarkerAutoConfiguration.java[`FreeMarkerAutoConfiguration`] for other FreeMarker customization options.
[[howto.hotswapping.reload-templates.groovy]]
==== Groovy Templates
=== Groovy Templates
If you use Groovy templates, set `spring.groovy.template.cache` to `false`.
See {spring-boot-autoconfigure-module-code}/groovy/template/GroovyTemplateAutoConfiguration.java[`GroovyTemplateAutoConfiguration`] for other Groovy customization options.
See {code-spring-boot-autoconfigure-src}/groovy/template/GroovyTemplateAutoConfiguration.java[`GroovyTemplateAutoConfiguration`] for other Groovy customization options.
[[howto.hotswapping.fast-application-restarts]]
=== Fast Application Restarts
== Fast Application Restarts
The `spring-boot-devtools` module includes support for automatic application restarts.
While not as fast as technologies such as https://www.jrebel.com/products/jrebel[JRebel] it is usually significantly faster than a "`cold start`".
You should probably give it a try before investigating some of the more complex reload options discussed later in this document.
For more details, see the <<using#using.devtools>> section.
For more details, see the xref:reference:using/devtools.adoc[Developer Tools] section.
[[howto.hotswapping.reload-java-classes-without-restarting]]
=== Reload Java Classes without Restarting the Container
== Reload Java Classes without Restarting the Container
Many modern IDEs (Eclipse, IDEA, and others) support hot swapping of bytecode.
Consequently, if you make a change that does not affect class or method signatures, it should reload cleanly with no side effects.

View File

@ -1,13 +1,15 @@
[[howto.http-clients]]
== HTTP Clients
= HTTP Clients
Spring Boot offers a number of starters that work with HTTP clients.
This section answers questions related to using them.
[[howto.http-clients.rest-template-proxy-configuration]]
=== Configure RestTemplate to Use a Proxy
As described in <<io#io.rest-client.resttemplate.customization>>, you can use a `RestTemplateCustomizer` with `RestTemplateBuilder` to build a customized `RestTemplate`.
== Configure RestTemplate to Use a Proxy
As described in xref:reference:io/rest-client.adoc#io.rest-client.resttemplate.customization[RestTemplate Customization], you can use a `RestTemplateCustomizer` with `RestTemplateBuilder` to build a customized `RestTemplate`.
This is the recommended approach for creating a `RestTemplate` configured to use a proxy.
The exact details of the proxy configuration depend on the underlying client request factory that is being used.
@ -15,12 +17,13 @@ The exact details of the proxy configuration depend on the underlying client req
[[howto.http-clients.webclient-reactor-netty-customization]]
=== Configure the TcpClient used by a Reactor Netty-based WebClient
== Configure the TcpClient used by a Reactor Netty-based WebClient
When Reactor Netty is on the classpath a Reactor Netty-based `WebClient` is auto-configured.
To customize the client's handling of network connections, provide a `ClientHttpConnector` bean.
The following example configures a 60 second connect timeout and adds a `ReadTimeoutHandler`:
include::code:MyReactorNettyClientConfiguration[]
include-code::MyReactorNettyClientConfiguration[]
TIP: Note the use of `ReactorResourceFactory` for the connection provider and event loop resources.
This ensures efficient sharing of resources for the server receiving requests and the client making requests.

View File

@ -0,0 +1,12 @@
[[howto]]
= How-to Guides
This section provides answers to some common '`how do I do that...`' questions that often arise when using Spring Boot.
Its coverage is not exhaustive, but it does cover quite a lot.
If you have a specific problem that we do not cover here, you might want to check https://stackoverflow.com/tags/spring-boot[stackoverflow.com] to see if someone has already provided an answer.
This is also a great place to ask new questions (please use the `spring-boot` tag).
We are also more than happy to extend this section.
If you want to add a '`how-to`', send us a {url-github}[pull request].

View File

@ -1,30 +1,26 @@
[[howto.jersey]]
== Jersey
= Jersey
[[howto.jersey.spring-security]]
=== Secure Jersey endpoints with Spring Security
== Secure Jersey endpoints with Spring Security
Spring Security can be used to secure a Jersey-based web application in much the same way as it can be used to secure a Spring MVC-based web application.
However, if you want to use Spring Security's method-level security with Jersey, you must configure Jersey to use `setStatus(int)` rather `sendError(int)`.
This prevents Jersey from committing the response before Spring Security has had an opportunity to report an authentication or authorization failure to the client.
The `jersey.config.server.response.setStatusOverSendError` property must be set to `true` on the application's `ResourceConfig` bean, as shown in the following example:
[source,java,indent=0,subs="verbatim"]
----
include::{docs-java}/howto/jersey/springsecurity/JerseySetStatusOverSendErrorConfig.java[]
----
include-code::JerseySetStatusOverSendErrorConfig[]
[[howto.jersey.alongside-another-web-framework]]
=== Use Jersey Alongside Another Web Framework
== Use Jersey Alongside Another Web Framework
To use Jersey alongside another web framework, such as Spring MVC, it should be configured so that it will allow the other framework to handle requests that it cannot handle.
First, configure Jersey to use a filter rather than a servlet by configuring the configprop:spring.jersey.type[] application property with a value of `filter`.
Second, configure your `ResourceConfig` to forward requests that would have resulted in a 404, as shown in the following example.
[source,java,indent=0,subs="verbatim"]
----
include::{docs-java}/howto/jersey/alongsideanotherwebframework/JerseyConfig.java[]
----
includ-code::alongsideanotherwebframework/JerseyConfig[]

View File

@ -1,17 +1,18 @@
[[howto.logging]]
== Logging
= Logging
Spring Boot has no mandatory logging dependency, except for the Commons Logging API, which is typically provided by Spring Framework's `spring-jcl` module.
To use https://logback.qos.ch[Logback], you need to include it and `spring-jcl` on the classpath.
The recommended way to do that is through the starters, which all depend on `spring-boot-starter-logging`.
For a web application, you only need `spring-boot-starter-web`, since it depends transitively on the logging starter.
If you use Maven, the following dependency adds logging for you:
[source,xml,indent=0,subs="verbatim"]
[source,xml]
----
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
----
Spring Boot has a `LoggingSystem` abstraction that attempts to configure logging based on the content of the classpath.
@ -19,12 +20,12 @@ If Logback is available, it is the first choice.
If the only change you need to make to logging is to set the levels of various loggers, you can do so in `application.properties` by using the "logging.level" prefix, as shown in the following example:
[source,yaml,indent=0,subs="verbatim",configprops,configblocks]
[configprops,yaml]
----
logging:
level:
org.springframework.web: "debug"
org.hibernate: "error"
logging:
level:
org.springframework.web: "debug"
org.hibernate: "error"
----
You can also set the location of a file to which the log will be written (in addition to the console) by using `logging.file.name`.
@ -35,10 +36,11 @@ By default, Spring Boot picks up the native configuration from its default locat
[[howto.logging.logback]]
=== Configure Logback for Logging
== Configure Logback for Logging
If you need to apply customizations to logback beyond those that can be achieved with `application.properties`, you will need to add a standard logback configuration file.
You can add a `logback.xml` file to the root of your classpath for logback to find.
You can also use `logback-spring.xml` if you want to use the <<features#features.logging.logback-extensions,Spring Boot Logback extensions>>.
You can also use `logback-spring.xml` if you want to use the xref:reference:features/logging.adoc#features.logging.logback-extensions[Spring Boot Logback extensions].
TIP: The Logback documentation has a https://logback.qos.ch/manual/configuration.html[dedicated section that covers configuration] in some detail.
@ -55,17 +57,17 @@ In addition, a legacy `base.xml` file is provided for compatibility with earlier
A typical custom `logback.xml` file would look something like this:
[source,xml,indent=0,subs="verbatim"]
[source,xml]
----
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include resource="org/springframework/boot/logging/logback/defaults.xml"/>
<include resource="org/springframework/boot/logging/logback/console-appender.xml" />
<root level="INFO">
<appender-ref ref="CONSOLE" />
</root>
<logger name="org.springframework.web" level="DEBUG"/>
</configuration>
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include resource="org/springframework/boot/logging/logback/defaults.xml"/>
<include resource="org/springframework/boot/logging/logback/console-appender.xml" />
<root level="INFO">
<appender-ref ref="CONSOLE" />
</root>
<logger name="org.springframework.web" level="DEBUG"/>
</configuration>
----
Your logback configuration file can also make use of System properties that the `LoggingSystem` takes care of creating for you:
@ -88,35 +90,37 @@ Any `logback-spring.groovy` files will not be detected.
[[howto.logging.logback.file-only-output]]
==== Configure Logback for File-only Output
=== Configure Logback for File-only Output
If you want to disable console logging and write output only to a file, you need a custom `logback-spring.xml` that imports `file-appender.xml` but not `console-appender.xml`, as shown in the following example:
[source,xml,indent=0,subs="verbatim"]
[source,xml]
----
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include resource="org/springframework/boot/logging/logback/defaults.xml" />
<property name="LOG_FILE" value="${LOG_FILE:-${LOG_PATH:-${LOG_TEMP:-${java.io.tmpdir:-/tmp}}/}spring.log}"/>
<include resource="org/springframework/boot/logging/logback/file-appender.xml" />
<root level="INFO">
<appender-ref ref="FILE" />
</root>
</configuration>
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include resource="org/springframework/boot/logging/logback/defaults.xml" />
<property name="LOG_FILE" value="${LOG_FILE:-${LOG_PATH:-${LOG_TEMP:-${java.io.tmpdir:-/tmp}}/}spring.log}"/>
<include resource="org/springframework/boot/logging/logback/file-appender.xml" />
<root level="INFO">
<appender-ref ref="FILE" />
</root>
</configuration>
----
You also need to add `logging.file.name` to your `application.properties` or `application.yaml`, as shown in the following example:
[source,yaml,indent=0,subs="verbatim",configprops,configblocks]
[configprops,yaml]
----
logging:
file:
name: "myapplication.log"
logging:
file:
name: "myapplication.log"
----
[[howto.logging.log4j]]
=== Configure Log4j for Logging
== Configure Log4j for Logging
Spring Boot supports https://logging.apache.org/log4j/2.x/[Log4j 2] for logging configuration if it is on the classpath.
If you use the starters for assembling dependencies, you have to exclude Logback and then include Log4j 2 instead.
If you do not use the starters, you need to provide (at least) `spring-jcl` in addition to Log4j 2.
@ -124,42 +128,42 @@ If you do not use the starters, you need to provide (at least) `spring-jcl` in a
The recommended path is through the starters, even though it requires some jiggling.
The following example shows how to set up the starters in Maven:
[source,xml,indent=0,subs="verbatim"]
[source,xml]
----
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
----
Gradle provides a few different ways to set up the starters.
One way is to use a {gradle-docs}/resolution_rules.html#sec:module_replacement[module replacement].
One way is to use a {url-gradle-docs}/resolution_rules.html#sec:module_replacement[module replacement].
To do so, declare a dependency on the Log4j 2 starter and tell Gradle that any occurrences of the default logging starter should be replaced by the Log4j 2 starter, as shown in the following example:
[source,gradle,indent=0,subs="verbatim"]
[source,gradle]
----
dependencies {
implementation "org.springframework.boot:spring-boot-starter-log4j2"
modules {
module("org.springframework.boot:spring-boot-starter-logging") {
replacedBy("org.springframework.boot:spring-boot-starter-log4j2", "Use Log4j2 instead of Logback")
}
dependencies {
implementation "org.springframework.boot:spring-boot-starter-log4j2"
modules {
module("org.springframework.boot:spring-boot-starter-logging") {
replacedBy("org.springframework.boot:spring-boot-starter-log4j2", "Use Log4j2 instead of Logback")
}
}
}
----
NOTE: The Log4j starters gather together the dependencies for common logging requirements (such as having Tomcat use `java.util.logging` but configuring the output using Log4j 2).
@ -169,7 +173,8 @@ NOTE: To ensure that debug logging performed using `java.util.logging` is routed
[[howto.logging.log4j.yaml-or-json-config]]
==== Use YAML or JSON to Configure Log4j 2
=== Use YAML or JSON to Configure Log4j 2
In addition to its default XML configuration format, Log4j 2 also supports YAML and JSON configuration files.
To configure Log4j 2 to use an alternative configuration file format, add the appropriate dependencies to the classpath and name your configuration files to match your chosen file format, as shown in the following example:
@ -189,7 +194,8 @@ To configure Log4j 2 to use an alternative configuration file format, add the ap
[[howto.logging.log4j.composite-configuration]]
==== Use Composite Configuration to Configure Log4j 2
=== Use Composite Configuration to Configure Log4j 2
Log4j 2 has support for combining multiple configuration files into a single composite configuration.
To use this support in Spring Boot, configure configprop:logging.log4j2.config.override[] with the locations of one or more secondary configuration files.
The secondary configuration files will be merged with the primary configuration, whether the primary's source is Spring Boot's defaults, a standard location such as `log4j.xml`, or the location configured by the configprop:logging.config[] property.

View File

@ -1,16 +1,18 @@
[[howto.messaging]]
== Messaging
= Messaging
Spring Boot offers a number of starters to support messaging.
This section answers questions that arise from using messaging with Spring Boot.
[[howto.messaging.disable-transacted-jms-session]]
=== Disable Transacted JMS Session
== Disable Transacted JMS Session
If your JMS broker does not support transacted sessions, you have to disable the support of transactions altogether.
If you create your own `JmsListenerContainerFactory`, there is nothing to do, since, by default it cannot be transacted.
If you want to use the `DefaultJmsListenerContainerFactoryConfigurer` to reuse Spring Boot's default, you can disable transacted sessions, as follows:
include::code:MyJmsConfiguration[]
include-code::MyJmsConfiguration[]
The preceding example overrides the default factory, and it should be applied to any other factory that your application defines, if any.

View File

@ -1,45 +1,47 @@
[[howto.nosql]]
== NoSQL
= NoSQL
Spring Boot offers a number of starters that support NoSQL technologies.
This section answers questions that arise from using NoSQL with Spring Boot.
[[howto.nosql.jedis-instead-of-lettuce]]
=== Use Jedis Instead of Lettuce
== Use Jedis Instead of Lettuce
By default, the Spring Boot starter (`spring-boot-starter-data-redis`) uses https://github.com/lettuce-io/lettuce-core/[Lettuce].
You need to exclude that dependency and include the https://github.com/xetorthio/jedis/[Jedis] one instead.
Spring Boot manages both of these dependencies, allowing you to switch to Jedis without specifying a version.
The following example shows how to accomplish this in Maven:
[source,xml,indent=0,subs="verbatim"]
[source,xml]
----
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<exclusions>
<exclusion>
<groupId>io.lettuce</groupId>
<artifactId>lettuce-core</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<exclusions>
<exclusion>
<groupId>io.lettuce</groupId>
<artifactId>lettuce-core</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>
----
The following example shows how to accomplish this in Gradle:
[source,gradle,indent=0,subs="verbatim"]
[source,gradle]
----
dependencies {
implementation('org.springframework.boot:spring-boot-starter-data-redis') {
exclude group: 'io.lettuce', module: 'lettuce-core'
}
implementation 'redis.clients:jedis'
// ...
dependencies {
implementation('org.springframework.boot:spring-boot-starter-data-redis') {
exclude group: 'io.lettuce', module: 'lettuce-core'
}
implementation 'redis.clients:jedis'
// ...
}
----

View File

@ -1,27 +1,30 @@
[[howto.properties-and-configuration]]
== Properties and Configuration
= Properties and Configuration
This section includes topics about setting and reading properties and configuration settings and their interaction with Spring Boot applications.
[[howto.properties-and-configuration.expand-properties]]
=== Automatically Expand Properties at Build Time
== Automatically Expand Properties at Build Time
Rather than hardcoding some properties that are also specified in your project's build configuration, you can automatically expand them by instead using the existing build configuration.
This is possible in both Maven and Gradle.
[[howto.properties-and-configuration.expand-properties.maven]]
==== Automatic Property Expansion Using Maven
=== Automatic Property Expansion Using Maven
You can automatically expand properties from the Maven project by using resource filtering.
If you use the `spring-boot-starter-parent`, you can then refer to your Maven '`project properties`' with `@..@` placeholders, as shown in the following example:
[source,yaml,indent=0,subs="verbatim",configblocks]
[configprops%novalidate,yaml]
----
app:
encoding: "@project.build.sourceEncoding@"
java:
version: "@java.version@"
app:
encoding: "@project.build.sourceEncoding@"
java:
version: "@java.version@"
----
NOTE: Only production configuration is filtered that way (in other words, no filtering is applied on `src/test/resources`).
@ -29,35 +32,35 @@ NOTE: Only production configuration is filtered that way (in other words, no fil
TIP: If you enable the `addResources` flag, the `spring-boot:run` goal can add `src/main/resources` directly to the classpath (for hot reloading purposes).
Doing so circumvents the resource filtering and this feature.
Instead, you can use the `exec:java` goal or customize the plugin's configuration.
See the {spring-boot-maven-plugin-docs}#getting-started[plugin usage page] for more details.
See the xref:maven-plugin:using.adoc[plugin usage page] for more details.
If you do not use the starter parent, you need to include the following element inside the `<build/>` element of your `pom.xml`:
[source,xml,indent=0,subs="verbatim"]
[source,xml]
----
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
----
You also need to include the following element inside `<plugins/>`:
[source,xml,indent=0,subs="verbatim"]
[source,xml]
----
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.7</version>
<configuration>
<delimiters>
<delimiter>@</delimiter>
</delimiters>
<useDefaultDelimiters>false</useDefaultDelimiters>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.7</version>
<configuration>
<delimiters>
<delimiter>@</delimiter>
</delimiters>
<useDefaultDelimiters>false</useDefaultDelimiters>
</configuration>
</plugin>
----
NOTE: The `useDefaultDelimiters` property is important if you use standard Spring placeholders (such as `$\{placeholder}`) in your configuration.
@ -66,23 +69,24 @@ If that property is not set to `false`, these may be expanded by the build.
[[howto.properties-and-configuration.expand-properties.gradle]]
==== Automatic Property Expansion Using Gradle
=== Automatic Property Expansion Using Gradle
You can automatically expand properties from the Gradle project by configuring the Java plugin's `processResources` task to do so, as shown in the following example:
[source,gradle,indent=0,subs="verbatim"]
[source,gradle]
----
tasks.named('processResources') {
expand(project.properties)
}
tasks.named('processResources') {
expand(project.properties)
}
----
You can then refer to your Gradle project's properties by using placeholders, as shown in the following example:
[source,yaml,indent=0,subs="verbatim",configblocks]
[configprops%novalidate,yaml]
----
app:
name: "${name}"
description: "${description}"
app:
name: "${name}"
description: "${description}"
----
NOTE: Gradle's `expand` method uses Groovy's `SimpleTemplateEngine`, which transforms `${..}` tokens.
@ -92,17 +96,18 @@ To use Spring property placeholders together with automatic expansion, escape th
[[howto.properties-and-configuration.externalize-configuration]]
=== Externalize the Configuration of SpringApplication
== Externalize the Configuration of SpringApplication
A `SpringApplication` has bean property setters, so you can use its Java API as you create the application to modify its behavior.
Alternatively, you can externalize the configuration by setting properties in `+spring.main.*+`.
For example, in `application.properties`, you might have the following settings:
[source,yaml,indent=0,subs="verbatim",configprops,configblocks]
[configprops,yaml]
----
spring:
main:
web-application-type: "none"
banner-mode: "off"
spring:
main:
web-application-type: "none"
banner-mode: "off"
----
Then the Spring Boot banner is not printed on startup, and the application is not starting an embedded web server.
@ -110,20 +115,20 @@ Then the Spring Boot banner is not printed on startup, and the application is no
Properties defined in external configuration override and replace the values specified with the Java API, with the notable exception of the primary sources.
Primary sources are those provided to the `SpringApplication` constructor:
include::code:application/MyApplication[]
include-code::application/MyApplication[]
Or to `sources(...)` method of a `SpringApplicationBuilder`:
include::code:builder/MyApplication[]
include-code::builder/MyApplication[]
Given the examples above, if we have the following configuration:
[source,yaml,indent=0,subs="verbatim",configprops,configblocks]
[configprops,yaml]
----
spring:
main:
sources: "com.example.MyDatabaseConfig,com.example.MyJmsConfig"
banner-mode: "console"
spring:
main:
sources: "com.example.MyDatabaseConfig,com.example.MyJmsConfig"
banner-mode: "console"
----
The actual application will show the banner (as overridden by configuration) and uses three sources for the `ApplicationContext`.
@ -136,8 +141,9 @@ The application sources are:
[[howto.properties-and-configuration.external-properties-location]]
=== Change the Location of External Properties of an Application
By default, properties from different sources are added to the Spring `Environment` in a defined order (see "`<<features#features.external-config>>`" in the '`Spring Boot features`' section for the exact order).
== Change the Location of External Properties of an Application
By default, properties from different sources are added to the Spring `Environment` in a defined order (see "`xref:reference:features/external-config.adoc[Externalized Configuration]`" in the '`Spring Boot features`' section for the exact order).
You can also provide the following System properties (or environment variables) to change the behavior:
@ -148,19 +154,20 @@ You can also provide the following System properties (or environment variables)
No matter what you set in the environment, Spring Boot always loads `application.properties` as described above.
By default, if YAML is used, then files with the '`.yaml`' and '`.yml`' extension are also added to the list.
TIP: If you want detailed information about the files that are being loaded you can <<features#features.logging.log-levels, set the logging level>> of `org.springframework.boot.context.config` to `trace`.
TIP: If you want detailed information about the files that are being loaded you can xref:reference:features/logging.adoc#features.logging.log-levels[set the logging level] of `org.springframework.boot.context.config` to `trace`.
[[howto.properties-and-configuration.short-command-line-arguments]]
=== Use '`Short`' Command Line Arguments
== Use '`Short`' Command Line Arguments
Some people like to use (for example) `--port=9000` instead of `--server.port=9000` to set configuration properties on the command line.
You can enable this behavior by using placeholders in `application.properties`, as shown in the following example:
[source,yaml,indent=0,subs="verbatim",configprops,configblocks]
[configprops,yaml]
----
server:
port: "${port:8080}"
server:
port: "${port:8080}"
----
TIP: If you inherit from the `spring-boot-starter-parent` POM, the default filter token of the `maven-resources-plugins` has been changed from `+${*}+` to `@` (that is, `@maven.token@` instead of `${maven.token}`) to prevent conflicts with Spring-style placeholders.
@ -172,19 +179,20 @@ In those two platforms, the `PORT` environment variable is set automatically and
[[howto.properties-and-configuration.yaml]]
=== Use YAML for External Properties
== Use YAML for External Properties
YAML is a superset of JSON and, as such, is a convenient syntax for storing external properties in a hierarchical format, as shown in the following example:
[source,yaml,indent=0,subs="verbatim"]
[source,yaml]
----
spring:
application:
name: "cruncher"
datasource:
driver-class-name: "com.mysql.jdbc.Driver"
url: "jdbc:mysql://localhost/test"
server:
port: 9000
spring:
application:
name: "cruncher"
datasource:
driver-class-name: "com.mysql.jdbc.Driver"
url: "jdbc:mysql://localhost/test"
server:
port: 9000
----
Create a file called `application.yaml` and put it in the root of your classpath.
@ -193,87 +201,90 @@ A YAML file is parsed to a Java `Map<String,Object>` (like a JSON object), and S
The preceding example YAML corresponds to the following `application.properties` file:
[source,properties,indent=0,subs="verbatim",configprops]
[source,properties,subs="verbatim",configprops]
----
spring.application.name=cruncher
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost/test
server.port=9000
spring.application.name=cruncher
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost/test
server.port=9000
----
See "`<<features#features.external-config.yaml>>`" in the '`Spring Boot features`' section for more information about YAML.
See "`xref:reference:features/external-config.adoc#features.external-config.yaml[Working With YAML]`" in the '`Spring Boot features`' section for more information about YAML.
[[howto.properties-and-configuration.set-active-spring-profiles]]
=== Set the Active Spring Profiles
== Set the Active Spring Profiles
The Spring `Environment` has an API for this, but you would normally set a System property (configprop:spring.profiles.active[]) or an OS environment variable (configprop:spring.profiles.active[format=envvar]).
Also, you can launch your application with a `-D` argument (remember to put it before the main class or jar archive), as follows:
[source,shell,indent=0,subs="verbatim"]
[source,shell]
----
$ java -jar -Dspring.profiles.active=production demo-0.0.1-SNAPSHOT.jar
$ java -jar -Dspring.profiles.active=production demo-0.0.1-SNAPSHOT.jar
----
In Spring Boot, you can also set the active profile in `application.properties`, as shown in the following example:
[source,yaml,indent=0,subs="verbatim",configprops,configblocks]
[configprops,yaml]
----
spring:
profiles:
active: "production"
spring:
profiles:
active: "production"
----
A value set this way is replaced by the System property or environment variable setting but not by the `SpringApplicationBuilder.profiles()` method.
Thus, the latter Java API can be used to augment the profiles without changing the defaults.
See "`<<features#features.profiles>>`" in the "`Spring Boot features`" section for more information.
See "`xref:reference:features/profiles.adoc[Profiles]`" in the "`Spring Boot features`" section for more information.
[[howto.properties-and-configuration.set-default-spring-profile-name]]
=== Set the Default Profile Name
== Set the Default Profile Name
The default profile is a profile that is enabled if no profile is active.
By default, the name of the default profile is `default`, but it could be changed using a System property (configprop:spring.profiles.default[]) or an OS environment variable (configprop:spring.profiles.default[format=envvar]).
In Spring Boot, you can also set the default profile name in `application.properties`, as shown in the following example:
[source,yaml,indent=0,subs="verbatim",configprops,configblocks]
[configprops,yaml]
----
spring:
profiles:
default: "dev"
spring:
profiles:
default: "dev"
----
See "`<<features#features.profiles>>`" in the "`Spring Boot features`" section for more information.
See "`xref:reference:features/profiles.adoc[Profiles]`" in the "`Spring Boot features`" section for more information.
[[howto.properties-and-configuration.change-configuration-depending-on-the-environment]]
=== Change Configuration Depending on the Environment
Spring Boot supports multi-document YAML and Properties files (see <<features#features.external-config.files.multi-document>> for details) which can be activated conditionally based on the active profiles.
== Change Configuration Depending on the Environment
Spring Boot supports multi-document YAML and Properties files (see xref:reference:features/external-config.adoc#features.external-config.files.multi-document[Working With Multi-Document Files] for details) which can be activated conditionally based on the active profiles.
If a document contains a `spring.config.activate.on-profile` key, then the profiles value (a comma-separated list of profiles or a profile expression) is fed into the Spring `Environment.acceptsProfiles()` method.
If the profile expression matches then that document is included in the final merge (otherwise, it is not), as shown in the following example:
[source,yaml,indent=0,subs="verbatim,attributes",configprops,configblocks]
[configprops,yaml]
----
server:
port: 9000
---
spring:
config:
activate:
on-profile: "development"
server:
port: 9001
---
spring:
config:
activate:
on-profile: "production"
server:
port: 0
server:
port: 9000
---
spring:
config:
activate:
on-profile: "development"
server:
port: 9001
---
spring:
config:
activate:
on-profile: "production"
server:
port: 0
----
In the preceding example, the default port is 9000.
@ -286,12 +297,13 @@ Later values override earlier values.
[[howto.properties-and-configuration.discover-build-in-options-for-external-properties]]
=== Discover Built-in Options for External Properties
== Discover Built-in Options for External Properties
Spring Boot binds external properties from `application.properties` (or YAML files and other places) into an application at runtime.
There is not (and technically cannot be) an exhaustive list of all supported properties in a single location, because contributions can come from additional jar files on your classpath.
A running application with the Actuator features has a `configprops` endpoint that shows all the bound and bindable properties available through `@ConfigurationProperties`.
The appendix includes an <<application-properties#appendix.application-properties, `application.properties`>> example with a list of the most common properties supported by Spring Boot.
The appendix includes an xref:appendix:application-properties/index.adoc[`application.properties`] example with a list of the most common properties supported by Spring Boot.
The definitive list comes from searching the source code for `@ConfigurationProperties` and `@Value` annotations as well as the occasional use of `Binder`.
For more about the exact ordering of loading properties, see "<<features#features.external-config>>".
For more about the exact ordering of loading properties, see "xref:reference:features/external-config.adoc[Externalized Configuration]".

View File

@ -1,40 +1,44 @@
[[howto.security]]
== Security
= Security
This section addresses questions about security when working with Spring Boot, including questions that arise from using Spring Security with Spring Boot.
For more about Spring Security, see the {spring-security}[Spring Security project page].
For more about Spring Security, see the {url-spring-security-site}[Spring Security project page].
[[howto.security.switch-off-spring-boot-configuration]]
=== Switch off the Spring Boot Security Configuration
== Switch off the Spring Boot Security Configuration
If you define a `@Configuration` with a `SecurityFilterChain` bean in your application, this action switches off the default webapp security settings in Spring Boot.
[[howto.security.change-user-details-service-and-add-user-accounts]]
=== Change the UserDetailsService and Add User Accounts
== Change the UserDetailsService and Add User Accounts
If you provide a `@Bean` of type `AuthenticationManager`, `AuthenticationProvider`, or `UserDetailsService`, the default `@Bean` for `InMemoryUserDetailsManager` is not created.
This means you have the full feature set of Spring Security available (such as {spring-security-docs}/servlet/authentication/index.html[various authentication options]).
This means you have the full feature set of Spring Security available (such as {url-spring-security-docs}/servlet/authentication/index.html[various authentication options]).
The easiest way to add user accounts is by providing your own `UserDetailsService` bean.
[[howto.security.enable-https]]
=== Enable HTTPS When Running behind a Proxy Server
== Enable HTTPS When Running behind a Proxy Server
Ensuring that all your main endpoints are only available over HTTPS is an important chore for any application.
If you use Tomcat as a servlet container, then Spring Boot adds Tomcat's own `RemoteIpValve` automatically if it detects some environment settings, allowing you to rely on the `HttpServletRequest` to report whether it is secure or not (even downstream of a proxy server that handles the real SSL termination).
The standard behavior is determined by the presence or absence of certain request headers (`x-forwarded-for` and `x-forwarded-proto`), whose names are conventional, so it should work with most front-end proxies.
You can switch on the valve by adding some entries to `application.properties`, as shown in the following example:
[source,yaml,indent=0,subs="verbatim",configprops,configblocks]
[configprops,yaml]
----
server:
tomcat:
remoteip:
remote-ip-header: "x-forwarded-for"
protocol-header: "x-forwarded-proto"
server:
tomcat:
remoteip:
remote-ip-header: "x-forwarded-for"
protocol-header: "x-forwarded-proto"
----
(The presence of either of those properties switches on the valve.
@ -42,4 +46,4 @@ Alternatively, you can add the `RemoteIpValve` by customizing the `TomcatServlet
To configure Spring Security to require a secure channel for all (or some) requests, consider adding your own `SecurityFilterChain` bean that adds the following `HttpSecurity` configuration:
include::code:MySecurityConfig[]
include-code::MySecurityConfig[]

View File

@ -1,5 +1,6 @@
[[howto.spring-mvc]]
== Spring MVC
= Spring MVC
Spring Boot has a number of starters that include Spring MVC.
Note that some starters include a dependency on Spring MVC rather than include it directly.
This section answers common questions about Spring MVC and Spring Boot.
@ -7,10 +8,11 @@ This section answers common questions about Spring MVC and Spring Boot.
[[howto.spring-mvc.write-json-rest-service]]
=== Write a JSON REST Service
== Write a JSON REST Service
Any Spring `@RestController` in a Spring Boot application should render JSON response by default as long as Jackson2 is on the classpath, as shown in the following example:
include::code:MyController[]
include-code::MyController[]
As long as `MyThing` can be serialized by Jackson2 (true for a normal POJO or Groovy object), then `http://localhost:8080/thing` serves a JSON representation of it by default.
Note that, in a browser, you might sometimes see XML responses, because browsers tend to send accept headers that prefer XML.
@ -18,31 +20,32 @@ Note that, in a browser, you might sometimes see XML responses, because browsers
[[howto.spring-mvc.write-xml-rest-service]]
=== Write an XML REST Service
== Write an XML REST Service
If you have the Jackson XML extension (`jackson-dataformat-xml`) on the classpath, you can use it to render XML responses.
The previous example that we used for JSON would work.
To use the Jackson XML renderer, add the following dependency to your project:
[source,xml,indent=0,subs="verbatim"]
[source,xml]
----
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
</dependency>
----
If Jackson's XML extension is not available and JAXB is available, XML can be rendered with the additional requirement of having `MyThing` annotated as `@XmlRootElement`, as shown in the following example:
include::code:MyThing[]
include-code::MyThing[]
You will need to ensure that the JAXB library is part of your project, for example by adding:
[source,xml,indent=0,subs="verbatim"]
[source,xml]
----
<dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-runtime</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-runtime</artifactId>
</dependency>
----
NOTE: To get the server to render XML instead of JSON, you might have to send an `Accept: text/xml` header (or use a browser).
@ -50,7 +53,8 @@ NOTE: To get the server to render XML instead of JSON, you might have to send an
[[howto.spring-mvc.customize-jackson-objectmapper]]
=== Customize the Jackson ObjectMapper
== Customize the Jackson ObjectMapper
Spring MVC (client and server side) uses `HttpMessageConverters` to negotiate content conversion in an HTTP exchange.
If Jackson is on the classpath, you already get the default converter(s) provided by `Jackson2ObjectMapperBuilder`, an instance of which is auto-configured for you.
@ -104,7 +108,7 @@ These features are described in several enums (in Jackson) that map onto propert
|===
For example, to enable pretty print, set `spring.jackson.serialization.indent_output=true`.
Note that, thanks to the use of <<features#features.external-config.typesafe-configuration-properties.relaxed-binding, relaxed binding>>, the case of `indent_output` does not have to match the case of the corresponding enum constant, which is `INDENT_OUTPUT`.
Note that, thanks to the use of xref:reference:features/external-config.adoc#features.external-config.typesafe-configuration-properties.relaxed-binding[relaxed binding], the case of `indent_output` does not have to match the case of the corresponding enum constant, which is `INDENT_OUTPUT`.
This environment-based configuration is applied to the auto-configured `Jackson2ObjectMapperBuilder` bean and applies to any mappers created by using the builder, including the auto-configured `ObjectMapper` bean.
@ -121,12 +125,13 @@ If you provide any `@Beans` of type `MappingJackson2HttpMessageConverter`, they
Also, a convenience bean of type `HttpMessageConverters` is provided (and is always available if you use the default MVC configuration).
It has some useful methods to access the default and user-enhanced message converters.
See the "`<<howto#howto.spring-mvc.customize-responsebody-rendering>>`" section and the {spring-boot-autoconfigure-module-code}/web/servlet/WebMvcAutoConfiguration.java[`WebMvcAutoConfiguration`] source code for more details.
See the "`xref:spring-mvc.adoc#howto.spring-mvc.customize-responsebody-rendering[Customize the @ResponseBody Rendering]`" section and the {code-spring-boot-autoconfigure-src}/web/servlet/WebMvcAutoConfiguration.java[`WebMvcAutoConfiguration`] source code for more details.
[[howto.spring-mvc.customize-responsebody-rendering]]
=== Customize the @ResponseBody Rendering
== Customize the @ResponseBody Rendering
Spring uses `HttpMessageConverters` to render `@ResponseBody` (or responses from `@RestController`).
You can contribute additional converters by adding beans of the appropriate type in a Spring Boot context.
If a bean you add is of a type that would have been included by default anyway (such as `MappingJackson2HttpMessageConverter` for JSON conversions), it replaces the default value.
@ -137,12 +142,13 @@ As in normal MVC usage, any `WebMvcConfigurer` beans that you provide can also c
However, unlike with normal MVC, you can supply only additional converters that you need (because Spring Boot uses the same mechanism to contribute its defaults).
Finally, if you opt out of the Spring Boot default MVC configuration by providing your own `@EnableWebMvc` configuration, you can take control completely and do everything manually by using `getMessageConverters` from `WebMvcConfigurationSupport`.
See the {spring-boot-autoconfigure-module-code}/web/servlet/WebMvcAutoConfiguration.java[`WebMvcAutoConfiguration`] source code for more details.
See the {code-spring-boot-autoconfigure-src}/web/servlet/WebMvcAutoConfiguration.java[`WebMvcAutoConfiguration`] source code for more details.
[[howto.spring-mvc.multipart-file-uploads]]
=== Handling Multipart File Uploads
== Handling Multipart File Uploads
Spring Boot embraces the servlet 5 `jakarta.servlet.http.Part` API to support uploading files.
By default, Spring Boot configures Spring MVC with a maximum size of 1MB per file and a maximum of 10MB of file data in a single request.
You may override these values, the location to which intermediate data is stored (for example, to the `/tmp` directory), and the threshold past which data is flushed to disk by using the properties exposed in the `MultipartProperties` class.
@ -150,23 +156,24 @@ For example, if you want to specify that files be unlimited, set the configprop:
The multipart support is helpful when you want to receive multipart encoded file data as a `@RequestParam`-annotated parameter of type `MultipartFile` in a Spring MVC controller handler method.
See the {spring-boot-autoconfigure-module-code}/web/servlet/MultipartAutoConfiguration.java[`MultipartAutoConfiguration`] source for more details.
See the {code-spring-boot-autoconfigure-src}/web/servlet/MultipartAutoConfiguration.java[`MultipartAutoConfiguration`] source for more details.
NOTE: It is recommended to use the container's built-in support for multipart uploads rather than introducing an additional dependency such as Apache Commons File Upload.
[[howto.spring-mvc.switch-off-dispatcherservlet]]
=== Switch Off the Spring MVC DispatcherServlet
== Switch Off the Spring MVC DispatcherServlet
By default, all content is served from the root of your application (`/`).
If you would rather map to a different path, you can configure one as follows:
[source,yaml,indent=0,subs="verbatim",configprops,configblocks]
[configprops,yaml]
----
spring:
mvc:
servlet:
path: "/mypath"
spring:
mvc:
servlet:
path: "/mypath"
----
If you have additional servlets you can declare a `@Bean` of type `Servlet` or `ServletRegistrationBean` for each and Spring Boot will register them transparently to the container.
@ -177,14 +184,16 @@ Configuring the `DispatcherServlet` yourself is unusual but if you really need t
[[howto.spring-mvc.switch-off-default-configuration]]
=== Switch off the Default MVC Configuration
== Switch off the Default MVC Configuration
The easiest way to take complete control over MVC configuration is to provide your own `@Configuration` with the `@EnableWebMvc` annotation.
Doing so leaves all MVC configuration in your hands.
[[howto.spring-mvc.customize-view-resolvers]]
=== Customize ViewResolvers
== Customize ViewResolvers
A `ViewResolver` is a core component of Spring MVC, translating view names in `@Controller` to actual `View` implementations.
Note that `ViewResolvers` are mainly used in UI applications, rather than REST-style services (a `View` is not used to render a `@ResponseBody`).
There are many implementations of `ViewResolver` to choose from, and Spring on its own is not opinionated about which ones you should use.
@ -227,7 +236,7 @@ If you add your own, you have to be aware of the order and in which position you
For more detail, see the following sections:
* {spring-boot-autoconfigure-module-code}/web/servlet/WebMvcAutoConfiguration.java[`WebMvcAutoConfiguration`]
* {spring-boot-autoconfigure-module-code}/thymeleaf/ThymeleafAutoConfiguration.java[`ThymeleafAutoConfiguration`]
* {spring-boot-autoconfigure-module-code}/freemarker/FreeMarkerAutoConfiguration.java[`FreeMarkerAutoConfiguration`]
* {spring-boot-autoconfigure-module-code}/groovy/template/GroovyTemplateAutoConfiguration.java[`GroovyTemplateAutoConfiguration`]
* {code-spring-boot-autoconfigure-src}/web/servlet/WebMvcAutoConfiguration.java[`WebMvcAutoConfiguration`]
* {code-spring-boot-autoconfigure-src}/thymeleaf/ThymeleafAutoConfiguration.java[`ThymeleafAutoConfiguration`]
* {code-spring-boot-autoconfigure-src}/freemarker/FreeMarkerAutoConfiguration.java[`FreeMarkerAutoConfiguration`]
* {code-spring-boot-autoconfigure-src}/groovy/template/GroovyTemplateAutoConfiguration.java[`GroovyTemplateAutoConfiguration`]

View File

@ -1,34 +1,34 @@
[[howto.testing]]
== Testing
= Testing
Spring Boot includes a number of testing utilities and support classes as well as a dedicated starter that provides common test dependencies.
This section answers common questions about testing.
[[howto.testing.with-spring-security]]
=== Testing With Spring Security
== Testing With Spring Security
Spring Security provides support for running tests as a specific user.
For example, the test in the snippet below will run with an authenticated user that has the `ADMIN` role.
include::code:MySecurityTests[]
include-code::MySecurityTests[]
Spring Security provides comprehensive integration with Spring MVC Test, and this can also be used when testing controllers using the `@WebMvcTest` slice and `MockMvc`.
For additional details on Spring Security's testing support, see Spring Security's {spring-security-docs}/servlet/test/index.html[reference documentation].
For additional details on Spring Security's testing support, see Spring Security's {url-spring-security-docs}/servlet/test/index.html[reference documentation].
[[howto.testing.slice-tests]]
=== Structure `@Configuration` classes for inclusion in slice tests
== Structure `@Configuration` classes for inclusion in slice tests
Slice tests work by restricting Spring Framework's component scanning to a limited set of components based on their type.
For any beans that are not created through component scanning, for example, beans that are created using the `@Bean` annotation, slice tests will not be able to include/exclude them from the application context.
Consider this example:
[source,java,indent=0,subs="verbatim"]
----
include::{docs-java}/howto/testing/slicetests/MyConfiguration.java[]
----
include-code::MyConfiguration[]
For a `@WebMvcTest` for an application with the above `@Configuration` class, you might expect to have the `SecurityFilterChain` bean in the application context so that you can test if your controller endpoints are secured properly.
However, `MyConfiguration` is not picked up by @WebMvcTest's component scanning filter because it doesn't match any of the types specified by the filter.
@ -36,15 +36,9 @@ You can include the configuration explicitly by annotating the test class with `
This will load all the beans in `MyConfiguration` including the `BasicDataSource` bean which isn't required when testing the web tier.
Splitting the configuration class into two will enable importing just the security configuration.
[source,java,indent=0,subs="verbatim"]
----
include::{docs-java}/howto/testing/slicetests/MySecurityConfiguration.java[]
----
include-code::MySecurityConfiguration[]
[source,java,indent=0,subs="verbatim"]
----
include::{docs-java}/howto/testing/slicetests/MyDatasourceConfiguration.java[]
----
include-code::MyDatasourceConfiguration[]
Having a single configuration class can be inefficient when beans of a certain domain need to be included in slice tests.
Instead, structuring the application's configuration as multiple granular classes with beans for a specific domain can enable importing them only for specific slice tests.

View File

@ -1,12 +1,13 @@
[[howto.traditional-deployment]]
== Traditional Deployment
= Traditional Deployment
Spring Boot supports traditional deployment as well as more modern forms of deployment.
This section answers common questions about traditional deployment.
[[howto.traditional-deployment.war]]
=== Create a Deployable War File
== Create a Deployable War File
WARNING: Because Spring WebFlux does not strictly depend on the servlet API and applications are deployed by default on an embedded Reactor Netty server, War deployment is not supported for WebFlux applications.
@ -14,21 +15,21 @@ The first step in producing a deployable war file is to provide a `SpringBootSer
Doing so makes use of Spring Framework's servlet 3.0 support and lets you configure your application when it is launched by the servlet container.
Typically, you should update your application's main class to extend `SpringBootServletInitializer`, as shown in the following example:
include::code:MyApplication[]
include-code::MyApplication[]
The next step is to update your build configuration such that your project produces a war file rather than a jar file.
If you use Maven and `spring-boot-starter-parent` (which configures Maven's war plugin for you), all you need to do is to modify `pom.xml` to change the packaging to war, as follows:
[source,xml,indent=0,subs="verbatim"]
[source,xml]
----
<packaging>war</packaging>
<packaging>war</packaging>
----
If you use Gradle, you need to modify `build.gradle` to apply the war plugin to the project, as follows:
[source,gradle,indent=0,subs="verbatim"]
[source,gradle]
----
apply plugin: 'war'
apply plugin: 'war'
----
The final step in the process is to ensure that the embedded servlet container does not interfere with the servlet container to which the war file is deployed.
@ -36,47 +37,48 @@ To do so, you need to mark the embedded servlet container dependency as being pr
If you use Maven, the following example marks the servlet container (Tomcat, in this case) as being provided:
[source,xml,indent=0,subs="verbatim"]
[source,xml]
----
<dependencies>
<!-- ... -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<!-- ... -->
</dependencies>
<dependencies>
<!-- ... -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<!-- ... -->
</dependencies>
----
If you use Gradle, the following example marks the servlet container (Tomcat, in this case) as being provided:
[source,gradle,indent=0,subs="verbatim"]
[source,gradle]
----
dependencies {
// ...
providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat'
// ...
}
dependencies {
// ...
providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat'
// ...
}
----
TIP: `providedRuntime` is preferred to Gradle's `compileOnly` configuration.
Among other limitations, `compileOnly` dependencies are not on the test classpath, so any web-based integration tests fail.
If you use the <<build-tool-plugins#build-tool-plugins, Spring Boot build tools>>, marking the embedded servlet container dependency as provided produces an executable war file with the provided dependencies packaged in a `lib-provided` directory.
If you use the xref:build-tool-plugin:index.adoc[Spring Boot build tools], marking the embedded servlet container dependency as provided produces an executable war file with the provided dependencies packaged in a `lib-provided` directory.
This means that, in addition to being deployable to a servlet container, you can also run your application by using `java -jar` on the command line.
[[howto.traditional-deployment.convert-existing-application]]
=== Convert an Existing Application to Spring Boot
== Convert an Existing Application to Spring Boot
To convert an existing non-web Spring application to a Spring Boot application, replace the code that creates your `ApplicationContext` and replace it with calls to `SpringApplication` or `SpringApplicationBuilder`.
Spring MVC web applications are generally amenable to first creating a deployable war application and then migrating it later to an executable war or jar.
See the https://spring.io/guides/gs/convert-jar-to-war/[Getting Started Guide on Converting a jar to a war].
To create a deployable war by extending `SpringBootServletInitializer` (for example, in a class called `Application`) and adding the Spring Boot `@SpringBootApplication` annotation, use code similar to that shown in the following example:
include::code:MyApplication[tag=!main]
include-code::MyApplication[tag=!main]
Remember that, whatever you put in the `sources` is merely a Spring `ApplicationContext`.
Normally, anything that already works should work here.
@ -95,13 +97,13 @@ If you have other features in your application (for instance, using other servle
Once the war file is working, you can make it executable by adding a `main` method to your `Application`, as shown in the following example:
include::code:MyApplication[tag=main]
include-code::MyApplication[tag=main]
[NOTE]
====
If you intend to start your application as a war or as an executable application, you need to share the customizations of the builder in a method that is both available to the `SpringBootServletInitializer` callback and in the `main` method in a class similar to the following:
include::code:both/MyApplication[]
include-code::both/MyApplication[]
====
Applications can fall into more than one category:
@ -117,7 +119,7 @@ Servlet 3.0+ applications might translate pretty easily if they already use the
Normally, all the code from an existing `WebApplicationInitializer` can be moved into a `SpringBootServletInitializer`.
If your existing application has more than one `ApplicationContext` (for example, if it uses `AbstractDispatcherServletInitializer`) then you might be able to combine all your context sources into a single `SpringApplication`.
The main complication you might encounter is if combining does not work and you need to maintain the context hierarchy.
See the <<howto#howto.application.context-hierarchy, entry on building a hierarchy>> for examples.
See the xref:application.adoc#howto.application.context-hierarchy[entry on building a hierarchy] for examples.
An existing parent context that contains web-specific features usually needs to be broken up so that all the `ServletContextAware` components are in the child context.
Applications that are not already Spring applications might be convertible to Spring Boot applications, and the previously mentioned guidance may help.
@ -127,30 +129,31 @@ In that case, we suggest https://stackoverflow.com/questions/tagged/spring-boot[
[[howto.traditional-deployment.weblogic]]
=== Deploying a WAR to WebLogic
== Deploying a WAR to WebLogic
To deploy a Spring Boot application to WebLogic, you must ensure that your servlet initializer *directly* implements `WebApplicationInitializer` (even if you extend from a base class that already implements it).
A typical initializer for WebLogic should resemble the following example:
include::code:MyApplication[]
include-code::MyApplication[]
If you use Logback, you also need to tell WebLogic to prefer the packaged version rather than the version that was pre-installed with the server.
You can do so by adding a `WEB-INF/weblogic.xml` file with the following contents:
[source,xml,indent=0,subs="verbatim"]
[source,xml]
----
<?xml version="1.0" encoding="UTF-8"?>
<wls:weblogic-web-app
xmlns:wls="http://xmlns.oracle.com/weblogic/weblogic-web-app"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
https://java.sun.com/xml/ns/javaee/ejb-jar_3_0.xsd
http://xmlns.oracle.com/weblogic/weblogic-web-app
https://xmlns.oracle.com/weblogic/weblogic-web-app/1.4/weblogic-web-app.xsd">
<wls:container-descriptor>
<wls:prefer-application-packages>
<wls:package-name>org.slf4j</wls:package-name>
</wls:prefer-application-packages>
</wls:container-descriptor>
</wls:weblogic-web-app>
<?xml version="1.0" encoding="UTF-8"?>
<wls:weblogic-web-app
xmlns:wls="http://xmlns.oracle.com/weblogic/weblogic-web-app"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
https://java.sun.com/xml/ns/javaee/ejb-jar_3_0.xsd
http://xmlns.oracle.com/weblogic/weblogic-web-app
https://xmlns.oracle.com/weblogic/weblogic-web-app/1.4/weblogic-web-app.xsd">
<wls:container-descriptor>
<wls:prefer-application-packages>
<wls:package-name>org.slf4j</wls:package-name>
</wls:prefer-application-packages>
</wls:container-descriptor>
</wls:weblogic-web-app>
----

View File

@ -1,5 +1,6 @@
[[howto.webserver]]
== Embedded Web Servers
= Embedded Web Servers
Each Spring Boot web application includes an embedded web server.
This feature leads to a number of how-to questions, including how to change the embedded server and how to configure the embedded server.
This section answers those questions.
@ -7,7 +8,8 @@ This section answers those questions.
[[howto.webserver.use-another]]
=== Use Another Web Server
== Use Another Web Server
Many Spring Boot starters include default embedded containers.
* For servlet stack applications, the `spring-boot-starter-web` includes Tomcat by including `spring-boot-starter-tomcat`, but you can use `spring-boot-starter-jetty` or `spring-boot-starter-undertow` instead.
@ -18,39 +20,39 @@ To help with this process, Spring Boot provides a separate starter for each of t
The following Maven example shows how to exclude Tomcat and include Jetty for Spring MVC:
[source,xml,indent=0,subs="verbatim"]
[source,xml]
----
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<!-- Exclude the Tomcat dependency -->
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- Use Jetty instead -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<!-- Exclude the Tomcat dependency -->
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- Use Jetty instead -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
----
The following Gradle example configures the necessary dependencies and a {gradle-docs}/resolution_rules.html#sec:module_replacement[module replacement] to use Undertow in place of Reactor Netty for Spring WebFlux:
The following Gradle example configures the necessary dependencies and a {url-gradle-docs}/resolution_rules.html#sec:module_replacement[module replacement] to use Undertow in place of Reactor Netty for Spring WebFlux:
[source,gradle,indent=0,subs="verbatim"]
[source,gradle]
----
dependencies {
implementation "org.springframework.boot:spring-boot-starter-undertow"
implementation "org.springframework.boot:spring-boot-starter-webflux"
modules {
module("org.springframework.boot:spring-boot-starter-reactor-netty") {
replacedBy("org.springframework.boot:spring-boot-starter-undertow", "Use Undertow instead of Reactor Netty")
}
dependencies {
implementation "org.springframework.boot:spring-boot-starter-undertow"
implementation "org.springframework.boot:spring-boot-starter-webflux"
modules {
module("org.springframework.boot:spring-boot-starter-reactor-netty") {
replacedBy("org.springframework.boot:spring-boot-starter-undertow", "Use Undertow instead of Reactor Netty")
}
}
}
----
NOTE: `spring-boot-starter-reactor-netty` is required to use the `WebClient` class, so you may need to keep a dependency on Netty even when you need to include a different HTTP server.
@ -58,44 +60,48 @@ NOTE: `spring-boot-starter-reactor-netty` is required to use the `WebClient` cla
[[howto.webserver.disable]]
=== Disabling the Web Server
== Disabling the Web Server
If your classpath contains the necessary bits to start a web server, Spring Boot will automatically start it.
To disable this behavior configure the `WebApplicationType` in your `application.properties`, as shown in the following example:
[source,yaml,indent=0,subs="verbatim",configprops,configblocks]
[configprops,yaml]
----
spring:
main:
web-application-type: "none"
spring:
main:
web-application-type: "none"
----
[[howto.webserver.change-port]]
=== Change the HTTP Port
== Change the HTTP Port
In a standalone application, the main HTTP port defaults to `8080` but can be set with configprop:server.port[] (for example, in `application.properties` or as a System property).
Thanks to relaxed binding of `Environment` values, you can also use configprop:server.port[format=envvar] (for example, as an OS environment variable).
To switch off the HTTP endpoints completely but still create a `WebApplicationContext`, use `server.port=-1` (doing so is sometimes useful for testing).
For more details, see "`<<web#web.servlet.embedded-container.customizing>>`" in the '`Spring Boot Features`' section, or the {spring-boot-autoconfigure-module-code}/web/ServerProperties.java[`ServerProperties`] source code.
For more details, see "`xref:reference:web/servlet.adoc#web.servlet.embedded-container.customizing[Customizing Embedded Servlet Containers]`" in the '`Spring Boot Features`' section, or the {code-spring-boot-autoconfigure-src}/web/ServerProperties.java[`ServerProperties`] source code.
[[howto.webserver.use-random-port]]
=== Use a Random Unassigned HTTP Port
== Use a Random Unassigned HTTP Port
To scan for a free port (using OS natives to prevent clashes) use `server.port=0`.
[[howto.webserver.discover-port]]
=== Discover the HTTP Port at Runtime
== Discover the HTTP Port at Runtime
You can access the port the server is running on from log output or from the `WebServerApplicationContext` through its `WebServer`.
The best way to get that and be sure it has been initialized is to add a `@Bean` of type `ApplicationListener<WebServerInitializedEvent>` and pull the container out of the event when it is published.
Tests that use `@SpringBootTest(webEnvironment=WebEnvironment.RANDOM_PORT)` can also inject the actual port into a field by using the `@LocalServerPort` annotation, as shown in the following example:
include::code:MyWebIntegrationTests[]
include-code::MyWebIntegrationTests[]
[NOTE]
====
@ -108,15 +114,16 @@ Contrary to a test, application code callbacks are processed early (before the v
[[howto.webserver.enable-response-compression]]
=== Enable HTTP Response Compression
== Enable HTTP Response Compression
HTTP response compression is supported by Jetty, Tomcat, Reactor Netty, and Undertow.
It can be enabled in `application.properties`, as follows:
[source,yaml,indent=0,subs="verbatim",configprops,configblocks]
[configprops,yaml]
----
server:
compression:
enabled: true
server:
compression:
enabled: true
----
By default, responses must be at least 2048 bytes in length for compression to be performed.
@ -138,18 +145,19 @@ You can configure this behavior by setting the configprop:server.compression.mim
[[howto.webserver.configure-ssl]]
=== Configure SSL
== Configure SSL
SSL can be configured declaratively by setting the various `+server.ssl.*+` properties, typically in `application.properties` or `application.yaml`.
The following example shows setting SSL properties using a Java KeyStore file:
[source,yaml,indent=0,subs="verbatim",configprops,configblocks]
[configprops,yaml]
----
server:
port: 8443
ssl:
key-store: "classpath:keystore.jks"
key-store-password: "secret"
key-password: "another-secret"
server:
port: 8443
ssl:
key-store: "classpath:keystore.jks"
key-store-password: "secret"
key-password: "another-secret"
----
Using configuration such as the preceding example means the application no longer supports a plain HTTP connector at port 8080.
@ -160,69 +168,73 @@ We recommend using `application.properties` to configure HTTPS, as the HTTP conn
[[howto.webserver.configure-ssl.pem-files]]
==== Using PEM-encoded files
=== Using PEM-encoded files
You can use PEM-encoded files instead of Java KeyStore files.
You should use PKCS#8 key files wherever possible.
PEM-encoded PKCS#8 key files start with a `-----BEGIN PRIVATE KEY-----` or `-----BEGIN ENCRYPTED PRIVATE KEY-----` header.
If you have files in other formats, e.g., PKCS#1 (`-----BEGIN RSA PRIVATE KEY-----`) or SEC 1 (`-----BEGIN EC PRIVATE KEY-----`), you can convert them to PKCS#8 using OpenSSL:
[source,shell,indent=0,subs="verbatim,attributes"]
[source,shell,subs="verbatim,attributes"]
----
openssl pkcs8 -topk8 -nocrypt -in <input file> -out <output file>
----
The following example shows setting SSL properties using PEM-encoded certificate and private key files:
[source,yaml,indent=0,subs="verbatim",configprops,configblocks]
[configprops,yaml]
----
server:
port: 8443
ssl:
certificate: "classpath:my-cert.crt"
certificate-private-key: "classpath:my-cert.key"
trust-certificate: "classpath:ca-cert.crt"
server:
port: 8443
ssl:
certificate: "classpath:my-cert.crt"
certificate-private-key: "classpath:my-cert.key"
trust-certificate: "classpath:ca-cert.crt"
----
Alternatively, the SSL trust material can be configured in an <<features#features.ssl,SSL bundle>> and applied to the web server as shown in this example:
Alternatively, the SSL trust material can be configured in an xref:reference:features/ssl.adoc[SSL bundle] and applied to the web server as shown in this example:
[source,yaml,indent=0,subs="verbatim",configprops,configblocks]
[configprops,yaml]
----
server:
port: 8443
ssl:
bundle: "example"
server:
port: 8443
ssl:
bundle: "example"
----
NOTE: The `server.ssl.bundle` property can not be combined with the discrete Java KeyStore or PEM property options under `server.ssl`.
See {spring-boot-module-code}/web/server/Ssl.java[`Ssl`] for details of all of the supported properties.
See {code-spring-boot-src}/web/server/Ssl.java[`Ssl`] for details of all of the supported properties.
[[howto.webserver.configure-http2]]
=== Configure HTTP/2
== Configure HTTP/2
You can enable HTTP/2 support in your Spring Boot application with the configprop:server.http2.enabled[] configuration property.
Both `h2` (HTTP/2 over TLS) and `h2c` (HTTP/2 over TCP) are supported.
To use `h2`, SSL must also be enabled.
When SSL is not enabled, `h2c` will be used.
You may, for example, want to use `h2c` when your application is <<howto.webserver.use-behind-a-proxy-server,running behind a proxy server>> that is performing TLS termination.
You may, for example, want to use `h2c` when your application is xref:webserver.adoc#howto.webserver.use-behind-a-proxy-server[running behind a proxy server] that is performing TLS termination.
[[howto.webserver.configure-http2.tomcat]]
==== HTTP/2 With Tomcat
=== HTTP/2 With Tomcat
Spring Boot ships by default with Tomcat 10.1.x which supports `h2c` and `h2` out of the box.
Alternatively, you can use `libtcnative` for `h2` support if the library and its dependencies are installed on the host operating system.
The library directory must be made available, if not already, to the JVM library path.
You can do so with a JVM argument such as `-Djava.library.path=/usr/local/opt/tomcat-native/lib`.
More on this in the {tomcat-docs}/apr.html[official Tomcat documentation].
More on this in the {url-tomcat-docs}/apr.html[official Tomcat documentation].
[[howto.webserver.configure-http2.jetty]]
==== HTTP/2 With Jetty
=== HTTP/2 With Jetty
For HTTP/2 support, Jetty requires the additional `org.eclipse.jetty.http2:jetty-http2-server` dependency.
To use `h2c` no other dependencies are required.
To use `h2`, you also need to choose one of the following dependencies, depending on your deployment:
@ -233,7 +245,8 @@ To use `h2`, you also need to choose one of the following dependencies, dependin
[[howto.webserver.configure-http2.netty]]
==== HTTP/2 With Reactor Netty
=== HTTP/2 With Reactor Netty
The `spring-boot-webflux-starter` is using by default Reactor Netty as a server.
Reactor Netty supports `h2c` and `h2` out of the box.
For optimal runtime performance, this server also supports `h2` with native libraries.
@ -245,25 +258,27 @@ Developers can choose to import only the required dependencies using a classifie
[[howto.webserver.configure-http2.undertow]]
==== HTTP/2 With Undertow
=== HTTP/2 With Undertow
Undertow supports `h2c` and `h2` out of the box.
[[howto.webserver.configure]]
=== Configure the Web Server
== Configure the Web Server
Generally, you should first consider using one of the many available configuration keys and customize your web server by adding new entries in your `application.properties` or `application.yaml` file.
See "`<<howto#howto.properties-and-configuration.discover-build-in-options-for-external-properties>>`").
See "`xref:properties-and-configuration.adoc#howto.properties-and-configuration.discover-build-in-options-for-external-properties[Discover Built-in Options for External Properties]`").
The `server.{asterisk}` namespace is quite useful here, and it includes namespaces like `server.tomcat.{asterisk}`, `server.jetty.{asterisk}` and others, for server-specific features.
See the list of <<application-properties#appendix.application-properties>>.
See the list of xref:appendix:application-properties/index.adoc[Common Application Properties].
The previous sections covered already many common use cases, such as compression, SSL or HTTP/2.
However, if a configuration key does not exist for your use case, you should then look at {spring-boot-module-api}/web/server/WebServerFactoryCustomizer.html[`WebServerFactoryCustomizer`].
However, if a configuration key does not exist for your use case, you should then look at xref:api:java/org/springframework/boot/web/server/WebServerFactoryCustomizer.html[`WebServerFactoryCustomizer`].
You can declare such a component and get access to the server factory relevant to your choice: you should select the variant for the chosen Server (Tomcat, Jetty, Reactor Netty, Undertow) and the chosen web stack (servlet or reactive).
The example below is for Tomcat with the `spring-boot-starter-web` (servlet stack):
include::code:MyTomcatWebServerCustomizer[]
include-code::MyTomcatWebServerCustomizer[]
NOTE: Spring Boot uses that infrastructure internally to auto-configure the server.
Auto-configured `WebServerFactoryCustomizer` beans have an order of `0` and will be processed before any user-defined customizers, unless it has an explicit order that states otherwise.
@ -300,16 +315,18 @@ When you do so, auto-configured customizers are still applied on your custom fac
[[howto.webserver.add-servlet-filter-listener]]
=== Add a Servlet, Filter, or Listener to an Application
== Add a Servlet, Filter, or Listener to an Application
In a servlet stack application, that is with the `spring-boot-starter-web`, there are two ways to add `Servlet`, `Filter`, `ServletContextListener`, and the other listeners supported by the Servlet API to your application:
* <<howto#howto.webserver.add-servlet-filter-listener.spring-bean>>
* <<howto#howto.webserver.add-servlet-filter-listener.using-scanning>>
* xref:webserver.adoc#howto.webserver.add-servlet-filter-listener.spring-bean[Add a Servlet, Filter, or Listener by Using a Spring Bean]
* xref:webserver.adoc#howto.webserver.add-servlet-filter-listener.using-scanning[Add Servlets, Filters, and Listeners by Using Classpath Scanning]
[[howto.webserver.add-servlet-filter-listener.spring-bean]]
==== Add a Servlet, Filter, or Listener by Using a Spring Bean
=== Add a Servlet, Filter, or Listener by Using a Spring Bean
To add a `Servlet`, `Filter`, or servlet `*Listener` by using a Spring bean, you must provide a `@Bean` definition for it.
Doing so can be very useful when you want to inject configuration or dependencies.
However, you must be very careful that they do not cause eager initialization of too many other beans, because they have to be installed in the container very early in the application lifecycle.
@ -324,40 +341,43 @@ If no `dispatcherType` is specified on a filter registration, `REQUEST` is used.
This aligns with the servlet specification's default dispatcher type.
====
Like any other Spring bean, you can define the order of servlet filter beans; please make sure to check the "`<<web#web.servlet.embedded-container.servlets-filters-listeners.beans>>`" section.
Like any other Spring bean, you can define the order of servlet filter beans; please make sure to check the "`xref:reference:web/servlet.adoc#web.servlet.embedded-container.servlets-filters-listeners.beans[Registering Servlets, Filters, and Listeners as Spring Beans]`" section.
[[howto.webserver.add-servlet-filter-listener.spring-bean.disable]]
===== Disable Registration of a Servlet or Filter
As <<howto#howto.webserver.add-servlet-filter-listener.spring-bean,described earlier>>, any `Servlet` or `Filter` beans are registered with the servlet container automatically.
==== Disable Registration of a Servlet or Filter
As xref:webserver.adoc#howto.webserver.add-servlet-filter-listener.spring-bean[described earlier], any `Servlet` or `Filter` beans are registered with the servlet container automatically.
To disable registration of a particular `Filter` or `Servlet` bean, create a registration bean for it and mark it as disabled, as shown in the following example:
include::code:MyFilterConfiguration[]
include-code::MyFilterConfiguration[]
[[howto.webserver.add-servlet-filter-listener.using-scanning]]
==== Add Servlets, Filters, and Listeners by Using Classpath Scanning
=== Add Servlets, Filters, and Listeners by Using Classpath Scanning
`@WebServlet`, `@WebFilter`, and `@WebListener` annotated classes can be automatically registered with an embedded servlet container by annotating a `@Configuration` class with `@ServletComponentScan` and specifying the package(s) containing the components that you want to register.
By default, `@ServletComponentScan` scans from the package of the annotated class.
[[howto.webserver.configure-access-logs]]
=== Configure Access Logging
== Configure Access Logging
Access logs can be configured for Tomcat, Undertow, and Jetty through their respective namespaces.
For instance, the following settings log access on Tomcat with a {tomcat-docs}/config/valve.html#Access_Logging[custom pattern].
For instance, the following settings log access on Tomcat with a {url-tomcat-docs}/config/valve.html#Access_Logging[custom pattern].
[source,yaml,indent=0,subs="verbatim",configprops,configblocks]
[configprops,yaml]
----
server:
tomcat:
basedir: "my-tomcat"
accesslog:
enabled: true
pattern: "%t %a %r %s (%D microseconds)"
server:
tomcat:
basedir: "my-tomcat"
accesslog:
enabled: true
pattern: "%t %a %r %s (%D microseconds)"
----
NOTE: The default location for logs is a `logs` directory relative to the Tomcat base directory.
@ -366,16 +386,16 @@ In the preceding example, the logs are available in `my-tomcat/logs` relative to
Access logging for Undertow can be configured in a similar fashion, as shown in the following example:
[source,yaml,indent=0,subs="verbatim",configprops,configblocks]
[configprops,yaml]
----
server:
undertow:
accesslog:
enabled: true
pattern: "%t %a %r %s (%D milliseconds)"
options:
server:
record-request-start-time: true
server:
undertow:
accesslog:
enabled: true
pattern: "%t %a %r %s (%D milliseconds)"
options:
server:
record-request-start-time: true
----
Note that, in addition to enabling access logging and configuring its pattern, recording request start times has also been enabled.
@ -385,13 +405,13 @@ You can customize this location by setting the configprop:server.undertow.access
Finally, access logging for Jetty can also be configured as follows:
[source,yaml,indent=0,subs="verbatim",configprops,configblocks]
[configprops,yaml]
----
server:
jetty:
accesslog:
enabled: true
filename: "/var/log/jetty-access.log"
server:
jetty:
accesslog:
enabled: true
filename: "/var/log/jetty-access.log"
----
By default, logs are redirected to `System.err`.
@ -400,7 +420,8 @@ For more details, see the Jetty documentation.
[[howto.webserver.use-behind-a-proxy-server]]
=== Running Behind a Front-end Proxy Server
== Running Behind a Front-end Proxy Server
If your application is running behind a proxy, a load-balancer or in the cloud, the request information (like the host, port, scheme...) might change along the way.
Your application may be running on `10.10.10.10:8080`, but HTTP clients should only see `example.org`.
@ -411,7 +432,7 @@ There are also non-standard headers, like `X-Forwarded-Host`, `X-Forwarded-Port`
If the proxy adds the commonly used `X-Forwarded-For` and `X-Forwarded-Proto` headers, setting `server.forward-headers-strategy` to `NATIVE` is enough to support those.
With this option, the Web servers themselves natively support this feature; you can check their specific documentation to learn about specific behavior.
If this is not enough, Spring Framework provides a {spring-framework-docs}/web/webmvc/filters.html#filters-forwarded-headers[ForwardedHeaderFilter] for the servlet stack and a {spring-framework-docs}/web/webflux/reactive-spring.html#webflux-forwarded-headers[ForwardedHeaderTransformer] for the reactive stack.
If this is not enough, Spring Framework provides a {url-spring-framework-docs}/web/webmvc/filters.html#filters-forwarded-headers[ForwardedHeaderFilter] for the servlet stack and a {url-spring-framework-docs}/web/webflux/reactive-spring.html#webflux-forwarded-headers[ForwardedHeaderTransformer] for the reactive stack.
You can use them in your application by setting configprop:server.forward-headers-strategy[] to `FRAMEWORK`.
TIP: If you are using Tomcat and terminating SSL at the proxy, configprop:server.tomcat.redirect-context-root[] should be set to `false`.
@ -423,28 +444,29 @@ In all other instances, it defaults to `NONE`.
[[howto.webserver.use-behind-a-proxy-server.tomcat]]
==== Customize Tomcat's Proxy Configuration
=== Customize Tomcat's Proxy Configuration
If you use Tomcat, you can additionally configure the names of the headers used to carry "`forwarded`" information, as shown in the following example:
[source,yaml,indent=0,subs="verbatim",configprops,configblocks]
[configprops,yaml]
----
server:
tomcat:
remoteip:
remote-ip-header: "x-your-remote-ip-header"
protocol-header: "x-your-protocol-header"
server:
tomcat:
remoteip:
remote-ip-header: "x-your-remote-ip-header"
protocol-header: "x-your-protocol-header"
----
Tomcat is also configured with a regular expression that matches internal proxies that are to be trusted.
See the <<application-properties.adoc#application-properties.server.server.tomcat.remoteip.internal-proxies,configprop:server.tomcat.remoteip.internal-proxies[] entry in the appendix>> for its default value.
See the xref:appendix:application-properties/index.adoc#application-properties.server.server.tomcat.remoteip.internal-proxies[configprop:server.tomcat.remoteip.internal-proxies[] entry in the appendix] for its default value.
You can customize the valve's configuration by adding an entry to `application.properties`, as shown in the following example:
[source,yaml,indent=0,subs="verbatim",configprops,configblocks]
[configprops,yaml]
----
server:
tomcat:
remoteip:
internal-proxies: "192\\.168\\.\\d{1,3}\\.\\d{1,3}"
server:
tomcat:
remoteip:
internal-proxies: "192\\.168\\.\\d{1,3}\\.\\d{1,3}"
----
NOTE: You can trust all proxies by setting the `internal-proxies` to empty (but do not do so in production).
@ -454,20 +476,22 @@ You can take complete control of the configuration of Tomcat's `RemoteIpValve` b
[[howto.webserver.enable-multiple-connectors-in-tomcat]]
=== Enable Multiple Connectors with Tomcat
== Enable Multiple Connectors with Tomcat
You can add an `org.apache.catalina.connector.Connector` to the `TomcatServletWebServerFactory`, which can allow multiple connectors, including HTTP and HTTPS connectors, as shown in the following example:
include::code:MyTomcatConfiguration[]
include-code::MyTomcatConfiguration[]
[[howto.webserver.enable-tomcat-mbean-registry]]
=== Enable Tomcat's MBean Registry
== Enable Tomcat's MBean Registry
Embedded Tomcat's MBean registry is disabled by default.
This minimizes Tomcat's memory footprint.
If you want to use Tomcat's MBeans, for example so that they can be used by Micrometer to expose metrics, you must use the configprop:server.tomcat.mbeanregistry.enabled[] property to do so, as shown in the following example:
[source,yaml,indent=0,subs="verbatim",configprops,configblocks]
[configprops,yaml]
----
server:
tomcat:
@ -478,18 +502,20 @@ server:
[[howto.webserver.enable-multiple-listeners-in-undertow]]
=== Enable Multiple Listeners with Undertow
== Enable Multiple Listeners with Undertow
Add an `UndertowBuilderCustomizer` to the `UndertowServletWebServerFactory` and add a listener to the `Builder`, as shown in the following example:
include::code:MyUndertowConfiguration[]
include-code::MyUndertowConfiguration[]
[[howto.webserver.create-websocket-endpoints-using-serverendpoint]]
=== Create WebSocket Endpoints Using @ServerEndpoint
== Create WebSocket Endpoints Using @ServerEndpoint
If you want to use `@ServerEndpoint` in a Spring Boot application that used an embedded container, you must declare a single `ServerEndpointExporter` `@Bean`, as shown in the following example:
include::code:MyWebSocketConfiguration[]
include-code::MyWebSocketConfiguration[]
The bean shown in the preceding example registers any `@ServerEndpoint` annotated beans with the underlying WebSocket container.
When deployed to a standalone servlet container, this role is performed by a servlet container initializer, and the `ServerEndpointExporter` bean is not required.

View File

@ -0,0 +1,22 @@
* xref:how-to:index.adoc[]
** xref:how-to:application.adoc[]
** xref:how-to:properties-and-configuration.adoc[]
** xref:how-to:webserver.adoc[]
** xref:how-to:spring-mvc.adoc[]
** xref:how-to:jersey.adoc[]
** xref:how-to:http-clients.adoc[]
** xref:how-to:logging.adoc[]
** xref:how-to:data-access.adoc[]
** xref:how-to:data-initialization.adoc[]
** xref:how-to:nosql.adoc[]
** xref:how-to:messaging.adoc[]
** xref:how-to:batch.adoc[]
** xref:how-to:actuator.adoc[]
** xref:how-to:security.adoc[]
** xref:how-to:hotswapping.adoc[]
** xref:how-to:testing.adoc[]
** xref:how-to:build.adoc[]
** xref:how-to:aot.adoc[]
** xref:how-to:traditional-deployment.adoc[]
** xref:how-to:docker-compose.adoc[]

View File

@ -1,5 +1,6 @@
[[actuator.auditing]]
== Auditing
= Auditing
Once Spring Security is in play, Spring Boot Actuator has a flexible audit framework that publishes events (by default, "`authentication success`", "`failure`" and "`access denied`" exceptions).
This feature can be very useful for reporting and for implementing a lock-out policy based on authentication failures.
@ -11,7 +12,8 @@ For production environments, consider creating your own alternative `AuditEventR
[[actuator.auditing.custom]]
=== Custom Auditing
== Custom Auditing
To customize published security events, you can provide your own implementations of `AbstractAuthenticationAuditListener` and `AbstractAuthorizationAuditListener`.
You can also use the audit services for your own business events.

View File

@ -1,5 +1,6 @@
[[actuator.cloud-foundry]]
== Cloud Foundry Support
= Cloud Foundry Support
Spring Boot's actuator module includes additional support that is activated when you deploy to a compatible Cloud Foundry instance.
The `/cloudfoundryapplication` path provides an alternative secured route to all `@Endpoint` beans.
@ -12,34 +13,37 @@ To use the endpoint, you must pass a valid UAA token with the request.
[[actuator.cloud-foundry.disable]]
=== Disabling Extended Cloud Foundry Actuator Support
== Disabling Extended Cloud Foundry Actuator Support
If you want to fully disable the `/cloudfoundryapplication` endpoints, you can add the following setting to your `application.properties` file:
[source,yaml,indent=0,subs="verbatim",configprops,configblocks]
[configprops,yaml]
----
management:
cloudfoundry:
enabled: false
management:
cloudfoundry:
enabled: false
----
[[actuator.cloud-foundry.ssl]]
=== Cloud Foundry Self-signed Certificates
== Cloud Foundry Self-signed Certificates
By default, the security verification for `/cloudfoundryapplication` endpoints makes SSL calls to various Cloud Foundry services.
If your Cloud Foundry UAA or Cloud Controller services use self-signed certificates, you need to set the following property:
[source,yaml,indent=0,subs="verbatim",configprops,configblocks]
[configprops,yaml]
----
management:
cloudfoundry:
skip-ssl-validation: true
management:
cloudfoundry:
skip-ssl-validation: true
----
[[actuator.cloud-foundry.custom-context-path]]
=== Custom Context Path
== Custom Context Path
If the server's context-path has been configured to anything other than `/`, the Cloud Foundry endpoints are not available at the root of the application.
For example, if `server.servlet.context-path=/app`, Cloud Foundry endpoints are available at `/app/cloudfoundryapplication/*`.
@ -47,8 +51,8 @@ If you expect the Cloud Foundry endpoints to always be available at `/cloudfound
The configuration differs, depending on the web server in use.
For Tomcat, you can add the following configuration:
include::code:MyCloudFoundryConfiguration[]
include-code::MyCloudFoundryConfiguration[]
If you're using a Webflux based application, you can use the following configuration:
include::code:MyReactiveCloudFoundryConfiguration[]
include-code::MyReactiveCloudFoundryConfiguration[]

View File

@ -1,6 +1,7 @@
[[actuator.enabling]]
== Enabling Production-ready Features
The {spring-boot-code}/spring-boot-project/spring-boot-actuator[`spring-boot-actuator`] module provides all of Spring Boot's production-ready features.
= Enabling Production-ready Features
The {code-spring-boot}/spring-boot-project/spring-boot-actuator[`spring-boot-actuator`] module provides all of Spring Boot's production-ready features.
The recommended way to enable the features is to add a dependency on the `spring-boot-starter-actuator` "`Starter`".
.Definition of Actuator
@ -11,21 +12,21 @@ Actuators can generate a large amount of motion from a small change.
To add the actuator to a Maven-based project, add the following "`Starter`" dependency:
[source,xml,indent=0,subs="verbatim"]
[source,xml]
----
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
----
For Gradle, use the following declaration:
[source,gradle,indent=0,subs="verbatim"]
[source,gradle]
----
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-actuator'
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-actuator'
}
----

View File

@ -1,5 +1,6 @@
[[actuator.http-exchanges]]
== Recording HTTP Exchanges
= Recording HTTP Exchanges
You can enable recording of HTTP exchanges by providing a bean of type `HttpExchangeRepository` in your application's configuration.
For convenience, Spring Boot offers `InMemoryHttpExchangeRepository`, which, by default, stores the last 100 request-response exchanges.
`InMemoryHttpExchangeRepository` is limited compared to tracing solutions, and we recommend using it only for development environments.
@ -11,7 +12,8 @@ You can use the `httpexchanges` endpoint to obtain information about the request
[[actuator.http-exchanges.custom]]
=== Custom HTTP Exchange Recording
== Custom HTTP Exchange Recording
To customize the items that are included in each recorded exchange, use the configprop:management.httpexchanges.recording.include[] configuration property.
To disable recording entirely, set configprop:management.httpexchanges.recording.enabled[] to `false`.
To disable recoding entirely, set configprop:management.httpexchanges.recording.enabled[] to `false`.

View File

@ -0,0 +1,8 @@
[[actuator]]
= Production-ready Features
Spring Boot includes a number of additional features to help you monitor and manage your application when you push it to production.
You can choose to manage and monitor your application by using HTTP endpoints or with JMX.
Auditing, health, and metrics gathering can also be automatically applied to your application.

View File

@ -1,5 +1,6 @@
[[actuator.jmx]]
== Monitoring and Management over JMX
= Monitoring and Management over JMX
Java Management Extensions (JMX) provide a standard mechanism to monitor and manage applications.
By default, this feature is not enabled.
You can turn it on by setting the configprop:spring.jmx.enabled[] configuration property to `true`.
@ -9,7 +10,7 @@ Any of your beans that are annotated with Spring JMX annotations (`@ManagedResou
If your platform provides a standard `MBeanServer`, Spring Boot uses that and defaults to the VM `MBeanServer`, if necessary.
If all that fails, a new `MBeanServer` is created.
See the {spring-boot-autoconfigure-module-code}/jmx/JmxAutoConfiguration.java[`JmxAutoConfiguration`] class for more details.
See the {code-spring-boot-autoconfigure-src}/jmx/JmxAutoConfiguration.java[`JmxAutoConfiguration`] class for more details.
By default, Spring Boot also exposes management endpoints as JMX MBeans under the `org.springframework.boot` domain.
To take full control over endpoint registration in the JMX domain, consider registering your own `EndpointObjectNameFactory` implementation.
@ -17,7 +18,8 @@ To take full control over endpoint registration in the JMX domain, consider regi
[[actuator.jmx.custom-mbean-names]]
=== Customizing MBean Names
== Customizing MBean Names
The name of the MBean is usually generated from the `id` of the endpoint.
For example, the `health` endpoint is exposed as `org.springframework.boot:type=Endpoint,name=Health`.
@ -27,28 +29,29 @@ To solve this problem, you can set the configprop:spring.jmx.unique-names[] prop
You can also customize the JMX domain under which endpoints are exposed.
The following settings show an example of doing so in `application.properties`:
[source,yaml,indent=0,subs="verbatim",configprops,configblocks]
[configprops,yaml]
----
spring:
jmx:
unique-names: true
management:
endpoints:
jmx:
domain: "com.example.myapp"
spring:
jmx:
unique-names: true
management:
endpoints:
jmx:
domain: "com.example.myapp"
----
[[actuator.jmx.disable-jmx-endpoints]]
=== Disabling JMX Endpoints
== Disabling JMX Endpoints
If you do not want to expose endpoints over JMX, you can set the configprop:management.endpoints.jmx.exposure.exclude[] property to `*`, as the following example shows:
[source,yaml,indent=0,subs="verbatim",configprops,configblocks]
[configprops,yaml]
----
management:
endpoints:
jmx:
exposure:
exclude: "*"
management:
endpoints:
jmx:
exposure:
exclude: "*"
----

View File

@ -1,5 +1,6 @@
[[actuator.loggers]]
== Loggers
= Loggers
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 configuration, which is made up of both the explicitly configured logging level as well as the effective logging level given to it by the logging framework.
These levels can be one of:
@ -18,14 +19,15 @@ These levels can be one of:
[[actuator.loggers.configure]]
=== Configure a Logger
== Configure a Logger
To configure a given logger, `POST` a partial entity to the resource's URI, as the following example shows:
[source,json,indent=0,subs="verbatim"]
[source,json]
----
{
"configuredLevel": "DEBUG"
}
{
"configuredLevel": "DEBUG"
}
----
TIP: To "`reset`" the specific level of the logger (and use the default configuration instead), you can pass a value of `null` as the `configuredLevel`.

View File

@ -1,5 +1,6 @@
[[actuator.monitoring]]
== Monitoring and Management Over HTTP
= Monitoring and Management Over HTTP
If you are developing a web application, Spring Boot Actuator auto-configures all enabled endpoints to be exposed over HTTP.
The default convention is to use the `id` of the endpoint with a prefix of `/actuator` as the URL path.
For example, `health` is exposed as `/actuator/health`.
@ -7,57 +8,59 @@ For example, `health` is exposed as `/actuator/health`.
TIP: Actuator is supported natively with Spring MVC, Spring WebFlux, and Jersey.
If both Jersey and Spring MVC are available, Spring MVC is used.
NOTE: Jackson is a required dependency in order to get the correct JSON responses as documented in the API documentation ({spring-boot-actuator-restapi-docs}[HTML] or {spring-boot-actuator-restapi-pdfdocs}[PDF]).
NOTE: Jackson is a required dependency in order to get the correct JSON responses as documented in the xref:api:rest/actuator/index.adoc[API documentation].
[[actuator.monitoring.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, your application might already use `/actuator` for another purpose.
You can use the configprop:management.endpoints.web.base-path[] property to change the prefix for your management endpoint, as the following example shows:
[source,yaml,indent=0,subs="verbatim",configprops,configblocks]
[configprops,yaml]
----
management:
endpoints:
web:
base-path: "/manage"
management:
endpoints:
web:
base-path: "/manage"
----
The preceding `application.properties` example changes the endpoint from `/actuator/\{id}` to `/manage/\{id}` (for example, `/manage/info`).
NOTE: Unless the management port has been configured to <<actuator#actuator.monitoring.customizing-management-server-port,expose endpoints by using a different HTTP port>>, `management.endpoints.web.base-path` is relative to `server.servlet.context-path` (for servlet web applications) or `spring.webflux.base-path` (for reactive web applications).
NOTE: Unless the management port has been configured to xref:actuator/monitoring.adoc#actuator.monitoring.customizing-management-server-port[expose endpoints by using a different HTTP port], `management.endpoints.web.base-path` is relative to `server.servlet.context-path` (for servlet web applications) or `spring.webflux.base-path` (for reactive web applications).
If `management.server.port` is configured, `management.endpoints.web.base-path` is relative to `management.server.base-path`.
If you want to map endpoints to a different path, you can use the configprop:management.endpoints.web.path-mapping[] property.
The following example remaps `/actuator/health` to `/healthcheck`:
[source,yaml,indent=0,subs="verbatim",configprops,configblocks]
[configprops,yaml]
----
management:
endpoints:
web:
base-path: "/"
path-mapping:
health: "healthcheck"
management:
endpoints:
web:
base-path: "/"
path-mapping:
health: "healthcheck"
----
[[actuator.monitoring.customizing-management-server-port]]
=== Customizing the Management Server Port
== Customizing the Management Server Port
Exposing management endpoints by using the default HTTP port is a sensible choice for 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 can set the configprop:management.server.port[] property to change the HTTP port, as the following example shows:
[source,yaml,indent=0,subs="verbatim",configprops,configblocks]
[configprops,yaml]
----
management:
server:
port: 8081
management:
server:
port: 8081
----
NOTE: On Cloud Foundry, by default, applications receive requests only on port 8080 for both HTTP and TCP routing.
@ -66,48 +69,50 @@ If you want to use a custom management port on Cloud Foundry, you need to explic
[[actuator.monitoring.management-specific-ssl]]
=== Configuring Management-specific SSL
== Configuring Management-specific SSL
When configured to use a custom port, you can also configure the management server with its own SSL by using the various `management.server.ssl.*` properties.
For example, doing so lets a management server be available over HTTP while the main application uses HTTPS, as the following property settings show:
[source,yaml,indent=0,subs="verbatim",configprops,configblocks]
[configprops,yaml]
----
server:
port: 8443
ssl:
enabled: true
key-store: "classpath:store.jks"
key-password: "secret"
management:
server:
port: 8080
ssl:
enabled: false
server:
port: 8443
ssl:
enabled: true
key-store: "classpath:store.jks"
key-password: "secret"
management:
server:
port: 8080
ssl:
enabled: false
----
Alternatively, both the main server and the management server can use SSL but with different key stores, as follows:
[source,yaml,indent=0,subs="verbatim",configprops,configblocks]
[configprops,yaml]
----
server:
port: 8443
ssl:
enabled: true
key-store: "classpath:main.jks"
key-password: "secret"
management:
server:
port: 8080
ssl:
enabled: true
key-store: "classpath:management.jks"
key-password: "secret"
server:
port: 8443
ssl:
enabled: true
key-store: "classpath:main.jks"
key-password: "secret"
management:
server:
port: 8080
ssl:
enabled: true
key-store: "classpath:management.jks"
key-password: "secret"
----
[[actuator.monitoring.customizing-management-server-address]]
=== Customizing the Management Server Address
== Customizing the Management Server Address
You can customize the address on which the management endpoints are available by setting the configprop:management.server.address[] property.
Doing so can be useful if you want to listen only on an internal or ops-facing network or to listen only for connections from `localhost`.
@ -115,34 +120,35 @@ NOTE: You can listen on a different address only when the port differs from the
The following example `application.properties` does not allow remote management connections:
[source,yaml,indent=0,subs="verbatim",configprops,configblocks]
[configprops,yaml]
----
management:
server:
port: 8081
address: "127.0.0.1"
management:
server:
port: 8081
address: "127.0.0.1"
----
[[actuator.monitoring.disabling-http-endpoints]]
=== Disabling HTTP Endpoints
== Disabling HTTP Endpoints
If you do not want to expose endpoints over HTTP, you can set the management port to `-1`, as the following example shows:
[source,yaml,indent=0,subs="verbatim",configprops,configblocks]
[configprops,yaml]
----
management:
server:
port: -1
management:
server:
port: -1
----
You can also achieve this by using the configprop:management.endpoints.web.exposure.exclude[] property, as the following example shows:
[source,yaml,indent=0,subs="verbatim",configprops,configblocks]
[configprops,yaml]
----
management:
endpoints:
web:
exposure:
exclude: "*"
management:
endpoints:
web:
exposure:
exclude: "*"
----

View File

@ -1,12 +1,13 @@
[[actuator.observability]]
== Observability
= Observability
Observability is the ability to observe the internal state of a running system from the outside.
It consists of the three pillars logging, metrics and traces.
For metrics and traces, Spring Boot uses https://micrometer.io/docs/observation[Micrometer Observation].
To create your own observations (which will lead to metrics and traces), you can inject an `ObservationRegistry`.
include::code:MyCustomObservation[]
include-code::MyCustomObservation[]
NOTE: Low cardinality tags will be added to metrics and traces, while high cardinality tags will only be added to traces.
@ -29,36 +30,39 @@ To enable it, add the `io.r2dbc:r2dbc-proxy` dependency to your project.
[[actuator.observability.common-tags]]
=== Common tags
== Common tags
Common tags are generally used for dimensional drill-down on the operating environment, such as host, instance, region, stack, and others.
Common tags are applied to all observations as low cardinality tags and can be configured, as the following example shows:
[source,yaml,indent=0,subs="verbatim",configprops,configblocks]
[configprops,yaml]
----
management:
observations:
key-values:
region: "us-east-1"
stack: "prod"
management:
observations:
key-values:
region: "us-east-1"
stack: "prod"
----
The preceding example adds `region` and `stack` tags to all observations with a value of `us-east-1` and `prod`, respectively.
[[actuator.observability.preventing-observations]]
=== Preventing Observations
== Preventing Observations
If you'd like to prevent some observations from being reported, you can use the configprop:management.observations.enable[] properties:
[source,yaml,indent=0,subs="verbatim",configprops,configblocks]
[configprops,yaml]
----
management:
observations:
enable:
denied:
prefix: false
another:
denied:
prefix: false
management:
observations:
enable:
denied:
prefix: false
another:
denied:
prefix: false
----
The preceding example will prevent all observations with a name starting with `denied.prefix` or `another.denied.prefix`.
@ -68,14 +72,15 @@ TIP: If you want to prevent Spring Security from reporting observations, set the
If you need greater control over the prevention of observations, you can register beans of type `ObservationPredicate`.
Observations are only reported if all the `ObservationPredicate` beans return `true` for that observation.
include::code:MyObservationPredicate[]
include-code::MyObservationPredicate[]
The preceding example will prevent all observations whose name contains "denied".
[[actuator.observability.opentelemetry]]
=== OpenTelemetry Support
== OpenTelemetry Support
Spring Boot's actuator module includes basic support for https://opentelemetry.io/[OpenTelemetry].
It provides a bean of type `OpenTelemetry`, and if there are beans of type `SdkTracerProvider`, `ContextPropagators`, `SdkLoggerProvider` or `SdkMeterProvider` in the application context, they automatically get registered.
@ -84,13 +89,14 @@ The attributes of the auto-configured `Resource` can be configured via the confi
If you have defined your own `Resource` bean, this will no longer be the case.
NOTE: Spring Boot does not provide auto-configuration for OpenTelemetry metrics or logging.
OpenTelemetry tracing is only auto-configured when used together with <<actuator#actuator.micrometer-tracing, Micrometer Tracing>>.
OpenTelemetry tracing is only auto-configured when used together with xref:actuator/tracing.adoc[Micrometer Tracing].
The next sections will provide more details about logging, metrics and traces.
[[actuator.observability.annotations]]
=== Micrometer Observation Annotations support
== Micrometer Observation Annotations support
To enable scanning of metrics and tracing annotations like `@Timed`, `@Counted`, `@MeterTag` and `@NewSpan` annotations, you will need to set the configprop:management.observations.annotations.enabled[] property to `true`.
This feature is supported Micrometer directly, please refer to the {micrometer-concepts-docs}#_the_timed_annotation[Micrometer] and {micrometer-tracing-docs}/api.html#_aspect_oriented_programming[Micrometer Tracing] reference docs.
This feature is supported Micrometer directly, please refer to the {url-micrometer-docs-concepts}#_the_timed_annotation[Micrometer] and {url-micrometer-tracing-docs}/api.html#_aspect_oriented_programming[Micrometer Tracing] reference docs.

View File

@ -1,5 +1,6 @@
[[actuator.process-monitoring]]
== Process Monitoring
= Process Monitoring
In the `spring-boot` module, you can find two classes to create files that are often useful for process monitoring:
* `ApplicationPidFileWriter` creates a file that contains the application PID (by default, in the application directory with a file name of `application.pid`).
@ -7,25 +8,27 @@ In the `spring-boot` module, you can find two classes to create files that are o
By default, these writers are not activated, but you can enable them:
* <<actuator#actuator.process-monitoring.configuration,By Extending Configuration>>
* <<actuator#actuator.process-monitoring.programmatically>>
* xref:actuator/process-monitoring.adoc#actuator.process-monitoring.configuration[By Extending Configuration]
* xref:actuator/process-monitoring.adoc#actuator.process-monitoring.programmatically[Programmatically Enabling Process Monitoring]
[[actuator.process-monitoring.configuration]]
=== Extending Configuration
== Extending Configuration
In the `META-INF/spring.factories` file, you can activate the listener (or listeners) that writes a PID file:
[indent=0]
[source]
----
org.springframework.context.ApplicationListener=\
org.springframework.boot.context.ApplicationPidFileWriter,\
org.springframework.boot.web.context.WebServerPortFileWriter
org.springframework.context.ApplicationListener=\
org.springframework.boot.context.ApplicationPidFileWriter,\
org.springframework.boot.web.context.WebServerPortFileWriter
----
[[actuator.process-monitoring.programmatically]]
=== Programmatically Enabling Process Monitoring
== Programmatically Enabling Process Monitoring
You can also activate a listener by invoking the `SpringApplication.addListeners(...)` method and passing the appropriate `Writer` object.
This method also lets you customize the file name and path in the `Writer` constructor.

View File

@ -1,5 +1,6 @@
[[actuator.micrometer-tracing]]
== Tracing
= Tracing
Spring Boot Actuator provides dependency management and auto-configuration for https://micrometer.io/docs/tracing[Micrometer Tracing], a facade for popular tracer libraries.
TIP: To learn more about Micrometer Tracing capabilities, see its https://micrometer.io/docs/tracing[reference documentation].
@ -7,7 +8,8 @@ TIP: To learn more about Micrometer Tracing capabilities, see its https://microm
[[actuator.micrometer-tracing.tracers]]
=== Supported Tracers
== Supported Tracers
Spring Boot ships auto-configuration for the following tracers:
* https://opentelemetry.io/[OpenTelemetry] with https://zipkin.io/[Zipkin], https://docs.wavefront.com/[Wavefront], or https://opentelemetry.io/docs/reference/specification/protocol/[OTLP]
@ -16,14 +18,15 @@ Spring Boot ships auto-configuration for the following tracers:
[[actuator.micrometer-tracing.getting-started]]
=== Getting Started
== Getting Started
We need an example application that we can use to get started with tracing.
For our purposes, the simple "`Hello World!`" web application that's covered in the "`<<getting-started#getting-started.first-application>>`" section will suffice.
For our purposes, the simple "`Hello World!`" web application that's covered in the "`xref:tutorial:first-application/index.adoc[Developing Your First Spring Boot Application]`" section will suffice.
We're going to use the OpenTelemetry tracer with Zipkin as trace backend.
To recap, our main application code looks like this:
include::code:MyApplication[]
include-code::MyApplication[]
NOTE: There's an added logger statement in the `home()` method, which will be important later.
@ -35,12 +38,12 @@ Now we have to add the following dependencies:
Add the following application properties:
[source,yaml,indent=0,subs="verbatim",configprops,configblocks]
[configprops,yaml]
----
management:
tracing:
sampling:
probability: 1.0
management:
tracing:
sampling:
probability: 1.0
----
By default, Spring Boot samples only 10% of requests to prevent overwhelming the trace backend.
@ -54,9 +57,9 @@ After Zipkin is running, you can start your application.
If you open a web browser to `http://localhost:8080`, you should see the following output:
[indent=0]
[source]
----
Hello World!
Hello World!
----
Behind the scenes, an observation has been created for the HTTP request, which in turn gets bridged to OpenTelemetry, which reports a new trace to Zipkin.
@ -68,7 +71,8 @@ Press the "Show" button to see the details of that trace.
[[actuator.micrometer-tracing.logging]]
=== Logging Correlation IDs
== Logging Correlation IDs
Correlation IDs provide a helpful way to link lines in your log files to spans/traces.
If you are using Micrometer Tracing, Spring Boot will include correlation IDs in your logs by default.
@ -78,12 +82,12 @@ For example, if Micrometer Tracing has added an MDC `traceId` of `803B448A0489F8
If you prefer to use a different format for your correlation ID, you can use the configprop:logging.pattern.correlation[] property to define one.
For example, the following will provide a correlation ID for Logback in format previously used by Spring Cloud Sleuth:
[source,yaml,indent=0,subs="verbatim",configprops,configblocks]
[configprops,yaml]
----
logging:
pattern:
correlation: "[${spring.application.name:},%X{traceId:-},%X{spanId:-}] "
include-application-name: false
logging:
pattern:
correlation: "[${spring.application.name:},%X{traceId:-},%X{spanId:-}] "
include-application-name: false
----
NOTE: In the example above, configprop:logging.include-application-name[] is set to `false` to avoid the application name being duplicated in the log messages (configprop:logging.pattern.correlation[] already contains it).
@ -92,15 +96,17 @@ It's also worth mentioning that configprop:logging.pattern.correlation[] contain
[[actuator.micrometer-tracing.propagating-traces]]
=== Propagating Traces
To automatically propagate traces over the network, use the auto-configured <<io#io.rest-client.resttemplate, `RestTemplateBuilder`>> or <<io#io.rest-client.webclient, `WebClient.Builder`>> to construct the client.
== Propagating Traces
To automatically propagate traces over the network, use the auto-configured xref:io/rest-client.adoc#io.rest-client.resttemplate[`RestTemplateBuilder`] or xref:io/rest-client.adoc#io.rest-client.webclient[`WebClient.Builder`] to construct the client.
WARNING: If you create the `WebClient` or the `RestTemplate` without using the auto-configured builders, automatic trace propagation won't work!
[[actuator.micrometer-tracing.tracer-implementations]]
=== Tracer Implementations
== Tracer Implementations
As Micrometer Tracer supports multiple tracer implementations, there are multiple dependency combinations possible with Spring Boot.
All tracer implementations need the `org.springframework.boot:spring-boot-starter-actuator` dependency.
@ -108,7 +114,8 @@ All tracer implementations need the `org.springframework.boot:spring-boot-starte
[[actuator.micrometer-tracing.tracer-implementations.otel-zipkin]]
==== OpenTelemetry With Zipkin
=== OpenTelemetry With Zipkin
Tracing with OpenTelemetry and reporting to Zipkin requires the following dependencies:
* `io.micrometer:micrometer-tracing-bridge-otel` - bridges the Micrometer Observation API to OpenTelemetry.
@ -119,7 +126,8 @@ Use the `management.zipkin.tracing.*` configuration properties to configure repo
[[actuator.micrometer-tracing.tracer-implementations.otel-wavefront]]
==== OpenTelemetry With Wavefront
=== OpenTelemetry With Wavefront
Tracing with OpenTelemetry and reporting to Wavefront requires the following dependencies:
* `io.micrometer:micrometer-tracing-bridge-otel` - bridges the Micrometer Observation API to OpenTelemetry.
@ -130,7 +138,8 @@ Use the `management.wavefront.*` configuration properties to configure reporting
[[actuator.micrometer-tracing.tracer-implementations.otel-otlp]]
==== OpenTelemetry With OTLP
=== OpenTelemetry With OTLP
Tracing with OpenTelemetry and reporting using OTLP requires the following dependencies:
* `io.micrometer:micrometer-tracing-bridge-otel` - bridges the Micrometer Observation API to OpenTelemetry.
@ -141,7 +150,8 @@ Use the `management.otlp.tracing.*` configuration properties to configure report
[[actuator.micrometer-tracing.tracer-implementations.brave-zipkin]]
==== OpenZipkin Brave With Zipkin
=== OpenZipkin Brave With Zipkin
Tracing with OpenZipkin Brave and reporting to Zipkin requires the following dependencies:
* `io.micrometer:micrometer-tracing-bridge-brave` - bridges the Micrometer Observation API to Brave.
@ -154,7 +164,8 @@ Use the `management.zipkin.tracing.*` configuration properties to configure repo
[[actuator.micrometer-tracing.tracer-implementations.brave-wavefront]]
==== OpenZipkin Brave With Wavefront
=== OpenZipkin Brave With Wavefront
Tracing with OpenZipkin Brave and reporting to Wavefront requires the following dependencies:
* `io.micrometer:micrometer-tracing-bridge-brave` - bridges the Micrometer Observation API to Brave.
@ -165,16 +176,19 @@ Use the `management.wavefront.*` configuration properties to configure reporting
[[actuator.micrometer-tracing.micrometer-observation]]
=== Integration with Micrometer Observation
== Integration with Micrometer Observation
A `TracingAwareMeterObservationHandler` is automatically registered on the `ObservationRegistry`, which creates spans for every completed observation.
[[actuator.micrometer-tracing.creating-spans]]
=== Creating Custom Spans
== Creating Custom Spans
You can create your own spans by starting an observation.
For this, inject `ObservationRegistry` into your component:
include::code:CustomObservation[]
include-code::CustomObservation[]
This will create an observation named "some-operation" with the tag "some-tag=some-value".
@ -183,10 +197,11 @@ TIP: If you want to create a span without creating a metric, you need to use the
[[actuator.micrometer-tracing.baggage]]
=== Baggage
== Baggage
You can create baggage with the `Tracer` API:
include::code:CreatingBaggage[]
include-code::CreatingBaggage[]
This example creates baggage named `baggage1` with the value `value1`.
The baggage is automatically propagated over the network if you're using W3C propagation.
@ -200,7 +215,7 @@ For the example above, setting this property to `baggage1` results in an MDC ent
[[actuator.micrometer-tracing.tests]]
=== Tests
== Tests
Tracing components which are reporting data are not auto-configured when using `@SpringBootTest`.
See <<features#features.testing.spring-boot-applications.tracing, the testing section>> for more details.
See xref:features/testing.adoc#features.testing.spring-boot-applications.tracing[the testing section] for more details.

View File

@ -1,5 +1,6 @@
[[container-images.buildpacks]]
== Cloud Native Buildpacks
= Cloud Native Buildpacks
Dockerfiles are just one way to build docker images.
Another way to build docker images is directly from your Maven or Gradle plugin, using buildpacks.
If youve ever used an application platform such as Cloud Foundry or Heroku then youve probably used a buildpack.
@ -10,7 +11,7 @@ With Cloud Native Buildpacks, you can create Docker compatible images that you c
Spring Boot includes buildpack support directly for both Maven and Gradle.
This means you can just type a single command and quickly get a sensible image into your locally running Docker daemon.
See the individual plugin documentation on how to use buildpacks with {spring-boot-maven-plugin-docs}#build-image[Maven] and {spring-boot-gradle-plugin-docs}#build-image[Gradle].
See the individual plugin documentation on how to use buildpacks with xref:maven-plugin:build-image.adoc#build-image[Maven] and xref:gradle-plugin:packaging-oci-image.adoc[Gradle].
NOTE: The https://github.com/paketo-buildpacks/spring-boot[Paketo Spring Boot buildpack] supports the `layers.idx` file, so any customization that is applied to it will be reflected in the image created by the buildpack.

View File

@ -1,15 +1,16 @@
[[container-images.dockerfiles]]
== Dockerfiles
While it is possible to convert a Spring Boot uber jar into a docker image with just a few lines in the Dockerfile, we will use the <<container-images#container-images.efficient-images.layering,layering feature>> to create an optimized docker image.
= Dockerfiles
While it is possible to convert a Spring Boot uber jar into a docker image with just a few lines in the Dockerfile, we will use the xref:container-images/efficient-images.adoc#container-images.efficient-images.layering[layering feature] to create an optimized docker image.
When you create a jar containing the layers index file, the `spring-boot-jarmode-layertools` jar will be added as a dependency to your jar.
With this jar on the classpath, you can launch your application in a special mode which allows the bootstrap code to run something entirely different from your application, for example, something that extracts the layers.
CAUTION: The `layertools` mode can not be used with a <<deployment#deployment.installing, fully executable Spring Boot archive>> that includes a launch script.
CAUTION: The `layertools` mode can not be used with a xref:deployment/installing.adoc[fully executable Spring Boot archive] that includes a launch script.
Disable launch script configuration when building a jar file that is intended to be used with `layertools`.
Heres how you can launch your jar with a `layertools` jar mode:
[source,shell,indent=0,subs="verbatim"]
[source,shell]
----
$ java -Djarmode=layertools -jar my-app.jar
----
@ -30,7 +31,7 @@ Available commands:
The `extract` command can be used to easily split the application into layers to be added to the dockerfile.
Here is an example of a Dockerfile using `jarmode`.
[source,dockerfile,indent=0,subs="verbatim"]
[source,dockerfile]
----
FROM eclipse-temurin:17-jre as builder
WORKDIR application
@ -49,9 +50,9 @@ ENTRYPOINT ["java", "org.springframework.boot.loader.launch.JarLauncher"]
Assuming the above `Dockerfile` is in the current directory, your docker image can be built with `docker build .`, or optionally specifying the path to your application jar, as shown in the following example:
[source,shell,indent=0,subs="verbatim"]
[source,shell]
----
$ docker build --build-arg JAR_FILE=path/to/myapp.jar .
$ docker build --build-arg JAR_FILE=path/to/myapp.jar .
----
This is a multi-stage dockerfile.

View File

@ -1,5 +1,6 @@
[[container-images.efficient-images]]
== Efficient Container Images
= Efficient Container Images
It is easily possible to package a Spring Boot uber jar as a docker image.
However, there are various downsides to copying and running the uber jar as is in the docker image.
Theres always a certain amount of overhead when running a uber jar without unpacking it, and in a containerized environment this can be noticeable.
@ -8,8 +9,10 @@ Since you probably recompile your code more often than you upgrade the version o
If you put jar files in the layer before your application classes, Docker often only needs to change the very bottom layer and can pick others up from its cache.
[[container-images.efficient-images.layering]]
=== Layering Docker Images
== Layering Docker Images
To make it easier to create optimized Docker images, Spring Boot supports adding a layer index file to the jar.
It provides a list of layers and the parts of the jar that should be contained within them.
The list of layers in the index is ordered based on the order in which the layers should be added to the Docker/OCI image.
@ -22,19 +25,19 @@ Out-of-the-box, the following layers are supported:
The following shows an example of a `layers.idx` file:
[source,yaml,indent=0,subs="verbatim"]
[source,yaml]
----
- "dependencies":
- BOOT-INF/lib/library1.jar
- BOOT-INF/lib/library2.jar
- "spring-boot-loader":
- org/springframework/boot/loader/launch/JarLauncher.class
- ... <other classes>
- "snapshot-dependencies":
- BOOT-INF/lib/library3-SNAPSHOT.jar
- "application":
- META-INF/MANIFEST.MF
- BOOT-INF/classes/a/b/C.class
- "dependencies":
- BOOT-INF/lib/library1.jar
- BOOT-INF/lib/library2.jar
- "spring-boot-loader":
- org/springframework/boot/loader/launch/JarLauncher.class
- ... <other classes>
- "snapshot-dependencies":
- BOOT-INF/lib/library3-SNAPSHOT.jar
- "application":
- META-INF/MANIFEST.MF
- BOOT-INF/classes/a/b/C.class
----
This layering is designed to separate code based on how likely it is to change between application builds.
@ -43,5 +46,5 @@ Application code is more likely to change between builds so it is isolated in a
Spring Boot also supports layering for war files with the help of a `layers.idx`.
For Maven, see the {spring-boot-maven-plugin-docs}#repackage-layers[packaging layered jar or war section] for more details on adding a layer index to the archive.
For Gradle, see the {spring-boot-gradle-plugin-docs}#packaging-layered-archives[packaging layered jar or war section] of the Gradle plugin documentation.
For Maven, see the xref:maven-plugin:packaging.adoc#packaging.layers[packaging layered jar or war section] for more details on adding a layer index to the archive.
For Gradle, see the xref:gradle-plugin:packaging.adoc#packaging-executable.configuring.layered-archives[packaging layered jar or war section] of the Gradle plugin documentation.

View File

@ -0,0 +1,4 @@
[[container-images]]
= Container Images
Spring Boot applications can be containerized xref:container-images/dockerfiles.adoc[using Dockerfiles], or by xref:container-images/cloud-native-buildpacks.adoc[using Cloud Native Buildpacks to create optimized docker compatible container images that you can run anywhere].

View File

@ -0,0 +1,4 @@
[[data]]
= Data
Spring Boot integrates with a number of data technologies, both SQL and NoSQL.

View File

@ -1,27 +1,29 @@
[[data.nosql]]
== Working with NoSQL Technologies
= Working with NoSQL Technologies
Spring Data provides additional projects that help you access a variety of NoSQL technologies, including:
* {spring-data-cassandra}[Cassandra]
* {spring-data-couchbase}[Couchbase]
* {spring-data-elasticsearch}[Elasticsearch]
* {spring-data-gemfire}[GemFire] or {spring-data-geode}[Geode]
* {spring-data-ldap}[LDAP]
* {spring-data-mongodb}[MongoDB]
* {spring-data-neo4j}[Neo4J]
* {spring-data-redis}[Redis]
* {url-spring-data-cassandra-site}[Cassandra]
* {url-spring-data-couchbase-site}[Couchbase]
* {url-spring-data-elasticsearch-site}[Elasticsearch]
* {url-spring-data-gemfire-site}[GemFire] or {url-spring-data-geode-site}[Geode]
* {url-spring-data-ldap-site}[LDAP]
* {url-spring-data-mongodb-site}[MongoDB]
* {url-spring-data-neo4j-site}[Neo4J]
* {url-spring-data-redis-site}[Redis]
Of these, Spring Boot provides auto-configuration for Cassandra, Couchbase, Elasticsearch, LDAP, MongoDB, Neo4J and Redis.
Additionally, {spring-boot-for-apache-geode}[Spring Boot for Apache Geode] provides {spring-boot-for-apache-geode-docs}#geode-repositories[auto-configuration for Apache Geode].
Additionally, {url-spring-boot-for-apache-geode-site}[Spring Boot for Apache Geode] provides {url-spring-boot-for-apache-geode-docs}#geode-repositories[auto-configuration for Apache Geode].
You can make use of the other projects, but you must configure them yourself.
See the appropriate reference documentation at {spring-data}.
See the appropriate reference documentation at {url-spring-data-site}.
Spring Boot also provides auto-configuration for the InfluxDB client but it is deprecated in favor of https://github.com/influxdata/influxdb-client-java[the new InfluxDB Java client] that provides its own Spring Boot integration.
[[data.nosql.redis]]
=== Redis
== Redis
https://redis.io/[Redis] is a cache, message broker, and richly-featured key-value store.
Spring Boot offers basic auto-configuration for the https://github.com/lettuce-io/lettuce-core/[Lettuce] and https://github.com/xetorthio/jedis/[Jedis] client libraries and the abstractions on top of them provided by https://github.com/spring-projects/spring-data-redis[Spring Data Redis].
@ -34,25 +36,26 @@ TIP: We also provide a `spring-boot-starter-data-redis-reactive` "`Starter`" for
[[data.nosql.redis.connecting]]
==== Connecting to Redis
=== Connecting to Redis
You can inject an auto-configured `RedisConnectionFactory`, `StringRedisTemplate`, or vanilla `RedisTemplate` instance as you would any other Spring Bean.
The following listing shows an example of such a bean:
include::code:MyBean[]
include-code::MyBean[]
By default, the instance tries to connect to a Redis server at `localhost:6379`.
You can specify custom connection details using `spring.data.redis.*` properties, as shown in the following example:
[source,yaml,indent=0,subs="verbatim",configprops,configblocks]
[configprops,yaml]
----
spring:
data:
redis:
host: "localhost"
port: 6379
database: 0
username: "user"
password: "secret"
spring:
data:
redis:
host: "localhost"
port: 6379
database: 0
username: "user"
password: "secret"
----
TIP: You can also register an arbitrary number of beans that implement `LettuceClientConfigurationBuilderCustomizer` for more advanced customizations.
@ -66,41 +69,44 @@ By default, a pooled connection factory is auto-configured if `commons-pool2` is
The auto-configured `RedisConnectionFactory` can be configured to use SSL for communication with the server by setting the properties as shown in this example:
[source,yaml,indent=0,subs="verbatim",configprops,configblocks]
[configprops,yaml]
----
spring:
data:
redis:
ssl:
enabled: true
spring:
data:
redis:
ssl:
enabled: true
----
Custom SSL trust material can be configured in an <<features#features.ssl,SSL bundle>> and applied to the `RedisConnectionFactory` as shown in this example:
Custom SSL trust material can be configured in an xref:features/ssl.adoc[SSL bundle] and applied to the `RedisConnectionFactory` as shown in this example:
[source,yaml,indent=0,subs="verbatim",configprops,configblocks]
[configprops,yaml]
----
spring:
data:
redis:
ssl:
bundle: "example"
spring:
data:
redis:
ssl:
bundle: "example"
----
[[data.nosql.mongodb]]
=== MongoDB
== MongoDB
https://www.mongodb.com/[MongoDB] is an open-source NoSQL document database that uses a JSON-like schema instead of traditional table-based relational data.
Spring Boot offers several conveniences for working with MongoDB, including the `spring-boot-starter-data-mongodb` and `spring-boot-starter-data-mongodb-reactive` "`Starters`".
[[data.nosql.mongodb.connecting]]
==== Connecting to a MongoDB Database
=== Connecting to a MongoDB Database
To access MongoDB databases, you can inject an auto-configured `org.springframework.data.mongodb.MongoDatabaseFactory`.
By default, the instance tries to connect to a MongoDB server at `mongodb://localhost/test`.
The following example shows how to connect to a MongoDB database:
include::code:MyBean[]
include-code::MyBean[]
If you have defined your own `MongoClient`, it will be used to auto-configure a suitable `MongoDatabaseFactory`.
@ -112,53 +118,53 @@ Each will be called in order with the `MongoClientSettings.Builder` that is used
You can set the configprop:spring.data.mongodb.uri[] property to change the URL and configure additional settings such as the _replica set_, as shown in the following example:
[source,yaml,indent=0,subs="verbatim",configprops,configblocks]
[configprops,yaml]
----
spring:
data:
mongodb:
uri: "mongodb://user:secret@mongoserver1.example.com:27017,mongoserver2.example.com:23456/test"
spring:
data:
mongodb:
uri: "mongodb://user:secret@mongoserver1.example.com:27017,mongoserver2.example.com:23456/test"
----
Alternatively, you can specify connection details using discrete properties.
For example, you might declare the following settings in your `application.properties`:
[source,yaml,indent=0,subs="verbatim",configprops,configblocks]
[configprops,yaml]
----
spring:
data:
mongodb:
host: "mongoserver1.example.com"
port: 27017
additional-hosts:
- "mongoserver2.example.com:23456"
database: "test"
username: "user"
password: "secret"
spring:
data:
mongodb:
host: "mongoserver1.example.com"
port: 27017
additional-hosts:
- "mongoserver2.example.com:23456"
database: "test"
username: "user"
password: "secret"
----
The auto-configured `MongoClient` can be configured to use SSL for communication with the server by setting the properties as shown in this example:
[source,yaml,indent=0,subs="verbatim",configprops,configblocks]
[configprops,yaml]
----
spring:
data:
mongodb:
uri: "mongodb://user:secret@mongoserver1.example.com:27017,mongoserver2.example.com:23456/test"
ssl:
enabled: true
spring:
data:
mongodb:
uri: "mongodb://user:secret@mongoserver1.example.com:27017,mongoserver2.example.com:23456/test"
ssl:
enabled: true
----
Custom SSL trust material can be configured in an <<features#features.ssl,SSL bundle>> and applied to the `MongoClient` as shown in this example:
Custom SSL trust material can be configured in an xref:features/ssl.adoc[SSL bundle] and applied to the `MongoClient` as shown in this example:
[source,yaml,indent=0,subs="verbatim",configprops,configblocks]
[configprops,yaml]
----
spring:
data:
mongodb:
uri: "mongodb://user:secret@mongoserver1.example.com:27017,mongoserver2.example.com:23456/test"
ssl:
bundle: "example"
spring:
data:
mongodb:
uri: "mongodb://user:secret@mongoserver1.example.com:27017,mongoserver2.example.com:23456/test"
ssl:
bundle: "example"
----
@ -180,60 +186,64 @@ The auto-configuration configures this factory automatically if Netty is availab
[[data.nosql.mongodb.template]]
==== MongoTemplate
{spring-data-mongodb}[Spring Data MongoDB] provides a {spring-data-mongodb-api}/core/MongoTemplate.html[`MongoTemplate`] class that is very similar in its design to Spring's `JdbcTemplate`.
=== MongoTemplate
{url-spring-data-mongodb-site}[Spring Data MongoDB] provides a {url-spring-data-mongodb-javadoc}/org/springframework/data/mongodb/core/MongoTemplate.html[`MongoTemplate`] class that is very similar in its design to Spring's `JdbcTemplate`.
As with `JdbcTemplate`, Spring Boot auto-configures a bean for you to inject the template, as follows:
include::code:MyBean[]
include-code::MyBean[]
See the {spring-data-mongodb-api}/core/MongoOperations.html[`MongoOperations` Javadoc] for complete details.
See the {url-spring-data-mongodb-javadoc}/org/springframework/data/mongodb/core/MongoOperations.html[`MongoOperations` Javadoc] for complete details.
[[data.nosql.mongodb.repositories]]
==== Spring Data MongoDB Repositories
=== Spring Data MongoDB Repositories
Spring Data includes repository support for MongoDB.
As with the JPA repositories discussed earlier, the basic principle is that queries are constructed automatically, based on method names.
In fact, both Spring Data JPA and Spring Data MongoDB share the same common infrastructure.
You could take the JPA example from earlier and, assuming that `City` is now a MongoDB data class rather than a JPA `@Entity`, it works in the same way, as shown in the following example:
include::code:CityRepository[]
include-code::CityRepository[]
Repositories and documents are found through scanning.
By default, the <<using#using.auto-configuration.packages,auto-configuration packages>> are scanned.
By default, the xref:using/auto-configuration.adoc#using.auto-configuration.packages[auto-configuration packages] are scanned.
You can customize the locations to look for repositories and documents by using `@EnableMongoRepositories` and `@EntityScan` respectively.
TIP: For complete details of Spring Data MongoDB, including its rich object mapping technologies, see its {spring-data-mongodb}[reference documentation].
TIP: For complete details of Spring Data MongoDB, including its rich object mapping technologies, see its {url-spring-data-mongodb-docs}[reference documentation].
[[data.nosql.neo4j]]
=== Neo4j
== Neo4j
https://neo4j.com/[Neo4j] is an open-source NoSQL graph database that uses a rich data model of nodes connected by first class relationships, which is better suited for connected big data than traditional RDBMS approaches.
Spring Boot offers several conveniences for working with Neo4j, including the `spring-boot-starter-data-neo4j` "`Starter`".
[[data.nosql.neo4j.connecting]]
==== Connecting to a Neo4j Database
=== Connecting to a Neo4j Database
To access a Neo4j server, you can inject an auto-configured `org.neo4j.driver.Driver`.
By default, the instance tries to connect to a Neo4j server at `localhost:7687` using the Bolt protocol.
The following example shows how to inject a Neo4j `Driver` that gives you access, amongst other things, to a `Session`:
include::code:MyBean[]
include-code::MyBean[]
You can configure various aspects of the driver using `spring.neo4j.*` properties.
The following example shows how to configure the uri and credentials to use:
[source,yaml,indent=0,subs="verbatim",configprops,configblocks]
[configprops,yaml]
----
spring:
neo4j:
uri: "bolt://my-server:7687"
authentication:
username: "neo4j"
password: "secret"
spring:
neo4j:
uri: "bolt://my-server:7687"
authentication:
username: "neo4j"
password: "secret"
----
The auto-configured `Driver` is created using `ConfigBuilder`.
@ -243,21 +253,22 @@ Each will be called in order with the `ConfigBuilder` that is used to build the
[[data.nosql.neo4j.repositories]]
==== Spring Data Neo4j Repositories
=== Spring Data Neo4j Repositories
Spring Data includes repository support for Neo4j.
For complete details of Spring Data Neo4j, see the {spring-data-neo4j-docs}[reference documentation].
For complete details of Spring Data Neo4j, see the {url-spring-data-neo4j-docs}[reference documentation].
Spring Data Neo4j shares the common infrastructure with Spring Data JPA as many other Spring Data modules do.
You could take the JPA example from earlier and define `City` as Spring Data Neo4j `@Node` rather than JPA `@Entity` and the repository abstraction works in the same way, as shown in the following example:
include::code:CityRepository[]
include-code::CityRepository[]
The `spring-boot-starter-data-neo4j` "`Starter`" enables the repository support as well as transaction management.
Spring Boot supports both classic and reactive Neo4j repositories, using the `Neo4jTemplate` or `ReactiveNeo4jTemplate` beans.
When Project Reactor is available on the classpath, the reactive style is also auto-configured.
Repositories and entities are found through scanning.
By default, the <<using#using.auto-configuration.packages,auto-configuration packages>> are scanned.
By default, the xref:using/auto-configuration.adoc#using.auto-configuration.packages[auto-configuration packages] are scanned.
You can customize the locations to look for repositories and entities by using `@EnableNeo4jRepositories` and `@EntityScan` respectively.
[NOTE]
@ -265,13 +276,14 @@ You can customize the locations to look for repositories and entities by using `
In an application using the reactive style, a `ReactiveTransactionManager` is not auto-configured.
To enable transaction management, the following bean must be defined in your configuration:
include::code:MyNeo4jConfiguration[]
include-code::MyNeo4jConfiguration[]
====
[[data.nosql.elasticsearch]]
=== Elasticsearch
== Elasticsearch
https://www.elastic.co/products/elasticsearch[Elasticsearch] is an open source, distributed, RESTful search and analytics engine.
Spring Boot offers basic auto-configuration for Elasticsearch clients.
@ -286,24 +298,28 @@ Spring Boot provides a dedicated "`Starter`", `spring-boot-starter-data-elastics
[[data.nosql.elasticsearch.connecting-using-rest]]
==== Connecting to Elasticsearch Using REST clients
=== Connecting to Elasticsearch Using REST clients
Elasticsearch ships two different REST clients that you can use to query a cluster: the https://www.elastic.co/guide/en/elasticsearch/client/java-api-client/current/java-rest-low.html[low-level client] from the `org.elasticsearch.client:elasticsearch-rest-client` module and the https://www.elastic.co/guide/en/elasticsearch/client/java-api-client/current/index.html[Java API client] from the `co.elastic.clients:elasticsearch-java` module.
Additionally, Spring Boot provides support for a reactive client from the `org.springframework.data:spring-data-elasticsearch` module.
By default, the clients will target `http://localhost:9200`.
You can use `spring.elasticsearch.*` properties to further tune how the clients are configured, as shown in the following example:
[source,yaml,indent=0,subs="verbatim",configprops,configblocks]
[configprops,yaml]
----
spring:
elasticsearch:
uris: "https://search.example.com:9200"
socket-timeout: "10s"
username: "user"
password: "secret"
spring:
elasticsearch:
uris: "https://search.example.com:9200"
socket-timeout: "10s"
username: "user"
password: "secret"
----
[[data.nosql.elasticsearch.connecting-using-rest.restclient]]
===== Connecting to Elasticsearch Using RestClient
==== Connecting to Elasticsearch Using RestClient
If you have `elasticsearch-rest-client` on the classpath, Spring Boot will auto-configure and register a `RestClient` bean.
In addition to the properties described previously, to fine-tune the `RestClient` you can register an arbitrary number of beans that implement `RestClientBuilderCustomizer` for more advanced customizations.
To take full control over the clients' configuration, define a `RestClientBuilder` bean.
@ -313,19 +329,21 @@ To take full control over the clients' configuration, define a `RestClientBuilde
Additionally, if `elasticsearch-rest-client-sniffer` is on the classpath, a `Sniffer` is auto-configured to automatically discover nodes from a running Elasticsearch cluster and set them on the `RestClient` bean.
You can further tune how `Sniffer` is configured, as shown in the following example:
[source,yaml,indent=0,subs="verbatim",configprops,configblocks]
[configprops,yaml]
----
spring:
elasticsearch:
restclient:
sniffer:
interval: "10m"
delay-after-failure: "30s"
spring:
elasticsearch:
restclient:
sniffer:
interval: "10m"
delay-after-failure: "30s"
----
[[data.nosql.elasticsearch.connecting-using-rest.javaapiclient]]
===== Connecting to Elasticsearch Using ElasticsearchClient
==== Connecting to Elasticsearch Using ElasticsearchClient
If you have `co.elastic.clients:elasticsearch-java` on the classpath, Spring Boot will auto-configure and register an `ElasticsearchClient` bean.
The `ElasticsearchClient` uses a transport that depends upon the previously described `RestClient`.
@ -335,8 +353,9 @@ Furthermore, you can define a `RestClientOptions` bean to take further control o
[[data.nosql.elasticsearch.connecting-using-rest.reactiveclient]]
===== Connecting to Elasticsearch using ReactiveElasticsearchClient
{spring-data-elasticsearch}[Spring Data Elasticsearch] ships `ReactiveElasticsearchClient` for querying Elasticsearch instances in a reactive fashion.
==== Connecting to Elasticsearch using ReactiveElasticsearchClient
{url-spring-data-elasticsearch-site}[Spring Data Elasticsearch] ships `ReactiveElasticsearchClient` for querying Elasticsearch instances in a reactive fashion.
If you have Spring Data Elasticsearch and Reactor on the classpath, Spring Boot will auto-configure and register a `ReactiveElasticsearchClient`.
The `ReactiveElasticsearchclient` uses a transport that depends upon the previously described `RestClient`.
@ -346,22 +365,24 @@ Furthermore, you can define a `RestClientOptions` bean to take further control o
[[data.nosql.elasticsearch.connecting-using-spring-data]]
==== Connecting to Elasticsearch by Using Spring Data
=== Connecting to Elasticsearch by Using Spring Data
To connect to Elasticsearch, an `ElasticsearchClient` bean must be defined,
auto-configured by Spring Boot or manually provided by the application (see previous sections).
With this configuration in place, an
`ElasticsearchTemplate` can be injected like any other Spring bean,
as shown in the following example:
include::code:MyBean[]
include-code::MyBean[]
In the presence of `spring-data-elasticsearch` and Reactor, Spring Boot can also auto-configure a <<features#data.nosql.elasticsearch.connecting-using-rest.reactiveclient,ReactiveElasticsearchClient>> and a `ReactiveElasticsearchTemplate` as beans.
In the presence of `spring-data-elasticsearch` and Reactor, Spring Boot can also auto-configure a xref:data/nosql.adoc#data.nosql.elasticsearch.connecting-using-rest.reactiveclient[ReactiveElasticsearchClient] and a `ReactiveElasticsearchTemplate` as beans.
They are the reactive equivalent of the other REST clients.
[[data.nosql.elasticsearch.repositories]]
==== Spring Data Elasticsearch Repositories
=== Spring Data Elasticsearch Repositories
Spring Data includes repository support for Elasticsearch.
As with the JPA repositories discussed earlier, the basic principle is that queries are constructed for you automatically based on method names.
@ -369,10 +390,10 @@ In fact, both Spring Data JPA and Spring Data Elasticsearch share the same commo
You could take the JPA example from earlier and, assuming that `City` is now an Elasticsearch `@Document` class rather than a JPA `@Entity`, it works in the same way.
Repositories and documents are found through scanning.
By default, the <<using#using.auto-configuration.packages,auto-configuration packages>> are scanned.
By default, the xref:using/auto-configuration.adoc#using.auto-configuration.packages[auto-configuration packages] are scanned.
You can customize the locations to look for repositories and documents by using `@EnableElasticsearchRepositories` and `@EntityScan` respectively.
TIP: For complete details of Spring Data Elasticsearch, see the {spring-data-elasticsearch-docs}[reference documentation].
TIP: For complete details of Spring Data Elasticsearch, see the {url-spring-data-elasticsearch-docs}[reference documentation].
Spring Boot supports both classic and reactive Elasticsearch repositories, using the `ElasticsearchRestTemplate` or `ReactiveElasticsearchTemplate` beans.
Most likely those beans are auto-configured by Spring Boot given the required dependencies are present.
@ -382,7 +403,7 @@ Same applies to `ReactiveElasticsearchTemplate` and `ReactiveElasticsearchOperat
You can choose to disable the repositories support with the following property:
[source,yaml,indent=0,subs="verbatim",configprops,configblocks]
[configprops,yaml]
----
spring:
data:
@ -394,37 +415,39 @@ You can choose to disable the repositories support with the following property:
[[data.nosql.cassandra]]
=== Cassandra
== Cassandra
https://cassandra.apache.org/[Cassandra] is an open source, distributed database management system designed to handle large amounts of data across many commodity servers.
Spring Boot offers auto-configuration for Cassandra and the abstractions on top of it provided by https://github.com/spring-projects/spring-data-cassandra[Spring Data Cassandra].
Spring Boot offers auto-configuration for Cassandra and the abstractions on top of it provided by {url-spring-data-cassandra-site}[Spring Data Cassandra].
There is a `spring-boot-starter-data-cassandra` "`Starter`" for collecting the dependencies in a convenient way.
[[data.nosql.cassandra.connecting]]
==== Connecting to Cassandra
=== Connecting to Cassandra
You can inject an auto-configured `CassandraTemplate` or a Cassandra `CqlSession` instance as you would with any other Spring Bean.
The `spring.cassandra.*` properties can be used to customize the connection.
Generally, you provide `keyspace-name` and `contact-points` as well the local datacenter name, as shown in the following example:
[source,yaml,indent=0,subs="verbatim",configprops,configblocks]
[configprops,yaml]
----
spring:
cassandra:
keyspace-name: "mykeyspace"
contact-points: "cassandrahost1:9042,cassandrahost2:9042"
local-datacenter: "datacenter1"
spring:
cassandra:
keyspace-name: "mykeyspace"
contact-points: "cassandrahost1:9042,cassandrahost2:9042"
local-datacenter: "datacenter1"
----
If the port is the same for all your contact points you can use a shortcut and only specify the host names, as shown in the following example:
[source,yaml,indent=0,subs="verbatim",configprops,configblocks]
[configprops,yaml]
----
spring:
cassandra:
keyspace-name: "mykeyspace"
contact-points: "cassandrahost1,cassandrahost2"
local-datacenter: "datacenter1"
spring:
cassandra:
keyspace-name: "mykeyspace"
contact-points: "cassandrahost1,cassandrahost2"
local-datacenter: "datacenter1"
----
TIP: Those two examples are identical as the port default to `9042`.
@ -432,28 +455,28 @@ If you need to configure the port, use `spring.cassandra.port`.
The auto-configured `CqlSession` can be configured to use SSL for communication with the server by setting the properties as shown in this example:
[source,yaml,indent=0,subs="verbatim",configprops,configblocks]
[configprops,yaml]
----
spring:
cassandra:
keyspace-name: "mykeyspace"
contact-points: "cassandrahost1,cassandrahost2"
local-datacenter: "datacenter1"
ssl:
enabled: true
spring:
cassandra:
keyspace-name: "mykeyspace"
contact-points: "cassandrahost1,cassandrahost2"
local-datacenter: "datacenter1"
ssl:
enabled: true
----
Custom SSL trust material can be configured in an <<features#features.ssl,SSL bundle>> and applied to the `CqlSession` as shown in this example:
Custom SSL trust material can be configured in an xref:features/ssl.adoc[SSL bundle] and applied to the `CqlSession` as shown in this example:
[source,yaml,indent=0,subs="verbatim",configprops,configblocks]
[configprops,yaml]
----
spring:
cassandra:
keyspace-name: "mykeyspace"
contact-points: "cassandrahost1,cassandrahost2"
local-datacenter: "datacenter1"
ssl:
bundle: "example"
spring:
cassandra:
keyspace-name: "mykeyspace"
contact-points: "cassandrahost1,cassandrahost2"
local-datacenter: "datacenter1"
ssl:
bundle: "example"
----
@ -472,19 +495,20 @@ NOTE: If you use `CqlSessionBuilder` to create multiple `CqlSession` beans, keep
The following code listing shows how to inject a Cassandra bean:
include::code:MyBean[]
include-code::MyBean[]
If you add your own `@Bean` of type `CassandraTemplate`, it replaces the default.
[[data.nosql.cassandra.repositories]]
==== Spring Data Cassandra Repositories
=== Spring Data Cassandra Repositories
Spring Data includes basic repository support for Cassandra.
Currently, this is more limited than the JPA repositories discussed earlier and needs `@Query` annotated finder methods.
Repositories and entities are found through scanning.
By default, the <<using#using.auto-configuration.packages,auto-configuration packages>> are scanned.
By default, the xref:using/auto-configuration.adoc#using.auto-configuration.packages[auto-configuration packages] are scanned.
You can customize the locations to look for repositories and entities by using `@EnableCassandraRepositories` and `@EntityScan` respectively.
TIP: For complete details of Spring Data Cassandra, see the https://docs.spring.io/spring-data/cassandra/docs/[reference documentation].
@ -492,7 +516,8 @@ TIP: For complete details of Spring Data Cassandra, see the https://docs.spring.
[[data.nosql.couchbase]]
=== Couchbase
== Couchbase
https://www.couchbase.com/[Couchbase] is an open-source, distributed, multi-model NoSQL document-oriented database that is optimized for interactive applications.
Spring Boot offers auto-configuration for Couchbase and the abstractions on top of it provided by https://github.com/spring-projects/spring-data-couchbase[Spring Data Couchbase].
There are `spring-boot-starter-data-couchbase` and `spring-boot-starter-data-couchbase-reactive` "`Starters`" for collecting the dependencies in a convenient way.
@ -500,32 +525,33 @@ There are `spring-boot-starter-data-couchbase` and `spring-boot-starter-data-cou
[[data.nosql.couchbase.connecting]]
==== Connecting to Couchbase
=== Connecting to Couchbase
You can get a `Cluster` by adding the Couchbase SDK and some configuration.
The `spring.couchbase.*` properties can be used to customize the connection.
Generally, you provide the https://github.com/couchbaselabs/sdk-rfcs/blob/master/rfc/0011-connection-string.md[connection string], username, and password, as shown in the following example:
[source,yaml,indent=0,subs="verbatim",configprops,configblocks]
[configprops,yaml]
----
spring:
couchbase:
connection-string: "couchbase://192.168.1.123"
username: "user"
password: "secret"
spring:
couchbase:
connection-string: "couchbase://192.168.1.123"
username: "user"
password: "secret"
----
It is also possible to customize some of the `ClusterEnvironment` settings.
For instance, the following configuration changes the timeout to open a new `Bucket` and enables SSL support with a reference to a configured <<features#features.ssl,SSL bundle>>:
For instance, the following configuration changes the timeout to open a new `Bucket` and enables SSL support with a reference to a configured xref:features/ssl.adoc[SSL bundle]:
[source,yaml,indent=0,subs="verbatim",configprops,configblocks]
[configprops,yaml]
----
spring:
couchbase:
env:
timeouts:
connect: "3s"
ssl:
bundle: "example"
spring:
couchbase:
env:
timeouts:
connect: "3s"
ssl:
bundle: "example"
----
TIP: Check the `spring.couchbase.env.*` properties for more details.
@ -534,29 +560,30 @@ To take more control, one or more `ClusterEnvironmentBuilderCustomizer` beans ca
[[data.nosql.couchbase.repositories]]
==== Spring Data Couchbase Repositories
=== Spring Data Couchbase Repositories
Spring Data includes repository support for Couchbase.
Repositories and documents are found through scanning.
By default, the <<using#using.auto-configuration.packages,auto-configuration packages>> are scanned.
By default, the xref:using/auto-configuration.adoc#using.auto-configuration.packages[auto-configuration packages] are scanned.
You can customize the locations to look for repositories and documents by using `@EnableCouchbaseRepositories` and `@EntityScan` respectively.
For complete details of Spring Data Couchbase, see the {spring-data-couchbase-docs}[reference documentation].
For complete details of Spring Data Couchbase, see the {url-spring-data-couchbase-docs}[reference documentation].
You can inject an auto-configured `CouchbaseTemplate` instance as you would with any other Spring Bean, provided a `CouchbaseClientFactory` bean is available.
This happens when a `Cluster` is available, as described above, and a bucket name has been specified:
[source,yaml,indent=0,subs="verbatim",configprops,configblocks]
[configprops,yaml]
----
spring:
data:
couchbase:
bucket-name: "my-bucket"
spring:
data:
couchbase:
bucket-name: "my-bucket"
----
The following examples shows how to inject a `CouchbaseTemplate` bean:
include::code:MyBean[]
include-code::MyBean[]
There are a few beans that you can define in your own configuration to override those provided by the auto-configuration:
@ -567,12 +594,13 @@ There are a few beans that you can define in your own configuration to override
To avoid hard-coding those names in your own config, you can reuse `BeanNames` provided by Spring Data Couchbase.
For instance, you can customize the converters to use, as follows:
include::code:MyCouchbaseConfiguration[]
include-code::MyCouchbaseConfiguration[]
[[data.nosql.ldap]]
=== LDAP
== LDAP
https://en.wikipedia.org/wiki/Lightweight_Directory_Access_Protocol[LDAP] (Lightweight Directory Access Protocol) is an open, vendor-neutral, industry standard application protocol for accessing and maintaining distributed directory information services over an IP network.
Spring Boot offers auto-configuration for any compliant LDAP server as well as support for the embedded in-memory LDAP server from https://ldap.com/unboundid-ldap-sdk-for-java/[UnboundID].
@ -582,16 +610,17 @@ There is a `spring-boot-starter-data-ldap` "`Starter`" for collecting the depend
[[data.nosql.ldap.connecting]]
==== Connecting to an LDAP Server
=== Connecting to an LDAP Server
To connect to an LDAP server, make sure you declare a dependency on the `spring-boot-starter-data-ldap` "`Starter`" or `spring-ldap-core` and then declare the URLs of your server in your application.properties, as shown in the following example:
[source,yaml,indent=0,subs="verbatim",configprops,configblocks]
[configprops,yaml]
----
spring:
ldap:
urls: "ldap://myserver:1235"
username: "admin"
password: "secret"
spring:
ldap:
urls: "ldap://myserver:1235"
username: "admin"
password: "secret"
----
If you need to customize connection settings, you can use the `spring.ldap.base` and `spring.ldap.base-environment` properties.
@ -604,11 +633,12 @@ Make sure to flag your customized `ContextSource` as `@Primary` so that the auto
[[data.nosql.ldap.repositories]]
==== Spring Data LDAP Repositories
=== Spring Data LDAP Repositories
Spring Data includes repository support for LDAP.
Repositories and documents are found through scanning.
By default, the <<using#using.auto-configuration.packages,auto-configuration packages>> are scanned.
By default, the xref:using/auto-configuration.adoc#using.auto-configuration.packages[auto-configuration packages] are scanned.
You can customize the locations to look for repositories and documents by using `@EnableLdapRepositories` and `@EntityScan` respectively.
For complete details of Spring Data LDAP, see the https://docs.spring.io/spring-data/ldap/docs/1.0.x/reference/html/[reference documentation].
@ -616,21 +646,22 @@ For complete details of Spring Data LDAP, see the https://docs.spring.io/spring-
You can also inject an auto-configured `LdapTemplate` instance as you would with any other Spring Bean, as shown in the following example:
include::code:MyBean[]
include-code::MyBean[]
[[data.nosql.ldap.embedded]]
==== Embedded In-memory LDAP Server
=== Embedded In-memory LDAP Server
For testing purposes, Spring Boot supports auto-configuration of an in-memory LDAP server from https://ldap.com/unboundid-ldap-sdk-for-java/[UnboundID].
To configure the server, add a dependency to `com.unboundid:unboundid-ldapsdk` and declare a configprop:spring.ldap.embedded.base-dn[] property, as follows:
[source,yaml,indent=0,subs="verbatim",configprops,configblocks]
[configprops,yaml]
----
spring:
ldap:
embedded:
base-dn: "dc=spring,dc=io"
spring:
ldap:
embedded:
base-dn: "dc=spring,dc=io"
----
[NOTE]
@ -639,11 +670,11 @@ It is possible to define multiple base-dn values, however, since distinguished n
In yaml files, you can use the yaml list notation. In properties files, you must include the index as part of the property name:
[source,yaml,indent=0,subs="verbatim",configprops,configblocks]
[configprops,yaml]
----
spring.ldap.embedded.base-dn:
- "dc=spring,dc=io"
- "dc=vmware,dc=com"
spring.ldap.embedded.base-dn:
- "dc=spring,dc=io"
- "dc=vmware,dc=com"
----
====
@ -660,7 +691,8 @@ If you have custom attributes, you can use configprop:spring.ldap.embedded.valid
[[data.nosql.influxdb]]
=== InfluxDB
== InfluxDB
WARNING: Auto-configuration for InfluxDB is deprecated and scheduled for removal in Spring Boot 3.4 in favor of https://github.com/influxdata/influxdb-client-java[the new InfluxDB Java client] that provides its own Spring Boot integration.
https://www.influxdata.com/[InfluxDB] is an open-source time series database optimized for fast, high-availability storage and retrieval of time series data in fields such as operations monitoring, application metrics, Internet-of-Things sensor data, and real-time analytics.
@ -668,7 +700,8 @@ https://www.influxdata.com/[InfluxDB] is an open-source time series database opt
[[data.nosql.influxdb.connecting]]
==== Connecting to InfluxDB
=== Connecting to InfluxDB
Spring Boot auto-configures an `InfluxDB` instance, provided the `influxdb-java` client is on the classpath and the URL of the database is set using configprop:spring.influx.url[deprecated].
If the connection to InfluxDB requires a user and password, you can set the configprop:spring.influx.user[deprecated] and configprop:spring.influx.password[deprecated] properties accordingly.

View File

@ -1,26 +1,29 @@
[[data.sql]]
== SQL Databases
The {spring-framework}[Spring Framework] provides extensive support for working with SQL databases, from direct JDBC access using `JdbcClient` or `JdbcTemplate` to complete "`object relational mapping`" technologies such as Hibernate.
{spring-data}[Spring Data] provides an additional level of functionality: creating `Repository` implementations directly from interfaces and using conventions to generate queries from your method names.
= SQL Databases
The {url-spring-framework-site}[Spring Framework] provides extensive support for working with SQL databases, from direct JDBC access using `JdbcClient` or `JdbcTemplate` to complete "`object relational mapping`" technologies such as Hibernate.
{url-spring-data-site}[Spring Data] provides an additional level of functionality: creating `Repository` implementations directly from interfaces and using conventions to generate queries from your method names.
[[data.sql.datasource]]
=== Configure a DataSource
== Configure a DataSource
Java's `javax.sql.DataSource` interface provides a standard method of working with database connections.
Traditionally, a `DataSource` uses a `URL` along with some credentials to establish a database connection.
TIP: See <<howto#howto.data-access.configure-custom-datasource,the "`How-to`" section>> for more advanced examples, typically to take full control over the configuration of the DataSource.
TIP: See xref:how-to:data-access.adoc#howto.data-access.configure-custom-datasource[the "`How-to`" section] for more advanced examples, typically to take full control over the configuration of the DataSource.
[[data.sql.datasource.embedded]]
==== Embedded Database Support
=== Embedded Database Support
It is often convenient to develop applications by using an in-memory embedded database.
Obviously, in-memory databases do not provide persistent storage.
You need to populate your database when your application starts and be prepared to throw away data when your application ends.
TIP: The "`How-to`" section includes a <<howto#howto.data-initialization, section on how to initialize a database>>.
TIP: The "`How-to`" section includes a xref:how-to:data-initialization.adoc[section on how to initialize a database].
Spring Boot can auto-configure embedded https://www.h2database.com[H2], https://hsqldb.org/[HSQL], and https://db.apache.org/derby/[Derby] databases.
You need not provide any connection URLs.
@ -36,17 +39,17 @@ If you want to make sure that each context has a separate embedded database, you
For example, the typical POM dependencies would be as follows:
[source,xml,indent=0,subs="verbatim"]
[source,xml]
----
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<scope>runtime</scope>
</dependency>
----
NOTE: You need a dependency on `spring-jdbc` for an embedded database to be auto-configured.
@ -60,23 +63,25 @@ Disabling the database's automatic shutdown lets Spring Boot control when the da
[[data.sql.datasource.production]]
==== Connection to a Production Database
=== Connection to a Production Database
Production database connections can also be auto-configured by using a pooling `DataSource`.
[[data.sql.datasource.configuration]]
==== DataSource Configuration
=== DataSource Configuration
DataSource configuration is controlled by external configuration properties in `+spring.datasource.*+`.
For example, you might declare the following section in `application.properties`:
[source,yaml,indent=0,subs="verbatim",configprops,configblocks]
[configprops,yaml]
----
spring:
datasource:
url: "jdbc:mysql://localhost/test"
username: "dbuser"
password: "dbpass"
spring:
datasource:
url: "jdbc:mysql://localhost/test"
username: "dbuser"
password: "dbpass"
----
NOTE: You should at least specify the URL by setting the configprop:spring.datasource.url[] property.
@ -88,21 +93,21 @@ If you need to specify a specific class, you can use the configprop:spring.datas
NOTE: For a pooling `DataSource` to be created, we need to be able to verify that a valid `Driver` class is available, so we check for that before doing anything.
In other words, if you set `spring.datasource.driver-class-name=com.mysql.jdbc.Driver`, then that class has to be loadable.
See {spring-boot-autoconfigure-module-code}/jdbc/DataSourceProperties.java[`DataSourceProperties`] for more of the supported options.
These are the standard options that work regardless of <<features#data.sql.datasource.connection-pool, the actual implementation>>.
See {code-spring-boot-autoconfigure-src}/jdbc/DataSourceProperties.java[`DataSourceProperties`] for more of the supported options.
These are the standard options that work regardless of xref:data/sql.adoc#data.sql.datasource.connection-pool[the actual implementation].
It is also possible to fine-tune implementation-specific settings by using their respective prefix (`+spring.datasource.hikari.*+`, `+spring.datasource.tomcat.*+`, `+spring.datasource.dbcp2.*+`, and `+spring.datasource.oracleucp.*+`).
See the documentation of the connection pool implementation you are using for more details.
For instance, if you use the {tomcat-docs}/jdbc-pool.html#Common_Attributes[Tomcat connection pool], you could customize many additional settings, as shown in the following example:
For instance, if you use the {url-tomcat-docs}/jdbc-pool.html#Common_Attributes[Tomcat connection pool], you could customize many additional settings, as shown in the following example:
[source,yaml,indent=0,subs="verbatim",configprops,configblocks]
[configprops,yaml]
----
spring:
datasource:
tomcat:
max-wait: 10000
max-active: 50
test-on-borrow: true
spring:
datasource:
tomcat:
max-wait: 10000
max-active: 50
test-on-borrow: true
----
This will set the pool to wait 10000ms before throwing an exception if no connection is available, limit the maximum number of connections to 50 and validate the connection before borrowing it from the pool.
@ -110,7 +115,8 @@ This will set the pool to wait 10000ms before throwing an exception if no connec
[[data.sql.datasource.connection-pool]]
==== Supported Connection Pools
=== Supported Connection Pools
Spring Boot uses the following algorithm for choosing a specific implementation:
. We prefer https://github.com/brettwooldridge/HikariCP[HikariCP] for its performance and concurrency.
@ -140,35 +146,37 @@ The following connection pools are supported by `DataSourceBuilder`:
[[data.sql.datasource.jndi]]
==== Connection to a JNDI DataSource
=== Connection to a JNDI DataSource
If you deploy your Spring Boot application to an Application Server, you might want to configure and manage your DataSource by using your Application Server's built-in features and access it by using JNDI.
The configprop:spring.datasource.jndi-name[] property can be used as an alternative to the configprop:spring.datasource.url[], configprop:spring.datasource.username[], and configprop:spring.datasource.password[] properties to access the `DataSource` from a specific JNDI location.
For example, the following section in `application.properties` shows how you can access a JBoss AS defined `DataSource`:
[source,yaml,indent=0,subs="verbatim",configprops,configblocks]
[configprops,yaml]
----
spring:
datasource:
jndi-name: "java:jboss/datasources/customers"
spring:
datasource:
jndi-name: "java:jboss/datasources/customers"
----
[[data.sql.jdbc-template]]
=== Using JdbcTemplate
== Using JdbcTemplate
Spring's `JdbcTemplate` and `NamedParameterJdbcTemplate` classes are auto-configured, and you can `@Autowire` them directly into your own beans, as shown in the following example:
include::code:MyBean[]
include-code::MyBean[]
You can customize some properties of the template by using the `spring.jdbc.template.*` properties, as shown in the following example:
[source,yaml,indent=0,subs="verbatim",configprops,configblocks]
[configprops,yaml]
----
spring:
jdbc:
template:
max-rows: 500
spring:
jdbc:
template:
max-rows: 500
----
NOTE: The `NamedParameterJdbcTemplate` reuses the same `JdbcTemplate` instance behind the scenes.
@ -177,18 +185,20 @@ If more than one `JdbcTemplate` is defined and no primary candidate exists, the
[[data.sql.jdbc-client]]
=== Using JdbcClient
== Using JdbcClient
Spring's `JdbcClient` is auto-configured based on the presence of a `NamedParameterJdbcTemplate`.
You can inject it directly in your own beans as well, as shown in the following example:
include::code:MyBean[]
include-code::MyBean[]
If you rely on auto-configuration to create the underlying `JdbcTemplate`, any customization using `spring.jdbc.template.*` properties is taken into account in the client as well.
[[data.sql.jpa-and-spring-data]]
=== JPA and Spring Data JPA
== JPA and Spring Data JPA
The Java Persistence API is a standard technology that lets you "`map`" objects to relational databases.
The `spring-boot-starter-data-jpa` POM provides a quick way to get started.
It provides the following key dependencies:
@ -197,43 +207,45 @@ It provides the following key dependencies:
* Spring Data JPA: Helps you to implement JPA-based repositories.
* Spring ORM: Core ORM support from the Spring Framework.
TIP: We do not go into too many details of JPA or {spring-data}[Spring Data] here.
You can follow the https://spring.io/guides/gs/accessing-data-jpa/["`Accessing Data with JPA`"] guide from https://spring.io and read the {spring-data-jpa}[Spring Data JPA] and https://hibernate.org/orm/documentation/[Hibernate] reference documentation.
TIP: We do not go into too many details of JPA or {url-spring-data-site}[Spring Data] here.
You can follow the https://spring.io/guides/gs/accessing-data-jpa/["`Accessing Data with JPA`"] guide from https://spring.io and read the {url-spring-data-jpa-site}[Spring Data JPA] and https://hibernate.org/orm/documentation/[Hibernate] reference documentation.
[[data.sql.jpa-and-spring-data.entity-classes]]
==== Entity Classes
=== Entity Classes
Traditionally, JPA "`Entity`" classes are specified in a `persistence.xml` file.
With Spring Boot, this file is not necessary and "`Entity Scanning`" is used instead.
By default the <<using#using.auto-configuration.packages,auto-configuration packages>> are scanned.
By default the xref:using/auto-configuration.adoc#using.auto-configuration.packages[auto-configuration packages] are scanned.
Any classes annotated with `@Entity`, `@Embeddable`, or `@MappedSuperclass` are considered.
A typical entity class resembles the following example:
include::code:City[]
include-code::City[]
TIP: You can customize entity scanning locations by using the `@EntityScan` annotation.
See the "`<<howto#howto.data-access.separate-entity-definitions-from-spring-configuration>>`" how-to.
See the "`xref:how-to:data-access.adoc#howto.data-access.separate-entity-definitions-from-spring-configuration[Separate @Entity Definitions from Spring Configuration]`" how-to.
[[data.sql.jpa-and-spring-data.repositories]]
==== Spring Data JPA Repositories
{spring-data-jpa}[Spring Data JPA] repositories are interfaces that you can define to access data.
=== Spring Data JPA Repositories
{url-spring-data-jpa-site}[Spring Data JPA] repositories are interfaces that you can define to access data.
JPA queries are created automatically from your method names.
For example, a `CityRepository` interface might declare a `findAllByState(String state)` method to find all the cities in a given state.
For more complex queries, you can annotate your method with Spring Data's {spring-data-jpa-api}/repository/Query.html[`Query`] annotation.
For more complex queries, you can annotate your method with Spring Data's {url-spring-data-jpa-javadoc}/org/springframework/data/jpa/repository/Query.html[`Query`] annotation.
Spring Data repositories usually extend from the {spring-data-commons-api}/repository/Repository.html[`Repository`] or {spring-data-commons-api}/repository/CrudRepository.html[`CrudRepository`] interfaces.
If you use auto-configuration, the <<using#using.auto-configuration.packages,auto-configuration packages>> are searched for repositories.
Spring Data repositories usually extend from the {url-spring-data-commons-javadoc}/org/springframework/data/repository/Repository.html[`Repository`] or {url-spring-data-commons-javadoc}/org/springframework/data/repository/CrudRepository.html[`CrudRepository`] interfaces.
If you use auto-configuration, the xref:using/auto-configuration.adoc#using.auto-configuration.packages[auto-configuration packages] are searched for repositories.
TIP: You can customize the locations to look for repositories using `@EnableJpaRepositories`.
The following example shows a typical Spring Data repository interface definition:
include::code:CityRepository[]
include-code::CityRepository[]
Spring Data JPA repositories support three different modes of bootstrapping: default, deferred, and lazy.
To enable deferred or lazy bootstrapping, set the configprop:spring.data.jpa.repositories.bootstrap-mode[] property to `deferred` or `lazy` respectively.
@ -248,46 +260,48 @@ For JPA components (such as converters) that are created as Spring beans, use `O
====
TIP: We have barely scratched the surface of Spring Data JPA.
For complete details, see the {spring-data-jpa-docs}[Spring Data JPA reference documentation].
For complete details, see the {url-spring-data-jpa-docs}[Spring Data JPA reference documentation].
[[data.sql.jpa-and-spring-data.envers-repositories]]
==== Spring Data Envers Repositories
If {spring-data-envers}[Spring Data Envers] is available, JPA repositories are auto-configured to support typical Envers queries.
=== Spring Data Envers Repositories
If {url-spring-data-envers-site}[Spring Data Envers] is available, JPA repositories are auto-configured to support typical Envers queries.
To use Spring Data Envers, make sure your repository extends from `RevisionRepository` as shown in the following example:
include::code:CountryRepository[]
include-code::CountryRepository[]
NOTE: For more details, check the {spring-data-jpa-docs}/#envers[Spring Data Envers reference documentation].
NOTE: For more details, check the {url-spring-data-jpa-docs}/envers.html[Spring Data Envers reference documentation].
[[data.sql.jpa-and-spring-data.creating-and-dropping]]
==== Creating and Dropping JPA Databases
=== Creating and Dropping JPA Databases
By default, JPA databases are automatically created *only* if you use an embedded database (H2, HSQL, or Derby).
You can explicitly configure JPA settings by using `+spring.jpa.*+` properties.
For example, to create and drop tables you can add the following line to your `application.properties`:
[source,yaml,indent=0,subs="verbatim",configprops,configblocks]
[configprops,yaml]
----
spring:
jpa:
hibernate.ddl-auto: "create-drop"
spring:
jpa:
hibernate.ddl-auto: "create-drop"
----
NOTE: Hibernate's own internal property name for this (if you happen to remember it better) is `hibernate.hbm2ddl.auto`.
You can set it, along with other Hibernate native properties, by using `+spring.jpa.properties.*+` (the prefix is stripped before adding them to the entity manager).
The following line shows an example of setting JPA properties for Hibernate:
[source,yaml,indent=0,subs="verbatim",configprops,configblocks]
[configprops,yaml]
----
spring:
jpa:
properties:
hibernate:
"globally_quoted_identifiers": "true"
spring:
jpa:
properties:
hibernate:
"globally_quoted_identifiers": "true"
----
The line in the preceding example passes a value of `true` for the `hibernate.globally_quoted_identifiers` property to the Hibernate entity manager.
@ -298,14 +312,16 @@ There is also a `spring.jpa.generate-ddl` flag, but it is not used if Hibernate
[[data.sql.jpa-and-spring-data.open-entity-manager-in-view]]
==== Open EntityManager in View
If you are running a web application, Spring Boot by default registers {spring-framework-api}/orm/jpa/support/OpenEntityManagerInViewInterceptor.html[`OpenEntityManagerInViewInterceptor`] to apply the "`Open EntityManager in View`" pattern, to allow for lazy loading in web views.
=== Open EntityManager in View
If you are running a web application, Spring Boot by default registers {url-spring-framework-javadoc}/org/springframework/orm/jpa/support/OpenEntityManagerInViewInterceptor.html[`OpenEntityManagerInViewInterceptor`] to apply the "`Open EntityManager in View`" pattern, to allow for lazy loading in web views.
If you do not want this behavior, you should set `spring.jpa.open-in-view` to `false` in your `application.properties`.
[[data.sql.jdbc]]
=== Spring Data JDBC
== Spring Data JDBC
Spring Data includes repository support for JDBC and will automatically generate SQL for the methods on `CrudRepository`.
For more advanced queries, a `@Query` annotation is provided.
@ -313,18 +329,19 @@ Spring Boot will auto-configure Spring Data's JDBC repositories when the necessa
They can be added to your project with a single dependency on `spring-boot-starter-data-jdbc`.
If necessary, you can take control of Spring Data JDBC's configuration by adding the `@EnableJdbcRepositories` annotation or an `AbstractJdbcConfiguration` subclass to your application.
TIP: For complete details of Spring Data JDBC, see the {spring-data-jdbc-docs}[reference documentation].
TIP: For complete details of Spring Data JDBC, see the {url-spring-data-jdbc-docs}[reference documentation].
[[data.sql.h2-web-console]]
=== Using H2's Web Console
== Using H2's Web Console
The https://www.h2database.com[H2 database] provides a https://www.h2database.com/html/quickstart.html#h2_console[browser-based console] that Spring Boot can auto-configure for you.
The console is auto-configured when the following conditions are met:
* You are developing a servlet-based web application.
* `com.h2database:h2` is on the classpath.
* You are using <<using#using.devtools,Spring Boot's developer tools>>.
* You are using xref:using/devtools.adoc[Spring Boot's developer tools].
TIP: If you are not using Spring Boot's developer tools but would still like to make use of H2's console, you can configure the configprop:spring.h2.console.enabled[] property with a value of `true`.
@ -333,25 +350,27 @@ NOTE: The H2 console is only intended for use during development, so you should
[[data.sql.h2-web-console.custom-path]]
==== Changing the H2 Console's Path
=== Changing the H2 Console's Path
By default, the console is available at `/h2-console`.
You can customize the console's path by using the configprop:spring.h2.console.path[] property.
[[data.sql.h2-web-console.spring-security]]
==== Accessing the H2 Console in a Secured Application
=== Accessing the H2 Console in a Secured Application
H2 Console uses frames and, as it is intended for development only, does not implement CSRF protection measures.
If your application uses Spring Security, you need to configure it to
* disable CSRF protection for requests against the console,
* set the header `X-Frame-Options` to `SAMEORIGIN` on responses from the console.
More information on {spring-security-docs}/features/exploits/csrf.html[CSRF] and the header {spring-security-docs}/features/exploits/headers.html#headers-frame-options[X-Frame-Options] can be found in the Spring Security Reference Guide.
More information on {url-spring-security-docs}/features/exploits/csrf.html[CSRF] and the header {url-spring-security-docs}/features/exploits/headers.html#headers-frame-options[X-Frame-Options] can be found in the Spring Security Reference Guide.
In simple setups, a `SecurityFilterChain` like the following can be used:
include::code:DevProfileSecurityConfiguration[tag=!customizer]
include-code::DevProfileSecurityConfiguration[tag=!customizer]
WARNING: The H2 console is only intended for use during development.
In production, disabling CSRF protection or allowing frames for a website may create severe security risks.
@ -361,67 +380,71 @@ TIP: `PathRequest.toH2Console()` returns the correct request matcher also when t
[[data.sql.jooq]]
=== Using jOOQ
== Using jOOQ
jOOQ Object Oriented Querying (https://www.jooq.org/[jOOQ]) is a popular product from https://www.datageekery.com/[Data Geekery] which generates Java code from your database and lets you build type-safe SQL queries through its fluent API.
Both the commercial and open source editions can be used with Spring Boot.
[[data.sql.jooq.codegen]]
==== Code Generation
=== Code Generation
In order to use jOOQ type-safe queries, you need to generate Java classes from your database schema.
You can follow the instructions in the {jooq-docs}/#jooq-in-7-steps-step3[jOOQ user manual].
You can follow the instructions in the {url-jooq-docs}/#jooq-in-7-steps-step3[jOOQ user manual].
If you use the `jooq-codegen-maven` plugin and you also use the `spring-boot-starter-parent` "`parent POM`", you can safely omit the plugin's `<version>` tag.
You can also use Spring Boot-defined version variables (such as `h2.version`) to declare the plugin's database dependency.
The following listing shows an example:
[source,xml,indent=0,subs="verbatim"]
[source,xml]
----
<plugin>
<groupId>org.jooq</groupId>
<artifactId>jooq-codegen-maven</artifactId>
<executions>
<plugin>
<groupId>org.jooq</groupId>
<artifactId>jooq-codegen-maven</artifactId>
<executions>
...
</executions>
<dependencies>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>${h2.version}</version>
</dependency>
</dependencies>
<configuration>
<jdbc>
<driver>org.h2.Driver</driver>
<url>jdbc:h2:~/yourdatabase</url>
</jdbc>
<generator>
...
</executions>
<dependencies>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>${h2.version}</version>
</dependency>
</dependencies>
<configuration>
<jdbc>
<driver>org.h2.Driver</driver>
<url>jdbc:h2:~/yourdatabase</url>
</jdbc>
<generator>
...
</generator>
</configuration>
</plugin>
</generator>
</configuration>
</plugin>
----
[[data.sql.jooq.dslcontext]]
==== Using DSLContext
=== Using DSLContext
The fluent API offered by jOOQ is initiated through the `org.jooq.DSLContext` interface.
Spring Boot auto-configures a `DSLContext` as a Spring Bean and connects it to your application `DataSource`.
To use the `DSLContext`, you can inject it, as shown in the following example:
include::code:MyBean[tag=!method]
include-code::MyBean[tag=!method]
TIP: The jOOQ manual tends to use a variable named `create` to hold the `DSLContext`.
You can then use the `DSLContext` to construct your queries, as shown in the following example:
include::code:MyBean[tag=method]
include-code::MyBean[tag=method]
[[data.sql.jooq.sqldialect]]
==== jOOQ SQL Dialect
=== jOOQ SQL Dialect
Unless the configprop:spring.jooq.sql-dialect[] property has been configured, Spring Boot determines the SQL dialect to use for your datasource.
If Spring Boot could not detect the dialect, it uses `DEFAULT`.
@ -430,7 +453,8 @@ NOTE: Spring Boot can only auto-configure dialects supported by the open source
[[data.sql.jooq.customizing]]
==== Customizing jOOQ
=== Customizing jOOQ
More advanced customizations can be achieved by defining your own `DefaultConfigurationCustomizer` bean that will be invoked prior to creating the `org.jooq.Configuration` `@Bean`.
This takes precedence to anything that is applied by the auto-configuration.
@ -439,7 +463,8 @@ You can also create your own `org.jooq.Configuration` `@Bean` if you want to tak
[[data.sql.r2dbc]]
=== Using R2DBC
== Using R2DBC
The Reactive Relational Database Connectivity (https://r2dbc.io[R2DBC]) project brings reactive programming APIs to relational databases.
R2DBC's `io.r2dbc.spi.Connection` provides a standard method of working with non-blocking database connections.
Connections are provided by using a `ConnectionFactory`, similar to a `DataSource` with jdbc.
@ -447,13 +472,13 @@ Connections are provided by using a `ConnectionFactory`, similar to a `DataSourc
`ConnectionFactory` configuration is controlled by external configuration properties in `+spring.r2dbc.*+`.
For example, you might declare the following section in `application.properties`:
[source,yaml,indent=0,subs="verbatim",configprops,configblocks]
[configprops,yaml]
----
spring:
r2dbc:
url: "r2dbc:postgresql://localhost/test"
username: "dbuser"
password: "dbpass"
spring:
r2dbc:
url: "r2dbc:postgresql://localhost/test"
username: "dbuser"
password: "dbpass"
----
TIP: You do not need to specify a driver class name, since Spring Boot obtains the driver from R2DBC's Connection Factory discovery.
@ -461,16 +486,16 @@ TIP: You do not need to specify a driver class name, since Spring Boot obtains t
NOTE: At least the url should be provided.
Information specified in the URL takes precedence over individual properties, that is `name`, `username`, `password` and pooling options.
TIP: The "`How-to`" section includes a <<howto#howto.data-initialization.using-basic-sql-scripts, section on how to initialize a database>>.
TIP: The "`How-to`" section includes a xref:how-to:data-initialization.adoc#howto.data-initialization.using-basic-sql-scripts[section on how to initialize a database].
To customize the connections created by a `ConnectionFactory`, that is, set specific parameters that you do not want (or cannot) configure in your central database configuration, you can use a `ConnectionFactoryOptionsBuilderCustomizer` `@Bean`.
The following example shows how to manually override the database port while the rest of the options are taken from the application configuration:
include::code:MyR2dbcConfiguration[]
include-code::MyR2dbcConfiguration[]
The following examples show how to set some PostgreSQL connection options:
include::code:MyPostgresR2dbcConfiguration[]
include-code::MyPostgresR2dbcConfiguration[]
When a `ConnectionFactory` bean is available, the regular JDBC `DataSource` auto-configuration backs off.
If you want to retain the JDBC `DataSource` auto-configuration, and are comfortable with the risk of using the blocking JDBC API in a reactive application, add `@Import(DataSourceAutoConfiguration.class)` on a `@Configuration` class in your application to re-enable it.
@ -478,18 +503,19 @@ If you want to retain the JDBC `DataSource` auto-configuration, and are comforta
[[data.sql.r2dbc.embedded]]
==== Embedded Database Support
Similarly to <<features#data.sql.datasource.embedded,the JDBC support>>, Spring Boot can automatically configure an embedded database for reactive usage.
=== Embedded Database Support
Similarly to xref:data/sql.adoc#data.sql.datasource.embedded[the JDBC support], Spring Boot can automatically configure an embedded database for reactive usage.
You need not provide any connection URLs.
You need only include a build dependency to the embedded database that you want to use, as shown in the following example:
[source,xml,indent=0,subs="verbatim"]
[source,xml]
----
<dependency>
<groupId>io.r2dbc</groupId>
<artifactId>r2dbc-h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>io.r2dbc</groupId>
<artifactId>r2dbc-h2</artifactId>
<scope>runtime</scope>
</dependency>
----
[NOTE]
@ -501,26 +527,28 @@ If you want to make sure that each context has a separate embedded database, you
[[data.sql.r2dbc.using-database-client]]
==== Using DatabaseClient
=== Using DatabaseClient
A `DatabaseClient` bean is auto-configured, and you can `@Autowire` it directly into your own beans, as shown in the following example:
include::code:MyBean[]
include-code::MyBean[]
[[data.sql.r2dbc.repositories]]
==== Spring Data R2DBC Repositories
=== Spring Data R2DBC Repositories
https://spring.io/projects/spring-data-r2dbc[Spring Data R2DBC] repositories are interfaces that you can define to access data.
Queries are created automatically from your method names.
For example, a `CityRepository` interface might declare a `findAllByState(String state)` method to find all the cities in a given state.
For more complex queries, you can annotate your method with Spring Data's {spring-data-r2dbc-api}/repository/Query.html[`Query`] annotation.
For more complex queries, you can annotate your method with Spring Data's {url-spring-data-r2dbc-javadoc}/org/springframework/data/r2dbc/repository/Query.html[`Query`] annotation.
Spring Data repositories usually extend from the {spring-data-commons-api}/repository/Repository.html[`Repository`] or {spring-data-commons-api}/repository/CrudRepository.html[`CrudRepository`] interfaces.
If you use auto-configuration, the <<using#using.auto-configuration.packages,auto-configuration packages>> are searched for repositories.
Spring Data repositories usually extend from the {url-spring-data-commons-javadoc}/org/springframework/data/repository/Repository.html[`Repository`] or {url-spring-data-commons-javadoc}/org/springframework/data/repository/CrudRepository.html[`CrudRepository`] interfaces.
If you use auto-configuration, the xref:using/auto-configuration.adoc#using.auto-configuration.packages[auto-configuration packages] are searched for repositories.
The following example shows a typical Spring Data repository interface definition:
include::code:CityRepository[]
include-code::CityRepository[]
TIP: We have barely scratched the surface of Spring Data R2DBC. For complete details, see the {spring-data-r2dbc-docs}[Spring Data R2DBC reference documentation].
TIP: We have barely scratched the surface of Spring Data R2DBC. For complete details, see the {url-spring-data-r2dbc-docs}[Spring Data R2DBC reference documentation].

View File

@ -1,5 +1,6 @@
[[deployment.cloud]]
== Deploying to the Cloud
= Deploying to the Cloud
Spring Boot's executable jars are ready-made for most popular cloud PaaS (Platform-as-a-Service) providers.
These providers tend to require that you "`bring your own container`".
They manage application processes (not Java applications specifically), so they need an intermediary layer that adapts _your_ application to the _cloud's_ notion of a running process.
@ -13,12 +14,13 @@ It minimizes divergence between development and production environments.
Ideally, your application, like a Spring Boot executable jar, has everything that it needs to run packaged within it.
In this section, we look at what it takes to get the <<getting-started#getting-started.first-application, application that we developed>> in the "`Getting Started`" section up and running in the Cloud.
In this section, we look at what it takes to get the xref:tutorial:first-application/index.adoc[application that we developed] in the "`Getting Started`" section up and running in the Cloud.
[[deployment.cloud.cloud-foundry]]
=== Cloud Foundry
== Cloud Foundry
Cloud Foundry provides default buildpacks that come into play if no other buildpack is specified.
The Cloud Foundry https://github.com/cloudfoundry/java-buildpack[Java buildpack] has excellent support for Spring applications, including Spring Boot.
You can deploy stand-alone executable jar applications as well as traditional `.war` packaged applications.
@ -27,9 +29,9 @@ Once you have built your application (by using, for example, `mvn clean package`
Be sure to have https://docs.cloudfoundry.org/cf-cli/getting-started.html#login[logged in with your `cf` command line client] before pushing an application.
The following line shows using the `cf push` command to deploy an application:
[source,shell,indent=0,subs="verbatim"]
[source,shell]
----
$ cf push acloudyspringtime -p target/demo-0.0.1-SNAPSHOT.jar
$ cf push acloudyspringtime -p target/demo-0.0.1-SNAPSHOT.jar
----
NOTE: In the preceding example, we substitute `acloudyspringtime` for whatever value you give `cf` as the name of your application.
@ -39,45 +41,45 @@ If there is a Cloud Foundry https://docs.cloudfoundry.org/devguide/deploy-apps/m
At this point, `cf` starts uploading your application, producing output similar to the following example:
[indent=0,subs="verbatim,quotes"]
[source,subs="verbatim,quotes"]
----
Uploading acloudyspringtime... *OK*
Preparing to start acloudyspringtime... *OK*
-----> Downloaded app package (*8.9M*)
-----> Java Buildpack Version: v3.12 (offline) | https://github.com/cloudfoundry/java-buildpack.git#6f25b7e
-----> Downloading Open Jdk JRE
Expanding Open Jdk JRE to .java-buildpack/open_jdk_jre (1.6s)
-----> Downloading Open JDK Like Memory Calculator 2.0.2_RELEASE from https://java-buildpack.cloudfoundry.org/memory-calculator/trusty/x86_64/memory-calculator-2.0.2_RELEASE.tar.gz (found in cache)
Memory Settings: -Xss349K -Xmx681574K -XX:MaxMetaspaceSize=104857K -Xms681574K -XX:MetaspaceSize=104857K
-----> Downloading Container Certificate Trust Store 1.0.0_RELEASE from https://java-buildpack.cloudfoundry.org/container-certificate-trust-store/container-certificate-trust-store-1.0.0_RELEASE.jar (found in cache)
Adding certificates to .java-buildpack/container_certificate_trust_store/truststore.jks (0.6s)
-----> Downloading Spring Auto Reconfiguration 1.10.0_RELEASE from https://java-buildpack.cloudfoundry.org/auto-reconfiguration/auto-reconfiguration-1.10.0_RELEASE.jar (found in cache)
Checking status of app 'acloudyspringtime'...
0 of 1 instances running (1 starting)
...
0 of 1 instances running (1 starting)
...
0 of 1 instances running (1 starting)
...
1 of 1 instances running (1 running)
Uploading acloudyspringtime... *OK*
Preparing to start acloudyspringtime... *OK*
-----> Downloaded app package (*8.9M*)
-----> Java Buildpack Version: v3.12 (offline) | https://github.com/cloudfoundry/java-buildpack.git#6f25b7e
-----> Downloading Open Jdk JRE
Expanding Open Jdk JRE to .java-buildpack/open_jdk_jre (1.6s)
-----> Downloading Open JDK Like Memory Calculator 2.0.2_RELEASE from https://java-buildpack.cloudfoundry.org/memory-calculator/trusty/x86_64/memory-calculator-2.0.2_RELEASE.tar.gz (found in cache)
Memory Settings: -Xss349K -Xmx681574K -XX:MaxMetaspaceSize=104857K -Xms681574K -XX:MetaspaceSize=104857K
-----> Downloading Container Certificate Trust Store 1.0.0_RELEASE from https://java-buildpack.cloudfoundry.org/container-certificate-trust-store/container-certificate-trust-store-1.0.0_RELEASE.jar (found in cache)
Adding certificates to .java-buildpack/container_certificate_trust_store/truststore.jks (0.6s)
-----> Downloading Spring Auto Reconfiguration 1.10.0_RELEASE from https://java-buildpack.cloudfoundry.org/auto-reconfiguration/auto-reconfiguration-1.10.0_RELEASE.jar (found in cache)
Checking status of app 'acloudyspringtime'...
0 of 1 instances running (1 starting)
...
0 of 1 instances running (1 starting)
...
0 of 1 instances running (1 starting)
...
1 of 1 instances running (1 running)
App started
App started
----
Congratulations! The application is now live!
Once your application is live, you can verify the status of the deployed application by using the `cf apps` command, as shown in the following example:
[source,shell,indent=0,subs="verbatim"]
[source,shell]
----
$ cf apps
Getting applications in ...
OK
$ cf apps
Getting applications in ...
OK
name requested state instances memory disk urls
...
acloudyspringtime started 1/1 512M 1G acloudyspringtime.cfapps.io
...
name requested state instances memory disk urls
...
acloudyspringtime started 1/1 512M 1G acloudyspringtime.cfapps.io
...
----
Once Cloud Foundry acknowledges that your application has been deployed, you should be able to find the application at the URI given.
@ -86,34 +88,37 @@ In the preceding example, you could find it at `\https://acloudyspringtime.cfapp
[[deployment.cloud.cloud-foundry.binding-to-services]]
==== Binding to Services
=== Binding to Services
By default, metadata about the running application as well as service connection information is exposed to the application as environment variables (for example: `$VCAP_SERVICES`).
This architecture decision is due to Cloud Foundry's polyglot (any language and platform can be supported as a buildpack) nature.
Process-scoped environment variables are language agnostic.
Environment variables do not always make for the easiest API, so Spring Boot automatically extracts them and flattens the data into properties that can be accessed through Spring's `Environment` abstraction, as shown in the following example:
include::code:MyBean[]
include-code::MyBean[]
All Cloud Foundry properties are prefixed with `vcap`.
You can use `vcap` properties to access application information (such as the public URL of the application) and service information (such as database credentials).
See the {spring-boot-module-api}/cloud/CloudFoundryVcapEnvironmentPostProcessor.html[`CloudFoundryVcapEnvironmentPostProcessor`] Javadoc for complete details.
See the xref:api:java/org/springframework/boot/cloud/CloudFoundryVcapEnvironmentPostProcessor.html[`CloudFoundryVcapEnvironmentPostProcessor`] Javadoc for complete details.
TIP: The https://github.com/pivotal-cf/java-cfenv/[Java CFEnv] project is a better fit for tasks such as configuring a DataSource.
[[deployment.cloud.kubernetes]]
=== Kubernetes
== Kubernetes
Spring Boot auto-detects Kubernetes deployment environments by checking the environment for `"*_SERVICE_HOST"` and `"*_SERVICE_PORT"` variables.
You can override this detection with the configprop:spring.main.cloud-platform[] configuration property.
Spring Boot helps you to <<features#features.spring-application.application-availability,manage the state of your application>> and export it with <<actuator#actuator.endpoints.kubernetes-probes, HTTP Kubernetes Probes using Actuator>>.
Spring Boot helps you to xref:features/spring-application.adoc#features.spring-application.application-availability[manage the state of your application] and export it with xref:actuator/endpoints.adoc#actuator.endpoints.kubernetes-probes[HTTP Kubernetes Probes using Actuator].
[[deployment.cloud.kubernetes.container-lifecycle]]
==== Kubernetes Container Lifecycle
=== Kubernetes Container Lifecycle
When Kubernetes deletes an application instance, the shutdown process involves several subsystems concurrently: shutdown hooks, unregistering the service, removing the instance from the load-balancer...
Because this shutdown processing happens in parallel (and due to the nature of distributed systems), there is a window during which traffic can be routed to a pod that has also begun its shutdown processing.
@ -121,19 +126,19 @@ You can configure a sleep execution in a preStop handler to avoid requests being
This sleep should be long enough for new requests to stop being routed to the pod and its duration will vary from deployment to deployment.
The preStop handler can be configured by using the PodSpec in the pod's configuration file as follows:
[source,yaml,indent=0,subs="verbatim"]
[source,yaml]
----
spec:
containers:
- name: "example-container"
image: "example-image"
lifecycle:
preStop:
exec:
command: ["sh", "-c", "sleep 10"]
spec:
containers:
- name: "example-container"
image: "example-image"
lifecycle:
preStop:
exec:
command: ["sh", "-c", "sleep 10"]
----
Once the pre-stop hook has completed, SIGTERM will be sent to the container and <<web#web.graceful-shutdown,graceful shutdown>> will begin, allowing any remaining in-flight requests to complete.
Once the pre-stop hook has completed, SIGTERM will be sent to the container and xref:web/graceful-shutdown.adoc[graceful shutdown] will begin, allowing any remaining in-flight requests to complete.
NOTE: When Kubernetes sends a SIGTERM signal to the pod, it waits for a specified time called the termination grace period (the default for which is 30 seconds).
If the containers are still running after the grace period, they are sent the SIGKILL signal and forcibly removed.
@ -142,7 +147,8 @@ If the pod takes longer than 30 seconds to shut down, which could be because you
[[deployment.cloud.heroku]]
=== Heroku
== Heroku
Heroku is another popular PaaS platform.
To customize Heroku builds, you provide a `Procfile`, which provides the incantation required to deploy an application.
Heroku assigns a `port` for the Java application to use and then ensures that routing to the external URI works.
@ -150,9 +156,9 @@ Heroku assigns a `port` for the Java application to use and then ensures that ro
You must configure your application to listen on the correct port.
The following example shows the `Procfile` for our starter REST application:
[indent=0]
[source]
----
web: java -Dserver.port=$PORT -jar target/demo-0.0.1-SNAPSHOT.jar
web: java -Dserver.port=$PORT -jar target/demo-0.0.1-SNAPSHOT.jar
----
Spring Boot makes `-D` arguments available as properties accessible from a Spring `Environment` instance.
@ -162,52 +168,52 @@ The `$PORT` environment variable is assigned to us by the Heroku PaaS.
This should be everything you need.
The most common deployment workflow for Heroku deployments is to `git push` the code to production, as shown in the following example:
[source,shell,indent=0,subs="verbatim"]
[source,shell]
----
$ git push heroku main
$ git push heroku main
----
Which will result in the following:
[indent=0,subs="verbatim,quotes"]
[source,subs="verbatim,quotes"]
----
Initializing repository, *done*.
Counting objects: 95, *done*.
Delta compression using up to 8 threads.
Compressing objects: 100% (78/78), *done*.
Writing objects: 100% (95/95), 8.66 MiB | 606.00 KiB/s, *done*.
Total 95 (delta 31), reused 0 (delta 0)
Initializing repository, *done*.
Counting objects: 95, *done*.
Delta compression using up to 8 threads.
Compressing objects: 100% (78/78), *done*.
Writing objects: 100% (95/95), 8.66 MiB | 606.00 KiB/s, *done*.
Total 95 (delta 31), reused 0 (delta 0)
-----> Java app detected
-----> Installing OpenJDK... *done*
-----> Installing Maven... *done*
-----> Installing settings.xml... *done*
-----> Executing: mvn -B -DskipTests=true clean install
-----> Java app detected
-----> Installing OpenJDK... *done*
-----> Installing Maven... *done*
-----> Installing settings.xml... *done*
-----> Executing: mvn -B -DskipTests=true clean install
[INFO] Scanning for projects...
Downloading: https://repo.spring.io/...
Downloaded: https://repo.spring.io/... (818 B at 1.8 KB/sec)
....
Downloaded: https://s3pository.heroku.com/jvm/... (152 KB at 595.3 KB/sec)
[INFO] Installing /tmp/build_0c35a5d2-a067-4abc-a232-14b1fb7a8229/target/...
[INFO] Installing /tmp/build_0c35a5d2-a067-4abc-a232-14b1fb7a8229/pom.xml ...
[INFO] ------------------------------------------------------------------------
[INFO] *BUILD SUCCESS*
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 59.358s
[INFO] Finished at: Fri Mar 07 07:28:25 UTC 2014
[INFO] Final Memory: 20M/493M
[INFO] ------------------------------------------------------------------------
[INFO] Scanning for projects...
Downloading: https://repo.spring.io/...
Downloaded: https://repo.spring.io/... (818 B at 1.8 KB/sec)
....
Downloaded: https://s3pository.heroku.com/jvm/... (152 KB at 595.3 KB/sec)
[INFO] Installing /tmp/build_0c35a5d2-a067-4abc-a232-14b1fb7a8229/target/...
[INFO] Installing /tmp/build_0c35a5d2-a067-4abc-a232-14b1fb7a8229/pom.xml ...
[INFO] ------------------------------------------------------------------------
[INFO] *BUILD SUCCESS*
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 59.358s
[INFO] Finished at: Fri Mar 07 07:28:25 UTC 2014
[INFO] Final Memory: 20M/493M
[INFO] ------------------------------------------------------------------------
-----> Discovering process types
Procfile declares types -> *web*
-----> Discovering process types
Procfile declares types -> *web*
-----> Compressing... *done*, 70.4MB
-----> Launching... *done*, v6
https://agile-sierra-1405.herokuapp.com/ *deployed to Heroku*
-----> Compressing... *done*, 70.4MB
-----> Launching... *done*, v6
https://agile-sierra-1405.herokuapp.com/ *deployed to Heroku*
To git@heroku.com:agile-sierra-1405.git
* [new branch] main -> main
To git@heroku.com:agile-sierra-1405.git
* [new branch] main -> main
----
Your application should now be up and running on Heroku.
@ -216,7 +222,8 @@ For more details, see https://devcenter.heroku.com/articles/deploying-spring-boo
[[deployment.cloud.openshift]]
=== OpenShift
== OpenShift
https://www.openshift.com/[OpenShift] has many resources describing how to deploy Spring Boot applications, including:
* https://blog.openshift.com/using-openshift-enterprise-grade-spring-boot-deployments/[Using the S2I builder]
@ -227,7 +234,8 @@ https://www.openshift.com/[OpenShift] has many resources describing how to deplo
[[deployment.cloud.aws]]
=== Amazon Web Services (AWS)
== Amazon Web Services (AWS)
Amazon Web Services offers multiple ways to install Spring Boot-based applications, either as traditional web applications (war) or as executable jar files with an embedded web server.
The options include:
@ -243,14 +251,16 @@ In this document, we describe to approach using AWS Elastic Beanstalk.
[[deployment.cloud.aws.beanstalk]]
==== AWS Elastic Beanstalk
=== AWS Elastic Beanstalk
As described in the official https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/create_deploy_Java.html[Elastic Beanstalk Java guide], there are two main options to deploy a Java application.
You can either use the "`Tomcat Platform`" or the "`Java SE platform`".
[[deployment.cloud.aws.beanstalk.tomcat-platform]]
===== Using the Tomcat Platform
==== Using the Tomcat Platform
This option applies to Spring Boot projects that produce a war file.
No special configuration is required.
You need only follow the official guide.
@ -258,14 +268,16 @@ You need only follow the official guide.
[[deployment.cloud.aws.beanstalk.java-se-platform]]
===== Using the Java SE Platform
==== Using the Java SE Platform
This option applies to Spring Boot projects that produce a jar file and run an embedded web container.
Elastic Beanstalk environments run an nginx instance on port 80 to proxy the actual application, running on port 5000.
To configure it, add the following line to your `application.properties` file:
[indent=0]
[configprops,yaml]
----
server.port=5000
server:
port: 5000
----
@ -276,10 +288,10 @@ By default, Elastic Beanstalk uploads sources and compiles them in AWS.
However, it is best to upload the binaries instead.
To do so, add lines similar to the following to your `.elasticbeanstalk/config.yml` file:
[source,xml,indent=0,subs="verbatim"]
[source,xml]
----
deploy:
artifact: target/demo-0.0.1-SNAPSHOT.jar
deploy:
artifact: target/demo-0.0.1-SNAPSHOT.jar
----
====
@ -291,32 +303,34 @@ The load balancer has a significant cost.
To avoid that cost, set the environment type to "`Single instance`", as described in https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/environments-create-wizard.html#environments-create-wizard-capacity[the Amazon documentation].
You can also create single instance environments by using the CLI and the following command:
[indent=0]
[source]
----
eb create -s
eb create -s
----
====
[[deployment.cloud.aws.summary]]
==== Summary
=== Summary
This is one of the easiest ways to get to AWS, but there are more things to cover, such as how to integrate Elastic Beanstalk into any CI / CD tool, use the Elastic Beanstalk Maven plugin instead of the CLI, and others.
There is a https://exampledriven.wordpress.com/2017/01/09/spring-boot-aws-elastic-beanstalk-example/[blog post] covering these topics more in detail.
[[deployment.cloud.boxfuse]]
=== CloudCaptain and Amazon Web Services
== CloudCaptain and Amazon Web Services
https://cloudcaptain.sh/[CloudCaptain] works by turning your Spring Boot executable jar or war into a minimal VM image that can be deployed unchanged either on VirtualBox or on AWS.
CloudCaptain comes with deep integration for Spring Boot and uses the information from your Spring Boot configuration file to automatically configure ports and health check URLs.
CloudCaptain leverages this information both for the images it produces as well as for all the resources it provisions (instances, security groups, elastic load balancers, and so on).
Once you have created a https://console.cloudcaptain.sh[CloudCaptain account], connected it to your AWS account, installed the latest version of the CloudCaptain Client, and ensured that the application has been built by Maven or Gradle (by using, for example, `mvn clean package`), you can deploy your Spring Boot application to AWS with a command similar to the following:
[source,shell,indent=0,subs="verbatim"]
[source,shell]
----
$ boxfuse run myapp-1.0.jar -env=prod
$ boxfuse run myapp-1.0.jar -env=prod
----
See the https://cloudcaptain.sh/docs/commandline/run.html[`boxfuse run` documentation] for more options.
@ -327,25 +341,25 @@ If your executable jar or war contains an https://cloudcaptain.sh/docs/payloads/
At this point, CloudCaptain creates an image for your application, uploads it, and configures and starts the necessary resources on AWS, resulting in output similar to the following example:
[indent=0,subs="verbatim"]
[source]
----
Fusing Image for myapp-1.0.jar ...
Image fused in 00:06.838s (53937 K) -> axelfontaine/myapp:1.0
Creating axelfontaine/myapp ...
Pushing axelfontaine/myapp:1.0 ...
Verifying axelfontaine/myapp:1.0 ...
Creating Elastic IP ...
Mapping myapp-axelfontaine.boxfuse.io to 52.28.233.167 ...
Waiting for AWS to create an AMI for axelfontaine/myapp:1.0 in eu-central-1 (this may take up to 50 seconds) ...
AMI created in 00:23.557s -> ami-d23f38cf
Creating security group boxfuse-sg_axelfontaine/myapp:1.0 ...
Launching t2.micro instance of axelfontaine/myapp:1.0 (ami-d23f38cf) in eu-central-1 ...
Instance launched in 00:30.306s -> i-92ef9f53
Waiting for AWS to boot Instance i-92ef9f53 and Payload to start at https://52.28.235.61/ ...
Payload started in 00:29.266s -> https://52.28.235.61/
Remapping Elastic IP 52.28.233.167 to i-92ef9f53 ...
Waiting 15s for AWS to complete Elastic IP Zero Downtime transition ...
Deployment completed successfully. axelfontaine/myapp:1.0 is up and running at https://myapp-axelfontaine.boxfuse.io/
Fusing Image for myapp-1.0.jar ...
Image fused in 00:06.838s (53937 K) -> axelfontaine/myapp:1.0
Creating axelfontaine/myapp ...
Pushing axelfontaine/myapp:1.0 ...
Verifying axelfontaine/myapp:1.0 ...
Creating Elastic IP ...
Mapping myapp-axelfontaine.boxfuse.io to 52.28.233.167 ...
Waiting for AWS to create an AMI for axelfontaine/myapp:1.0 in eu-central-1 (this may take up to 50 seconds) ...
AMI created in 00:23.557s -> ami-d23f38cf
Creating security group boxfuse-sg_axelfontaine/myapp:1.0 ...
Launching t2.micro instance of axelfontaine/myapp:1.0 (ami-d23f38cf) in eu-central-1 ...
Instance launched in 00:30.306s -> i-92ef9f53
Waiting for AWS to boot Instance i-92ef9f53 and Payload to start at https://52.28.235.61/ ...
Payload started in 00:29.266s -> https://52.28.235.61/
Remapping Elastic IP 52.28.233.167 to i-92ef9f53 ...
Waiting 15s for AWS to complete Elastic IP Zero Downtime transition ...
Deployment completed successfully. axelfontaine/myapp:1.0 is up and running at https://myapp-axelfontaine.boxfuse.io/
----
Your application should now be up and running on AWS.
@ -355,13 +369,15 @@ See the blog post on https://cloudcaptain.sh/blog/spring-boot-ec2.html[deploying
[[deployment.cloud.azure]]
=== Azure
== Azure
This https://spring.io/guides/gs/spring-boot-for-azure/[Getting Started guide] walks you through deploying your Spring Boot application to either https://azure.microsoft.com/en-us/services/spring-cloud/[Azure Spring Cloud] or https://docs.microsoft.com/en-us/azure/app-service/overview[Azure App Service].
[[deployment.cloud.google]]
=== Google Cloud
== Google Cloud
Google Cloud has several options that can be used to launch Spring Boot applications.
The easiest to get started with is probably App Engine, but you could also find ways to run Spring Boot in a container with Container Engine or on a virtual machine with Compute Engine.
@ -370,39 +386,39 @@ To deploy your first app to App Engine standard environment, follow https://code
Alternatively, App Engine Flex requires you to create an `app.yaml` file to describe the resources your app requires.
Normally, you put this file in `src/main/appengine`, and it should resemble the following file:
[source,yaml,indent=0,subs="verbatim"]
[source,yaml]
----
service: "default"
service: "default"
runtime: "java17"
env: "flex"
runtime: "java17"
env: "flex"
handlers:
- url: "/.*"
script: "this field is required, but ignored"
handlers:
- url: "/.*"
script: "this field is required, but ignored"
manual_scaling:
instances: 1
manual_scaling:
instances: 1
health_check:
enable_health_check: false
health_check:
enable_health_check: false
env_variables:
ENCRYPT_KEY: "your_encryption_key_here"
env_variables:
ENCRYPT_KEY: "your_encryption_key_here"
----
You can deploy the app (for example, with a Maven plugin) by adding the project ID to the build configuration, as shown in the following example:
[source,xml,indent=0,subs="verbatim"]
[source,xml]
----
<plugin>
<groupId>com.google.cloud.tools</groupId>
<artifactId>appengine-maven-plugin</artifactId>
<version>2.4.4</version>
<configuration>
<project>myproject</project>
</configuration>
</plugin>
<plugin>
<groupId>com.google.cloud.tools</groupId>
<artifactId>appengine-maven-plugin</artifactId>
<version>2.4.4</version>
<configuration>
<project>myproject</project>
</configuration>
</plugin>
----
Then deploy with `mvn appengine:deploy` (you need to authenticate first, otherwise the build fails).

View File

@ -1,19 +1,20 @@
[[deployment.efficient]]
== Efficient deployments
= Efficient deployments
[[deployment.efficient.unpacking]]
=== Unpacking the Executable JAR
== Unpacking the Executable JAR
If you are running your application from a container, you can use an executable jar, but it is also often an advantage to explode it and run it in a different way.
Certain PaaS implementations may also choose to unpack archives before they run.
For example, Cloud Foundry operates this way.
One way to run an unpacked archive is by starting the appropriate launcher, as follows:
[source,shell,indent=0,subs="verbatim"]
[source,shell]
----
$ jar -xf myapp.jar
$ java org.springframework.boot.loader.launch.JarLauncher
$ jar -xf myapp.jar
$ java org.springframework.boot.loader.launch.JarLauncher
----
This is actually slightly faster on startup (depending on the size of the jar) than running from an unexploded archive.
@ -21,10 +22,10 @@ After startup, you should not expect any differences.
Once you have unpacked the jar file, you can also get an extra boost to startup time by running the app with its "natural" main method instead of the `JarLauncher`. For example:
[source,shell,indent=0,subs="verbatim"]
[source,shell]
----
$ jar -xf myapp.jar
$ java -cp "BOOT-INF/classes:BOOT-INF/lib/*" com.example.MyApplication
$ jar -xf myapp.jar
$ java -cp "BOOT-INF/classes:BOOT-INF/lib/*" com.example.MyApplication
----
NOTE: Using the `JarLauncher` over the application's main method has the added benefit of a predictable classpath order.
@ -33,26 +34,27 @@ The jar contains a `classpath.idx` file which is used by the `JarLauncher` when
[[deployment.efficient.aot]]
=== Using Ahead-of-time Processing With the JVM
== Using Ahead-of-time Processing With the JVM
It's beneficial for the startup time to run your application using the AOT generated initialization code.
First, you need to ensure that the jar you are building includes AOT generated code.
For Maven, this means that you should build with `-Pnative` to activate the `native` profile:
[source,shell,indent=0,subs="verbatim"]
[source,shell]
----
$ mvn -Pnative package
$ mvn -Pnative package
----
For Gradle, you need to ensure that your build includes the `org.springframework.boot.aot` plugin.
When the JAR has been built, run it with `spring.aot.enabled` system property set to `true`. For example:
[source,shell,indent=0,subs="verbatim"]
[source,shell]
----
$ java -Dspring.aot.enabled=true -jar myapplication.jar
$ java -Dspring.aot.enabled=true -jar myapplication.jar
........ Starting AOT-processed MyApplication ...
........ Starting AOT-processed MyApplication ...
----
Beware that using the ahead-of-time processing has drawbacks.
@ -60,15 +62,16 @@ It implies the following restrictions:
* The classpath is fixed and fully defined at build time
* The beans defined in your application cannot change at runtime, meaning:
- The Spring `@Profile` annotation and profile-specific configuration <<howto#howto.aot.conditions,have limitations>>.
- The Spring `@Profile` annotation and profile-specific configuration xref:how-to:aot.adoc#howto.aot.conditions[have limitations].
- Properties that change if a bean is created are not supported (for example, `@ConditionalOnProperty` and `.enable` properties).
To learn more about ahead-of-time processing, please see the <<native-image#native-image.introducing-graalvm-native-images.understanding-aot-processing,Understanding Spring Ahead-of-Time Processing section>>.
To learn more about ahead-of-time processing, please see the xref:native-image/introducing-graalvm-native-images.adoc#native-image.introducing-graalvm-native-images.understanding-aot-processing[Understanding Spring Ahead-of-Time Processing section].
[[deployment.efficient.checkpoint-restore]]
=== Checkpoint and Restore With the JVM
== Checkpoint and Restore With the JVM
https://wiki.openjdk.org/display/crac/Main[Coordinated Restore at Checkpoint] (CRaC) is an OpenJDK project that defines a new Java API to allow you to checkpoint and restore an application on the HotSpot JVM.
It is based on https://github.com/checkpoint-restore/criu[CRIU], a project that implements checkpoint/restore functionality on Linux.
@ -81,4 +84,4 @@ The restored process retains all the capabilities of the HotSpot JVM, including
Based on the foundations provided by Spring Framework, Spring Boot provides support for checkpointing and restoring your application, and manages out-of-the-box the lifecycle of resources such as socket, files and thread pools https://github.com/spring-projects/spring-lifecycle-smoke-tests/blob/main/STATUS.adoc[on a limited scope].
Additional lifecycle management is expected for other dependencies and potentially for the application code dealing with such resources.
You can find more details about the two modes supported ("on demand checkpoint/restore of a running application" and "automatic checkpoint/restore at startup"), how to enable checkpoint and restore support and some guidelines in {spring-framework-docs}/integration/checkpoint-restore.html[the Spring Framework JVM Checkpoint Restore support documentation].
You can find more details about the two modes supported ("on demand checkpoint/restore of a running application" and "automatic checkpoint/restore at startup"), how to enable checkpoint and restore support and some guidelines in {url-spring-framework-docs}/integration/checkpoint-restore.html[the Spring Framework JVM Checkpoint Restore support documentation].

View File

@ -1,19 +1,8 @@
[[deployment]]
= Deploying Spring Boot Applications
include::attributes.adoc[]
Spring Boot's flexible packaging options provide a great deal of choice when it comes to deploying your application.
You can deploy Spring Boot applications to a variety of cloud platforms, to virtual/real machines, or make them fully executable for Unix systems.
This section covers some of the more common deployment scenarios.
include::deployment/cloud.adoc[]
include::deployment/installing.adoc[]
include::deployment/efficient.adoc[]
include::deployment/whats-next.adoc[]

View File

@ -1,35 +1,37 @@
[[deployment.installing]]
== Installing Spring Boot Applications
= Installing Spring Boot Applications
In addition to running Spring Boot applications by using `java -jar` directly, it is also possible to run them as `systemd`, `init.d` or Windows services.
[[deployment.installing.system-d]]
=== Installation as a systemd Service
== Installation as a systemd Service
`systemd` is the successor of the System V init system and is now being used by many modern Linux distributions.
Spring Boot applications can be launched by using `systemd` '`service`' scripts.
Assuming that you have a Spring Boot application packaged as an uber jar in `/var/myapp`, to install it as a `systemd` service, create a script named `myapp.service` and place it in `/etc/systemd/system` directory.
The following script offers an example:
[indent=0]
[source]
----
[Unit]
Description=myapp
After=syslog.target network.target
[Unit]
Description=myapp
After=syslog.target network.target
[Service]
User=myapp
Group=myapp
[Service]
User=myapp
Group=myapp
Environment="JAVA_HOME=/path/to/java/home"
Environment="JAVA_HOME=/path/to/java/home"
ExecStart=${JAVA_HOME}/bin/java -jar /var/myapp/myapp.jar
ExecStop=/bin/kill -15 $MAINPID
SuccessExitStatus=143
ExecStart=${JAVA_HOME}/bin/java -jar /var/myapp/myapp.jar
ExecStop=/bin/kill -15 $MAINPID
SuccessExitStatus=143
[Install]
WantedBy=multi-user.target
[Install]
WantedBy=multi-user.target
----
IMPORTANT: Remember to change the `Description`, `User`, `Group`, `Environment` and `ExecStart` fields for your application.
@ -41,9 +43,9 @@ Consult the https://www.freedesktop.org/software/systemd/man/systemd.service.htm
To flag the application to start automatically on system boot, use the following command:
[source,shell,indent=0,subs="verbatim"]
[source,shell]
----
$ systemctl enable myapp.service
$ systemctl enable myapp.service
----
Run `man systemctl` for more details.
@ -51,8 +53,9 @@ Run `man systemctl` for more details.
[[deployment.installing.init-d]]
=== Installation as an init.d Service (System V)
To use your application as `init.d` service, configure its build to produce a <<deployment#deployment.installing, fully executable jar>>.
== Installation as an init.d Service (System V)
To use your application as `init.d` service, configure its build to produce a xref:deployment/installing.adoc[fully executable jar].
CAUTION: Fully executable jars work by embedding an extra script at the front of the file.
Currently, some tools do not accept this format, so you may not always be able to use this technique.
@ -65,24 +68,24 @@ A standard-format jar file that contains one or more zip64-format nested jars ca
To create a '`fully executable`' jar with Maven, use the following plugin configuration:
[source,xml,indent=0,subs="verbatim"]
[source,xml]
----
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<executable>true</executable>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<executable>true</executable>
</configuration>
</plugin>
----
The following example shows the equivalent Gradle configuration:
[source,gradle,indent=0,subs="verbatim"]
[source,gradle]
----
tasks.named('bootJar') {
launchScript()
}
tasks.named('bootJar') {
launchScript()
}
----
It can then be symlinked to `init.d` to support the standard `start`, `stop`, `restart`, and `status` commands.
@ -97,17 +100,17 @@ The default scripts supports the following features:
Assuming that you have a Spring Boot application installed in `/var/myapp`, to install a Spring Boot application as an `init.d` service, create a symlink, as follows:
[source,shell,indent=0,subs="verbatim"]
[source,shell]
----
$ sudo ln -s /var/myapp/myapp.jar /etc/init.d/myapp
$ sudo ln -s /var/myapp/myapp.jar /etc/init.d/myapp
----
Once installed, you can start and stop the service in the usual way.
For example, on a Debian-based system, you could start it with the following command:
[source,shell,indent=0,subs="verbatim"]
[source,shell]
----
$ service myapp start
$ service myapp start
----
TIP: If your application fails to start, check the log file written to `/var/log/<appname>.log` for errors.
@ -115,15 +118,16 @@ TIP: If your application fails to start, check the log file written to `/var/log
You can also flag the application to start automatically by using your standard operating system tools.
For example, on Debian, you could use the following command:
[source,shell,indent=0,subs="verbatim"]
[source,shell]
----
$ update-rc.d myapp defaults <priority>
$ update-rc.d myapp defaults <priority>
----
[[deployment.installing.init-d.securing]]
==== Securing an init.d Service
=== Securing an init.d Service
NOTE: The following is a set of guidelines on how to secure a Spring Boot application that runs as an init.d service.
It is not intended to be an exhaustive list of everything that should be done to harden an application and the environment in which it runs.
@ -132,9 +136,9 @@ When the environment variable is not set, the user who owns the jar file is used
You should never run a Spring Boot application as `root`, so `RUN_AS_USER` should never be root and your application's jar file should never be owned by root.
Instead, create a specific user to run your application and set the `RUN_AS_USER` environment variable or use `chown` to make it the owner of the jar file, as shown in the following example:
[source,shell,indent=0,subs="verbatim"]
[source,shell]
----
$ chown bootapp:bootapp your-app.jar
$ chown bootapp:bootapp your-app.jar
----
In this case, the default executable script runs the application as the `bootapp` user.
@ -145,36 +149,37 @@ For example, you can set the account's shell to `/usr/sbin/nologin`.
You should also take steps to prevent the modification of your application's jar file.
Firstly, configure its permissions so that it cannot be written and can only be read or executed by its owner, as shown in the following example:
[source,shell,indent=0,subs="verbatim"]
[source,shell]
----
$ chmod 500 your-app.jar
$ chmod 500 your-app.jar
----
Second, you should also take steps to limit the damage if your application or the account that is running it is compromised.
If an attacker does gain access, they could make the jar file writable and change its contents.
One way to protect against this is to make it immutable by using `chattr`, as shown in the following example:
[source,shell,indent=0,subs="verbatim"]
[source,shell]
----
$ sudo chattr +i your-app.jar
$ sudo chattr +i your-app.jar
----
This will prevent any user, including root, from modifying the jar.
If root is used to control the application's service and you <<deployment#deployment.installing.init-d.script-customization.when-running.conf-file, use a `.conf` file>> to customize its startup, the `.conf` file is read and evaluated by the root user.
If root is used to control the application's service and you xref:deployment/installing.adoc#deployment.installing.init-d.script-customization.when-running.conf-file[use a `.conf` file] to customize its startup, the `.conf` file is read and evaluated by the root user.
It should be secured accordingly.
Use `chmod` so that the file can only be read by the owner and use `chown` to make root the owner, as shown in the following example:
[source,shell,indent=0,subs="verbatim"]
[source,shell]
----
$ chmod 400 your-app.conf
$ sudo chown root:root your-app.conf
$ chmod 400 your-app.conf
$ sudo chown root:root your-app.conf
----
[[deployment.installing.init-d.script-customization]]
==== Customizing the Startup Script
=== Customizing the Startup Script
The default embedded startup script written by the Maven or Gradle plugin can be customized in a number of ways.
For most people, using the default script along with a few customizations is usually enough.
If you find you cannot customize something that you need to, use the `embeddedLaunchScript` option to write your own file entirely.
@ -182,12 +187,13 @@ If you find you cannot customize something that you need to, use the `embeddedLa
[[deployment.installing.init-d.script-customization.when-written]]
===== Customizing the Start Script When It Is Written
==== Customizing the Start Script When It Is Written
It often makes sense to customize elements of the start script as it is written into the jar file.
For example, init.d scripts can provide a "`description`".
Since you know the description up front (and it need not change), you may as well provide it when the jar is generated.
To customize written elements, use the `embeddedLaunchScriptProperties` option of the Spring Boot Maven plugin or the {spring-boot-gradle-plugin-docs}#packaging-executable-configuring-launch-script[`properties` property of the Spring Boot Gradle plugin's `launchScript`].
To customize written elements, use the `embeddedLaunchScriptProperties` option of the Spring Boot Maven plugin or the xref:gradle-plugin:packaging.adoc#packaging-executable.configuring.launch-script[`properties` property of the Spring Boot Gradle plugin's `launchScript`].
The following property substitutions are supported with the default script:
@ -290,8 +296,9 @@ The following property substitutions are supported with the default script:
[[deployment.installing.init-d.script-customization.when-running]]
===== Customizing a Script When It Runs
For items of the script that need to be customized _after_ the jar has been written, you can use environment variables or a <<deployment#deployment.installing.init-d.script-customization.when-running.conf-file, config file>>.
==== Customizing a Script When It Runs
For items of the script that need to be customized _after_ the jar has been written, you can use environment variables or a xref:deployment/installing.adoc#deployment.installing.init-d.script-customization.when-running.conf-file[config file].
The following environment properties are supported with the default script:
@ -355,26 +362,28 @@ See the https://www.freedesktop.org/software/systemd/man/systemd.service.html[se
[[deployment.installing.init-d.script-customization.when-running.conf-file]]
====== Using a Conf File
===== Using a Conf File
With the exception of `JARFILE` and `APP_NAME`, the settings listed in the preceding section can be configured by using a `.conf` file.
The file is expected to be next to the jar file and have the same name but suffixed with `.conf` rather than `.jar`.
For example, a jar named `/var/myapp/myapp.jar` uses the configuration file named `/var/myapp/myapp.conf`, as shown in the following example:
.myapp.conf
[indent=0,subs="verbatim"]
[source,properties]
----
JAVA_OPTS=-Xmx1024M
LOG_FOLDER=/custom/log/folder
JAVA_OPTS=-Xmx1024M
LOG_FOLDER=/custom/log/folder
----
TIP: If you do not like having the config file next to the jar file, you can set a `CONF_FOLDER` environment variable to customize the location of the config file.
To learn about securing this file appropriately, see <<deployment#deployment.installing.init-d.securing,the guidelines for securing an init.d service>>.
To learn about securing this file appropriately, see xref:deployment/installing.adoc#deployment.installing.init-d.securing[the guidelines for securing an init.d service].
[[deployment.installing.windows-services]]
=== Microsoft Windows Services
== Microsoft Windows Services
A Spring Boot application can be started as a Windows service by using https://github.com/kohsuke/winsw[`winsw`].
A (https://github.com/snicoll/spring-boot-daemon[separately maintained sample]) describes step-by-step how you can create a Windows service for your Spring Boot application.

View File

@ -1,7 +1,8 @@
[[features.aop]]
== Aspect-Oriented Programming
= Aspect-Oriented Programming
Spring Boot provides auto-configuration for aspect-oriented programming (AOP).
You can learn more about AOP with Spring in the {spring-framework-docs}/core/aop-api.html[Spring Framework reference documentation].
You can learn more about AOP with Spring in the {url-spring-framework-docs}/core/aop-api.html[Spring Framework reference documentation].
By default, Spring Boot's auto-configuration configures Spring AOP to use CGLib proxies.
To use JDK proxies instead, set `configprop:spring.aop.proxy-target-class` to `false`.

View File

@ -1,35 +1,37 @@
[[features.developing-auto-configuration]]
== Creating Your Own Auto-configuration
= Creating Your Own Auto-configuration
If you work in a company that develops shared libraries, or if you work on an open-source or commercial library, you might want to develop your own auto-configuration.
Auto-configuration classes can be bundled in external jars and still be picked up by Spring Boot.
Auto-configuration can be associated to a "`starter`" that provides the auto-configuration code as well as the typical libraries that you would use with it.
We first cover what you need to know to build your own auto-configuration and then we move on to the <<features#features.developing-auto-configuration.custom-starter,typical steps required to create a custom starter>>.
We first cover what you need to know to build your own auto-configuration and then we move on to the xref:features/developing-auto-configuration.adoc#features.developing-auto-configuration.custom-starter[typical steps required to create a custom starter].
[[features.developing-auto-configuration.understanding-auto-configured-beans]]
=== Understanding Auto-configured Beans
== Understanding Auto-configured Beans
Classes that implement auto-configuration are annotated with `@AutoConfiguration`.
This annotation itself is meta-annotated with `@Configuration`, making auto-configurations standard `@Configuration` classes.
Additional `@Conditional` annotations are used to constrain when the auto-configuration should apply.
Usually, auto-configuration classes use `@ConditionalOnClass` and `@ConditionalOnMissingBean` annotations.
This ensures that auto-configuration applies only when relevant classes are found and when you have not declared your own `@Configuration`.
You can browse the source code of {spring-boot-autoconfigure-module-code}[`spring-boot-autoconfigure`] to see the `@AutoConfiguration` classes that Spring provides (see the {spring-boot-code}/spring-boot-project/spring-boot-autoconfigure/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports[`META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports`] file).
You can browse the source code of {code-spring-boot-autoconfigure-src}[`spring-boot-autoconfigure`] to see the `@AutoConfiguration` classes that Spring provides (see the {code-spring-boot}/spring-boot-project/spring-boot-autoconfigure/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports[`META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports`] file).
[[features.developing-auto-configuration.locating-auto-configuration-candidates]]
=== Locating Auto-configuration Candidates
== Locating Auto-configuration Candidates
Spring Boot checks for the presence of a `META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports` file within your published jar.
The file should list your configuration classes, with one class name per line, as shown in the following example:
[indent=0]
[source]
----
com.mycorp.libx.autoconfigure.LibXAutoConfiguration
com.mycorp.libx.autoconfigure.LibXWebAutoConfiguration
com.mycorp.libx.autoconfigure.LibXAutoConfiguration
com.mycorp.libx.autoconfigure.LibXWebAutoConfiguration
----
TIP: You can add comments to the imports file using the `#` character.
@ -39,7 +41,7 @@ Make sure that they are defined in a specific package space and that they are ne
Furthermore, auto-configuration classes should not enable component scanning to find additional components.
Specific `@Import` annotations should be used instead.
If your configuration needs to be applied in a specific order, you can use the `before`, `beforeName`, `after` and `afterName` attributes on the {spring-boot-autoconfigure-module-code}/AutoConfiguration.java[`@AutoConfiguration`] annotation or the dedicated {spring-boot-autoconfigure-module-code}/AutoConfigureBefore.java[`@AutoConfigureBefore`] and {spring-boot-autoconfigure-module-code}/AutoConfigureAfter.java[`@AutoConfigureAfter`] annotations.
If your configuration needs to be applied in a specific order, you can use the `before`, `beforeName`, `after` and `afterName` attributes on the {code-spring-boot-autoconfigure-src}/AutoConfiguration.java[`@AutoConfiguration`] annotation or the dedicated {code-spring-boot-autoconfigure-src}/AutoConfigureBefore.java[`@AutoConfigureBefore`] and {code-spring-boot-autoconfigure-src}/AutoConfigureAfter.java[`@AutoConfigureAfter`] annotations.
For example, if you provide web-specific configuration, your class may need to be applied after `WebMvcAutoConfiguration`.
If you want to order certain auto-configurations that should not have any direct knowledge of each other, you can also use `@AutoConfigureOrder`.
@ -51,24 +53,26 @@ The order in which those beans are subsequently created is unaffected and is det
[[features.developing-auto-configuration.condition-annotations]]
=== Condition Annotations
== Condition Annotations
You almost always want to include one or more `@Conditional` annotations on your auto-configuration class.
The `@ConditionalOnMissingBean` annotation is one common example that is used to allow developers to override auto-configuration if they are not happy with your defaults.
Spring Boot includes a number of `@Conditional` annotations that you can reuse in your own code by annotating `@Configuration` classes or individual `@Bean` methods.
These annotations include:
* <<features#features.developing-auto-configuration.condition-annotations.class-conditions>>
* <<features#features.developing-auto-configuration.condition-annotations.bean-conditions>>
* <<features#features.developing-auto-configuration.condition-annotations.property-conditions>>
* <<features#features.developing-auto-configuration.condition-annotations.resource-conditions>>
* <<features#features.developing-auto-configuration.condition-annotations.web-application-conditions>>
* <<features#features.developing-auto-configuration.condition-annotations.spel-conditions>>
* xref:features/developing-auto-configuration.adoc#features.developing-auto-configuration.condition-annotations.class-conditions[Class Conditions]
* xref:features/developing-auto-configuration.adoc#features.developing-auto-configuration.condition-annotations.bean-conditions[Bean Conditions]
* xref:features/developing-auto-configuration.adoc#features.developing-auto-configuration.condition-annotations.property-conditions[Property Conditions]
* xref:features/developing-auto-configuration.adoc#features.developing-auto-configuration.condition-annotations.resource-conditions[Resource Conditions]
* xref:features/developing-auto-configuration.adoc#features.developing-auto-configuration.condition-annotations.web-application-conditions[Web Application Conditions]
* xref:features/developing-auto-configuration.adoc#features.developing-auto-configuration.condition-annotations.spel-conditions[SpEL Expression Conditions]
[[features.developing-auto-configuration.condition-annotations.class-conditions]]
==== Class Conditions
=== Class Conditions
The `@ConditionalOnClass` and `@ConditionalOnMissingClass` annotations let `@Configuration` classes be included based on the presence or absence of specific classes.
Due to the fact that annotation metadata is parsed by using https://asm.ow2.io/[ASM], you can use the `value` attribute to refer to the real class, even though that class might not actually appear on the running application classpath.
You can also use the `name` attribute if you prefer to specify the class name by using a `String` value.
@ -77,21 +81,22 @@ This mechanism does not apply the same way to `@Bean` methods where typically th
To handle this scenario, a separate `@Configuration` class can be used to isolate the condition, as shown in the following example:
include::code:MyAutoConfiguration[]
include-code::MyAutoConfiguration[]
TIP: If you use `@ConditionalOnClass` or `@ConditionalOnMissingClass` as a part of a meta-annotation to compose your own composed annotations, you must use `name` as referring to the class in such a case is not handled.
[[features.developing-auto-configuration.condition-annotations.bean-conditions]]
==== Bean Conditions
=== Bean Conditions
The `@ConditionalOnBean` and `@ConditionalOnMissingBean` annotations let a bean be included based on the presence or absence of specific beans.
You can use the `value` attribute to specify beans by type or `name` to specify beans by name.
The `search` attribute lets you limit the `ApplicationContext` hierarchy that should be considered when searching for beans.
When placed on a `@Bean` method, the target type defaults to the return type of the method, as shown in the following example:
include::code:MyAutoConfiguration[]
include-code::MyAutoConfiguration[]
In the preceding example, the `someService` bean is going to be created if no bean of type `SomeService` is already contained in the `ApplicationContext`.
@ -108,7 +113,8 @@ Providing as much type information as possible in `@Bean` methods is particularl
[[features.developing-auto-configuration.condition-annotations.property-conditions]]
==== Property Conditions
=== Property Conditions
The `@ConditionalOnProperty` annotation lets configuration be included based on a Spring Environment property.
Use the `prefix` and `name` attributes to specify the property that should be checked.
By default, any property that exists and is not equal to `false` is matched.
@ -117,14 +123,16 @@ You can also create more advanced checks by using the `havingValue` and `matchIf
[[features.developing-auto-configuration.condition-annotations.resource-conditions]]
==== Resource Conditions
=== Resource Conditions
The `@ConditionalOnResource` annotation lets configuration be included only when a specific resource is present.
Resources can be specified by using the usual Spring conventions, as shown in the following example: `file:/home/user/test.dat`.
[[features.developing-auto-configuration.condition-annotations.web-application-conditions]]
==== Web Application Conditions
=== Web Application Conditions
The `@ConditionalOnWebApplication` and `@ConditionalOnNotWebApplication` annotations let configuration be included depending on whether the application is a web application.
A servlet-based web application is any application that uses a Spring `WebApplicationContext`, defines a `session` scope, or has a `ConfigurableWebEnvironment`.
A reactive web application is any application that uses a `ReactiveWebApplicationContext`, or has a `ConfigurableReactiveWebEnvironment`.
@ -135,8 +143,9 @@ This condition will not match for applications that are run with an embedded web
[[features.developing-auto-configuration.condition-annotations.spel-conditions]]
==== SpEL Expression Conditions
The `@ConditionalOnExpression` annotation lets configuration be included based on the result of a {spring-framework-docs}/core/expressions.html[SpEL expression].
=== SpEL Expression Conditions
The `@ConditionalOnExpression` annotation lets configuration be included based on the result of a {url-spring-framework-docs}/core/expressions.html[SpEL expression].
NOTE: Referencing a bean in the expression will cause that bean to be initialized very early in context refresh processing.
As a result, the bean won't be eligible for post-processing (such as configuration properties binding) and its state may be incomplete.
@ -144,7 +153,8 @@ As a result, the bean won't be eligible for post-processing (such as configurati
[[features.developing-auto-configuration.testing]]
=== Testing your Auto-configuration
== Testing your Auto-configuration
An auto-configuration can be affected by many factors: user configuration (`@Bean` definition and `Environment` customization), condition evaluation (presence of a particular library), and others.
Concretely, each test should create a well defined `ApplicationContext` that represents a combination of those customizations.
`ApplicationContextRunner` provides a great way to achieve that.
@ -154,7 +164,7 @@ WARNING: `ApplicationContextRunner` doesn't work when running the tests in a nat
`ApplicationContextRunner` is usually defined as a field of the test class to gather the base, common configuration.
The following example makes sure that `MyServiceAutoConfiguration` is always invoked:
include::code:MyServiceAutoConfigurationTests[tag=runner]
include-code::MyServiceAutoConfigurationTests[tag=runner]
TIP: If multiple auto-configurations have to be defined, there is no need to order their declarations as they are invoked in the exact same order as when running the application.
@ -162,38 +172,41 @@ Each test can use the runner to represent a particular use case.
For instance, the sample below invokes a user configuration (`UserConfiguration`) and checks that the auto-configuration backs off properly.
Invoking `run` provides a callback context that can be used with `AssertJ`.
include::code:MyServiceAutoConfigurationTests[tag=test-user-config]
include-code::MyServiceAutoConfigurationTests[tag=test-user-config]
It is also possible to easily customize the `Environment`, as shown in the following example:
include::code:MyServiceAutoConfigurationTests[tag=test-env]
include-code::MyServiceAutoConfigurationTests[tag=test-env]
The runner can also be used to display the `ConditionEvaluationReport`.
The report can be printed at `INFO` or `DEBUG` level.
The following example shows how to use the `ConditionEvaluationReportLoggingListener` to print the report in auto-configuration tests.
include::code:MyConditionEvaluationReportingTests[]
include-code::MyConditionEvaluationReportingTests[]
[[features.developing-auto-configuration.testing.simulating-a-web-context]]
==== Simulating a Web Context
=== Simulating a Web Context
If you need to test an auto-configuration that only operates in a servlet or reactive web application context, use the `WebApplicationContextRunner` or `ReactiveWebApplicationContextRunner` respectively.
[[features.developing-auto-configuration.testing.overriding-classpath]]
==== Overriding the Classpath
=== Overriding the Classpath
It is also possible to test what happens when a particular class and/or package is not present at runtime.
Spring Boot ships with a `FilteredClassLoader` that can easily be used by the runner.
In the following example, we assert that if `MyService` is not present, the auto-configuration is properly disabled:
include::code:../MyServiceAutoConfigurationTests[tag=test-classloader]
include-code::../MyServiceAutoConfigurationTests[tag=test-classloader]
[[features.developing-auto-configuration.custom-starter]]
=== Creating Your Own Starter
== Creating Your Own Starter
A typical Spring Boot starter contains code to auto-configure and customize the infrastructure of a given technology, let's call that "acme".
To make it easily extensible, a number of configuration keys in a dedicated namespace can be exposed to the environment.
Finally, a single "starter" dependency is provided to help users get started as easily as possible.
@ -214,7 +227,8 @@ If the auto-configuration is relatively straightforward and does not have option
[[features.developing-auto-configuration.custom-starter.naming]]
==== Naming
=== Naming
You should make sure to provide a proper namespace for your starter.
Do not start your module names with `spring-boot`, even if you use a different Maven `groupId`.
We may offer official support for the thing you auto-configure in the future.
@ -226,7 +240,8 @@ If you only have one module that combines the two, name it `acme-spring-boot-sta
[[features.developing-auto-configuration.custom-starter.configuration-keys]]
==== Configuration keys
=== Configuration keys
If your starter provides configuration keys, use a unique namespace for them.
In particular, do not include your keys in the namespaces that Spring Boot uses (such as `server`, `management`, `spring`, and so on).
If you use the same namespace, we may modify these namespaces in the future in ways that break your modules.
@ -234,7 +249,7 @@ As a rule of thumb, prefix all your keys with a namespace that you own (for exam
Make sure that configuration keys are documented by adding field javadoc for each property, as shown in the following example:
include::code:AcmeProperties[]
include-code::AcmeProperties[]
NOTE: You should only use plain text with `@ConfigurationProperties` field Javadoc, since they are not processed before being added to the JSON.
@ -246,14 +261,15 @@ Here are some rules we follow internally to make sure descriptions are consisten
* Use `java.time.Duration` rather than `long` and describe the default unit if it differs from milliseconds, such as "If a duration suffix is not specified, seconds will be used".
* Do not provide the default value in the description unless it has to be determined at runtime.
Make sure to <<configuration-metadata#appendix.configuration-metadata.annotation-processor,trigger meta-data generation>> so that IDE assistance is available for your keys as well.
Make sure to xref:specification:configuration-metadata/annotation-processor.adoc[trigger meta-data generation] so that IDE assistance is available for your keys as well.
You may want to review the generated metadata (`META-INF/spring-configuration-metadata.json`) to make sure your keys are properly documented.
Using your own starter in a compatible IDE is also a good idea to validate that quality of the metadata.
[[features.developing-auto-configuration.custom-starter.autoconfigure-module]]
==== The "`autoconfigure`" Module
=== The "`autoconfigure`" Module
The `autoconfigure` module contains everything that is necessary to get started with the library.
It may also contain configuration key definitions (such as `@ConfigurationProperties`) and any callback interface that can be used to further customize how the components are initialized.
@ -265,52 +281,53 @@ If that file is present, it is used to eagerly filter auto-configurations that d
When building with Maven, it is recommended to add the following dependency in a module that contains auto-configurations:
[source,xml,indent=0,subs="verbatim"]
[source,xml]
----
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure-processor</artifactId>
<optional>true</optional>
</dependency>
----
If you have defined auto-configurations directly in your application, make sure to configure the `spring-boot-maven-plugin` to prevent the `repackage` goal from adding the dependency into the uber jar:
[source,xml,indent=0,subs="verbatim"]
[source,xml]
----
<project>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure-processor</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
<project>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure-processor</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
----
With Gradle, the dependency should be declared in the `annotationProcessor` configuration, as shown in the following example:
[source,gradle,indent=0,subs="verbatim"]
[source,gradle]
----
dependencies {
annotationProcessor "org.springframework.boot:spring-boot-autoconfigure-processor"
}
dependencies {
annotationProcessor "org.springframework.boot:spring-boot-autoconfigure-processor"
}
----
[[features.developing-auto-configuration.custom-starter.starter-module]]
==== Starter Module
=== Starter Module
The starter is really an empty jar.
Its only purpose is to provide the necessary dependencies to work with the library.
You can think of it as an opinionated view of what is required to get started.

View File

@ -1,5 +1,6 @@
[[features.docker-compose]]
== Docker Compose Support
= Docker Compose Support
Docker Compose is a popular technology that can be used to define and manage multiple containers for services that your application needs.
A `compose.yml` file is typically created next to your application which defines and configures service containers.
@ -9,23 +10,23 @@ The `spring-boot-docker-compose` module can be included in a project to provide
Add the module dependency to your build, as shown in the following listings for Maven and Gradle:
.Maven
[source,xml,indent=0,subs="verbatim"]
[source,xml]
----
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-docker-compose</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-docker-compose</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
----
.Gradle
[source,gradle,indent=0,subs="verbatim"]
[source,gradle]
----
dependencies {
developmentOnly("org.springframework.boot:spring-boot-docker-compose")
}
dependencies {
developmentOnly("org.springframework.boot:spring-boot-docker-compose")
}
----
When this module is included as a dependency Spring Boot will do the following:
@ -44,14 +45,16 @@ To enable it, set configprop:spring.docker.compose.skip.in-tests[] to `false`.
[[features.docker-compose.prerequisites]]
=== Prerequisites
== Prerequisites
You need to have the `docker` and `docker compose` (or `docker-compose`) CLI applications on your path.
The minimum supported Docker Compose version is 2.2.0.
[[features.docker-compose.service-connections]]
=== Service Connections
== Service Connections
A service connection is a connection to any remote service.
Spring Boots auto-configuration can consume the details of a service connection and use them to establish a connection to a remote service.
When doing so, the connection details take precedence over any connection-related configuration properties.
@ -118,7 +121,8 @@ The following service connections are currently supported:
[[features.docker-compose.custom-images]]
=== Custom Images
== Custom Images
Sometimes you may need to use your own version of an image to provide a service.
You can use any custom image as long as it behaves in the same way as the standard image.
Specifically, any environment variables that the standard image supports must also be used in your custom image.
@ -128,58 +132,61 @@ Use a label named `org.springframework.boot.service-connection` to provide the s
For example:
[source,yaml,indent=0]
[source,yaml,]
----
services:
redis:
image: 'mycompany/mycustomredis:7.0'
ports:
- '6379'
labels:
org.springframework.boot.service-connection: redis
services:
redis:
image: 'mycompany/mycustomredis:7.0'
ports:
- '6379'
labels:
org.springframework.boot.service-connection: redis
----
[[features.docker-compose.skipping]]
=== Skipping Specific Containers
== Skipping Specific Containers
If you have a container image defined in your `compose.yml` that you dont want connected to your application you can use a label to ignore it.
Any container with labeled with `org.springframework.boot.ignore` will be ignored by Spring Boot.
For example:
[source,yaml,indent=0]
[source,yaml]
----
services:
redis:
image: 'redis:7.0'
ports:
- '6379'
labels:
org.springframework.boot.ignore: true
services:
redis:
image: 'redis:7.0'
ports:
- '6379'
labels:
org.springframework.boot.ignore: true
----
[[features.docker-compose.specific-file]]
=== Using a Specific Compose File
== Using a Specific Compose File
If your compose file is not in the same directory as your application, or if its named differently, you can use configprop:spring.docker.compose.file[] in your `application.properties` or `application.yaml` to point to a different file.
Properties can be defined as an exact path or a path thats relative to your application.
For example:
[source,yaml,indent=0,subs="verbatim",configprops,configblocks]
[configprops,yaml]
----
spring:
docker:
compose:
file: "../my-compose.yml"
spring:
docker:
compose:
file: "../my-compose.yml"
----
[[features.docker-compose.readiness]]
=== Waiting for Container Readiness
== Waiting for Container Readiness
Containers started by Docker Compose may take some time to become fully ready.
The recommended way of checking for readiness is to add a `healthcheck` section under the service definition in your `compose.yml` file.
@ -190,28 +197,28 @@ You can disable this on a per-container basis by adding a `org.springframework.b
For example:
[source,yaml,indent=0]
[source,yaml]
----
services:
redis:
image: 'redis:7.0'
ports:
- '6379'
labels:
org.springframework.boot.readiness-check.tcp.disable: true
services:
redis:
image: 'redis:7.0'
ports:
- '6379'
labels:
org.springframework.boot.readiness-check.tcp.disable: true
----
You can also change timeout values in your `application.properties` or `application.yaml` file:
[source,yaml,indent=0,subs="verbatim",configprops,configblocks]
[configprops,yaml]
----
spring:
docker:
compose:
readiness:
tcp:
connect-timeout: 10s
read-timeout: 5s
spring:
docker:
compose:
readiness:
tcp:
connect-timeout: 10s
read-timeout: 5s
----
The overall timeout can be configured using configprop:spring.docker.compose.readiness.timeout[].
@ -219,7 +226,8 @@ The overall timeout can be configured using configprop:spring.docker.compose.rea
[[features.docker-compose.lifecycle]]
=== Controlling the Docker Compose Lifecycle
== Controlling the Docker Compose Lifecycle
By default Spring Boot calls `docker compose up` when your application starts and `docker compose stop` when it's shut down.
If you prefer to have different lifecycle management you can use the configprop:spring.docker.compose.lifecycle-management[] property.
@ -234,31 +242,32 @@ The configprop:spring.docker.compose.stop.command[] allows you to configure if `
The following example shows how lifecycle management can be configured:
[source,yaml,indent=0,subs="verbatim",configprops,configblocks]
[configprops,yaml]
----
spring:
docker:
compose:
lifecycle-management: start-and-stop
start:
command: start
stop:
command: down
timeout: 1m
spring:
docker:
compose:
lifecycle-management: start-and-stop
start:
command: start
stop:
command: down
timeout: 1m
----
[[features.docker-compose.profiles]]
=== Activating Docker Compose Profiles
== Activating Docker Compose Profiles
Docker Compose profiles are similar to Spring profiles in that they let you adjust your Docker Compose configuration for specific environments.
If you want to activate a specific Docker Compose profile you can use the configprop:spring.docker.compose.profiles.active[] property in your `application.properties` or `application.yaml` file:
[source,yaml,indent=0,subs="verbatim",configprops,configblocks]
[configprops,yaml]
----
spring:
docker:
compose:
profiles:
active: "myprofile"
spring:
docker:
compose:
profiles:
active: "myprofile"
----

View File

@ -0,0 +1,7 @@
[[features]]
= Core Features
This section dives into the details of Spring Boot.
Here you can learn about the key features that you may want to use and customize.
If you have not already done so, you might want to read the "xref:tutorial:index.adoc[Tutoral]" and "xref:using/index.adoc[Developing with Spring Boot]" sections, so that you have a good grounding of the basics.

View File

@ -1,5 +1,6 @@
[[features.internationalization]]
== Internationalization
= Internationalization
Spring Boot supports localized messages so that your application can cater to users of different language preferences.
By default, Spring Boot looks for the presence of a `messages` resource bundle at the root of the classpath.
@ -9,14 +10,14 @@ If no properties file is found that matches any of the configured base names, th
The basename of the resource bundle as well as several other attributes can be configured using the `spring.messages` namespace, as shown in the following example:
[source,yaml,indent=0,subs="verbatim",configprops,configblocks]
[configprops,yaml]
----
spring:
messages:
basename: "messages,config.i18n.messages"
fallback-to-system-locale: false
spring:
messages:
basename: "messages,config.i18n.messages"
fallback-to-system-locale: false
----
TIP: `spring.messages.basename` supports comma-separated list of locations, either a package qualifier or a resource resolved from the classpath root.
See {spring-boot-autoconfigure-module-code}/context/MessageSourceProperties.java[`MessageSourceProperties`] for more supported options.
See {code-spring-boot-autoconfigure-src}/context/MessageSourceProperties.java[`MessageSourceProperties`] for more supported options.

View File

@ -1,5 +1,6 @@
[[features.json]]
== JSON
= JSON
Spring Boot provides integration with three JSON mapping libraries:
- Gson
@ -11,37 +12,40 @@ Jackson is the preferred and default library.
[[features.json.jackson]]
=== Jackson
== Jackson
Auto-configuration for Jackson is provided and Jackson is part of `spring-boot-starter-json`.
When Jackson is on the classpath an `ObjectMapper` bean is automatically configured.
Several configuration properties are provided for <<howto#howto.spring-mvc.customize-jackson-objectmapper,customizing the configuration of the `ObjectMapper`>>.
Several configuration properties are provided for xref:how-to:spring-mvc.adoc#howto.spring-mvc.customize-jackson-objectmapper[customizing the configuration of the `ObjectMapper`].
[[features.json.jackson.custom-serializers-and-deserializers]]
==== Custom Serializers and Deserializers
=== Custom Serializers and Deserializers
If you use Jackson to serialize and deserialize JSON data, you might want to write your own `JsonSerializer` and `JsonDeserializer` classes.
Custom serializers are usually https://github.com/FasterXML/jackson-docs/wiki/JacksonHowToCustomSerializers[registered with Jackson through a module], but Spring Boot provides an alternative `@JsonComponent` annotation that makes it easier to directly register Spring Beans.
You can use the `@JsonComponent` annotation directly on `JsonSerializer`, `JsonDeserializer` or `KeyDeserializer` implementations.
You can also use it on classes that contain serializers/deserializers as inner classes, as shown in the following example:
include::code:MyJsonComponent[]
include-code::MyJsonComponent[]
All `@JsonComponent` beans in the `ApplicationContext` are automatically registered with Jackson.
Because `@JsonComponent` is meta-annotated with `@Component`, the usual component-scanning rules apply.
Spring Boot also provides {spring-boot-module-code}/jackson/JsonObjectSerializer.java[`JsonObjectSerializer`] and {spring-boot-module-code}/jackson/JsonObjectDeserializer.java[`JsonObjectDeserializer`] base classes that provide useful alternatives to the standard Jackson versions when serializing objects.
See {spring-boot-module-api}/jackson/JsonObjectSerializer.html[`JsonObjectSerializer`] and {spring-boot-module-api}/jackson/JsonObjectDeserializer.html[`JsonObjectDeserializer`] in the Javadoc for details.
Spring Boot also provides {code-spring-boot-src}/jackson/JsonObjectSerializer.java[`JsonObjectSerializer`] and {code-spring-boot-src}/jackson/JsonObjectDeserializer.java[`JsonObjectDeserializer`] base classes that provide useful alternatives to the standard Jackson versions when serializing objects.
See xref:api:java/org/springframework/boot/jackson/JsonObjectSerializer.html[`JsonObjectSerializer`] and xref:api:java/org/springframework/boot/jackson/JsonObjectDeserializer.html[`JsonObjectDeserializer`] in the Javadoc for details.
The example above can be rewritten to use `JsonObjectSerializer`/`JsonObjectDeserializer` as follows:
include::code:object/MyJsonComponent[]
include-code::object/MyJsonComponent[]
[[features.json.jackson.mixins]]
==== Mixins
=== Mixins
Jackson has support for mixins that can be used to mix additional annotations into those already declared on a target class.
Spring Boot's Jackson auto-configuration will scan your application's packages for classes annotated with `@JsonMixin` and register them with the auto-configured `ObjectMapper`.
The registration is performed by Spring Boot's `JsonMixinModule`.
@ -49,7 +53,8 @@ The registration is performed by Spring Boot's `JsonMixinModule`.
[[features.json.gson]]
=== Gson
== Gson
Auto-configuration for Gson is provided.
When Gson is on the classpath a `Gson` bean is automatically configured.
Several `+spring.gson.*+` configuration properties are provided for customizing the configuration.
@ -58,7 +63,8 @@ To take more control, one or more `GsonBuilderCustomizer` beans can be used.
[[features.json.json-b]]
=== JSON-B
== JSON-B
Auto-configuration for JSON-B is provided.
When the JSON-B API and an implementation are on the classpath a `Jsonb` bean will be automatically configured.
The preferred JSON-B implementation is Eclipse Yasson for which dependency management is provided.

View File

@ -1,9 +1,10 @@
[[features.kotlin]]
== Kotlin Support
https://kotlinlang.org[Kotlin] is a statically-typed language targeting the JVM (and other platforms) which allows writing concise and elegant code while providing {kotlin-docs}java-interop.html[interoperability] with existing libraries written in Java.
= Kotlin Support
https://kotlinlang.org[Kotlin] is a statically-typed language targeting the JVM (and other platforms) which allows writing concise and elegant code while providing {url-kotlin-docs}/java-interop.html[interoperability] with existing libraries written in Java.
Spring Boot provides Kotlin support by leveraging the support in other Spring projects such as Spring Framework, Spring Data, and Reactor.
See the {spring-framework-docs}/languages/kotlin.html[Spring Framework Kotlin support documentation] for more information.
See the {url-spring-framework-docs}/languages/kotlin.html[Spring Framework Kotlin support documentation] for more information.
The easiest way to start with Spring Boot and Kotlin is to follow https://spring.io/guides/tutorials/spring-boot-kotlin/[this comprehensive tutorial].
You can create new Kotlin projects by using https://start.spring.io/#!language=kotlin[start.spring.io].
@ -12,12 +13,13 @@ Feel free to join the #spring channel of https://slack.kotlinlang.org/[Kotlin Sl
[[features.kotlin.requirements]]
=== Requirements
== Requirements
Spring Boot requires at least Kotlin 1.7.x and manages a suitable Kotlin version through dependency management.
To use Kotlin, `org.jetbrains.kotlin:kotlin-stdlib` and `org.jetbrains.kotlin:kotlin-reflect` must be present on the classpath.
The `kotlin-stdlib` variants `kotlin-stdlib-jdk7` and `kotlin-stdlib-jdk8` can also be used.
Since https://discuss.kotlinlang.org/t/classes-final-by-default/166[Kotlin classes are final by default], you are likely to want to configure {kotlin-docs}compiler-plugins.html#spring-support[kotlin-spring] plugin in order to automatically open Spring-annotated classes so that they can be proxied.
Since https://discuss.kotlinlang.org/t/classes-final-by-default/166[Kotlin classes are final by default], you are likely to want to configure {url-kotlin-docs}/compiler-plugins.html#spring-support[kotlin-spring] plugin in order to automatically open Spring-annotated classes so that they can be proxied.
https://github.com/FasterXML/jackson-module-kotlin[Jackson's Kotlin module] is required for serializing / deserializing JSON data in Kotlin.
It is automatically registered when found on the classpath.
@ -28,15 +30,16 @@ TIP: These dependencies and plugins are provided by default if one bootstraps a
[[features.kotlin.null-safety]]
=== Null-safety
One of Kotlin's key features is {kotlin-docs}null-safety.html[null-safety].
== Null-safety
One of Kotlin's key features is {url-kotlin-docs}/null-safety.html[null-safety].
It deals with `null` values at compile time rather than deferring the problem to runtime and encountering a `NullPointerException`.
This helps to eliminate a common source of bugs without paying the cost of wrappers like `Optional`.
Kotlin also allows using functional constructs with nullable values as described in this https://www.baeldung.com/kotlin-null-safety[comprehensive guide to null-safety in Kotlin].
Although Java does not allow one to express null-safety in its type system, Spring Framework, Spring Data, and Reactor now provide null-safety of their API through tooling-friendly annotations.
By default, types from Java APIs used in Kotlin are recognized as {kotlin-docs}java-interop.html#null-safety-and-platform-types[platform types] for which null-checks are relaxed.
{kotlin-docs}java-interop.html#jsr-305-support[Kotlin's support for JSR 305 annotations] combined with nullability annotations provide null-safety for the related Spring API in Kotlin.
By default, types from Java APIs used in Kotlin are recognized as {url-kotlin-docs}/java-interop.html#null-safety-and-platform-types[platform types] for which null-checks are relaxed.
{url-kotlin-docs}/java-interop.html#jsr-305-support[Kotlin's support for JSR 305 annotations] combined with nullability annotations provide null-safety for the related Spring API in Kotlin.
The JSR 305 checks can be configured by adding the `-Xjsr305` compiler flag with the following options: `-Xjsr305={strict|warn|ignore}`.
The default behavior is the same as `-Xjsr305=warn`.
@ -44,47 +47,49 @@ The `strict` value is required to have null-safety taken in account in Kotlin ty
WARNING: Generic type arguments, varargs and array elements nullability are not yet supported.
See https://jira.spring.io/browse/SPR-15942[SPR-15942] for up-to-date information.
Also be aware that Spring Boot's own API is {github-issues}10712[not yet annotated].
Also be aware that Spring Boot's own API is {url-github-issues}/10712[not yet annotated].
[[features.kotlin.api]]
=== Kotlin API
== Kotlin API
[[features.kotlin.api.run-application]]
==== runApplication
=== runApplication
Spring Boot provides an idiomatic way to run an application with `runApplication<MyApplication>(*args)` as shown in the following example:
[source,kotlin,indent=0,subs="verbatim"]
[source,kotlin]
----
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
@SpringBootApplication
class MyApplication
@SpringBootApplication
class MyApplication
fun main(args: Array<String>) {
runApplication<MyApplication>(*args)
}
fun main(args: Array<String>) {
runApplication<MyApplication>(*args)
}
----
This is a drop-in replacement for `SpringApplication.run(MyApplication::class.java, *args)`.
It also allows customization of the application as shown in the following example:
[source,kotlin,indent=0,subs="verbatim"]
[source,kotlin]
----
runApplication<MyApplication>(*args) {
setBannerMode(OFF)
}
runApplication<MyApplication>(*args) {
setBannerMode(OFF)
}
----
[[features.kotlin.api.extensions]]
==== Extensions
Kotlin {kotlin-docs}extensions.html[extensions] provide the ability to extend existing classes with additional functionality.
=== Extensions
Kotlin {url-kotlin-docs}/extensions.html[extensions] provide the ability to extend existing classes with additional functionality.
The Spring Boot Kotlin API makes use of these extensions to add new Kotlin specific conveniences to existing APIs.
`TestRestTemplate` extensions, similar to those provided by Spring Framework for `RestOperations` in Spring Framework, are provided.
@ -93,7 +98,8 @@ Among other things, the extensions make it possible to take advantage of Kotlin
[[features.kotlin.dependency-management]]
=== Dependency management
== Dependency management
In order to avoid mixing different versions of Kotlin dependencies on the classpath, Spring Boot imports the Kotlin BOM.
With Maven, the Kotlin version can be customized by setting the `kotlin.version` property and plugin management is provided for `kotlin-maven-plugin`.
@ -107,10 +113,11 @@ TIP: `org.jetbrains.kotlinx:kotlinx-coroutines-reactor` dependency is provided b
[[features.kotlin.configuration-properties]]
=== @ConfigurationProperties
`@ConfigurationProperties` when used in combination with <<features#features.external-config.typesafe-configuration-properties.constructor-binding,constructor binding>> supports classes with immutable `val` properties as shown in the following example:
== @ConfigurationProperties
[source,kotlin,indent=0,subs="verbatim"]
`@ConfigurationProperties` when used in combination with xref:features/external-config.adoc#features.external-config.typesafe-configuration-properties.constructor-binding[constructor binding] supports classes with immutable `val` properties as shown in the following example:
[source,kotlin]
----
@ConfigurationProperties("example.kotlin")
data class KotlinExampleProperties(
@ -125,30 +132,32 @@ data class KotlinExampleProperties(
}
----
TIP: To generate <<configuration-metadata#appendix.configuration-metadata.annotation-processor,your own metadata>> using the annotation processor, {kotlin-docs}kapt.html[`kapt` should be configured] with the `spring-boot-configuration-processor` dependency.
TIP: To generate xref:specification:configuration-metadata/annotation-processor.adoc[your own metadata] using the annotation processor, {url-kotlin-docs}/kapt.html[`kapt` should be configured] with the `spring-boot-configuration-processor` dependency.
Note that some features (such as detecting the default value or deprecated items) are not working due to limitations in the model kapt provides.
[[features.kotlin.testing]]
=== Testing
== Testing
While it is possible to use JUnit 4 to test Kotlin code, JUnit 5 is provided by default and is recommended.
JUnit 5 enables a test class to be instantiated once and reused for all of the class's tests.
This makes it possible to use `@BeforeAll` and `@AfterAll` annotations on non-static methods, which is a good fit for Kotlin.
To mock Kotlin classes, https://mockk.io/[MockK] is recommended.
If you need the `MockK` equivalent of the Mockito specific <<features#features.testing.spring-boot-applications.mocking-beans,`@MockBean` and `@SpyBean` annotations>>, you can use https://github.com/Ninja-Squad/springmockk[SpringMockK] which provides similar `@MockkBean` and `@SpykBean` annotations.
If you need the `MockK` equivalent of the Mockito specific xref:features/testing.adoc#features.testing.spring-boot-applications.mocking-beans[`@MockBean` and `@SpyBean` annotations], you can use https://github.com/Ninja-Squad/springmockk[SpringMockK] which provides similar `@MockkBean` and `@SpykBean` annotations.
[[features.kotlin.resources]]
=== Resources
== Resources
[[features.kotlin.resources.further-reading]]
==== Further reading
* {kotlin-docs}[Kotlin language reference]
=== Further reading
* {url-kotlin-docs}[Kotlin language reference]
* https://kotlinlang.slack.com/[Kotlin Slack] (with a dedicated #spring channel)
* https://stackoverflow.com/questions/tagged/spring+kotlin[Stack Overflow with `spring` and `kotlin` tags]
* https://try.kotlinlang.org/[Try Kotlin in your browser]
@ -163,7 +172,8 @@ If you need the `MockK` equivalent of the Mockito specific <<features#features.t
[[features.kotlin.resources.examples]]
==== Examples
=== Examples
* https://github.com/sdeleuze/spring-boot-kotlin-demo[spring-boot-kotlin-demo]: regular Spring Boot + Spring Data JPA project
* https://github.com/mixitconf/mixit[mixit]: Spring Boot 2 + WebFlux + Reactive Spring Data MongoDB
* https://github.com/sdeleuze/spring-kotlin-fullstack[spring-kotlin-fullstack]: WebFlux Kotlin fullstack example with Kotlin2js for frontend instead of JavaScript or TypeScript

View File

@ -1,7 +1,8 @@
[[features.logging]]
== Logging
= Logging
Spring Boot uses https://commons.apache.org/logging[Commons Logging] for all internal logging but leaves the underlying log implementation open.
Default configurations are provided for {java-api}/java.logging/java/util/logging/package-summary.html[Java Util Logging], https://logging.apache.org/log4j/2.x/[Log4j2], and https://logback.qos.ch/[Logback].
Default configurations are provided for {apiref-openjdk}/java.logging/java/util/logging/package-summary.html[Java Util Logging], https://logging.apache.org/log4j/2.x/[Log4j2], and https://logback.qos.ch/[Logback].
In each case, loggers are pre-configured to use console output with optional file output also available.
By default, if you use the "`Starters`", Logback is used for logging.
@ -17,12 +18,13 @@ This prevents logging performed by the container or other applications that have
[[features.logging.log-format]]
=== Log Format
== Log Format
The default log output from Spring Boot resembles the following example:
[indent=0]
[source]
----
include::{logging-format-output}[]
include::ROOT:partial$logging/logging-format.txt[]
----
The following items are output:
@ -43,15 +45,17 @@ It is mapped to `ERROR`.
TIP: If you have a configprop:spring.application.name[] property but don't want it logged you can set configprop:logging.include-application-name[] to `false`.
[[features.logging.console-output]]
=== Console Output
== Console Output
The default log configuration echoes messages to the console as they are written.
By default, `ERROR`-level, `WARN`-level, and `INFO`-level messages are logged.
You can also enable a "`debug`" mode by starting your application with a `--debug` flag.
[source,shell,indent=0,subs="verbatim"]
[source,shell]
----
$ java -jar myapp.jar --debug
$ java -jar myapp.jar --debug
----
NOTE: You can also specify `debug=true` in your `application.properties`.
@ -65,14 +69,15 @@ Doing so enables trace logging for a selection of core loggers (embedded contain
[[features.logging.console-output.color-coded]]
==== Color-coded Output
=== Color-coded Output
If your terminal supports ANSI, color output is used to aid readability.
You can set `spring.output.ansi.enabled` to a {spring-boot-module-api}/ansi/AnsiOutput.Enabled.html[supported value] to override the auto-detection.
You can set `spring.output.ansi.enabled` to a xref:api:java/org/springframework/boot/ansi/AnsiOutput.Enabled.html[supported value] to override the auto-detection.
Color coding is configured by using the `%clr` conversion word.
In its simplest form, the converter colors the output according to the log level, as shown in the following example:
[source,indent=0,subs="verbatim"]
[source]
----
%clr(%5p)
----
@ -104,9 +109,9 @@ The following table describes the mapping of log levels to colors:
Alternatively, you can specify the color or style that should be used by providing it as an option to the conversion.
For example, to make the text yellow, use the following setting:
[source,indent=0,subs="verbatim"]
[source]
----
%clr(%d{yyyy-MM-dd'T'HH:mm:ss.SSSXXX}){yellow}
%clr(%d{yyyy-MM-dd'T'HH:mm:ss.SSSXXX}){yellow}
----
The following colors and styles are supported:
@ -122,7 +127,8 @@ The following colors and styles are supported:
[[features.logging.file-output]]
=== File Output
== File Output
By default, Spring Boot logs only to the console and does not write log files.
If you want to write log files in addition to the console output, you need to set a configprop:logging.file.name[] or configprop:logging.file.path[] property (for example, in your `application.properties`).
@ -159,7 +165,8 @@ As a result, specific configuration keys (such as `logback.configurationFile` fo
[[features.logging.file-rotation]]
=== File Rotation
== File Rotation
If you are using the Logback, it is possible to fine-tune log rotation settings using your `application.properties` or `application.yaml` file.
For all other logging system, you will need to configure rotation settings directly yourself (for example, if you use Log4j2 then you could add a `log4j2.xml` or `log4j2-spring.xml` file).
@ -187,28 +194,20 @@ The following rotation policy properties are supported:
[[features.logging.log-levels]]
=== Log Levels
== Log Levels
All the supported logging systems can have the logger levels set in the Spring `Environment` (for example, in `application.properties`) by using `+logging.level.<logger-name>=<level>+` where `level` is one of TRACE, DEBUG, INFO, WARN, ERROR, FATAL, or OFF.
The `root` logger can be configured by using `logging.level.root`.
The following example shows potential logging settings in `application.properties`:
[source,properties,indent=0,subs="verbatim",configprops,role="primary"]
.Properties
[configprops,yaml]
----
logging.level.root=warn
logging.level.org.springframework.web=debug
logging.level.org.hibernate=error
----
[source,properties,indent=0,subs="verbatim",role="secondary"]
.Yaml
----
logging:
level:
root: "warn"
org.springframework.web: "debug"
org.hibernate: "error"
logging:
level:
root: "warn"
org.springframework.web: "debug"
org.hibernate: "error"
----
It is also possible to set logging levels using environment variables.
@ -216,32 +215,33 @@ For example, `LOGGING_LEVEL_ORG_SPRINGFRAMEWORK_WEB=DEBUG` will set `org.springf
NOTE: The above approach will only work for package level logging.
Since relaxed binding always converts environment variables to lowercase, it is not possible to configure logging for an individual class in this way.
If you need to configure logging for a class, you can use <<features#features.external-config.application-json, the `SPRING_APPLICATION_JSON`>> variable.
If you need to configure logging for a class, you can use xref:features/external-config.adoc#features.external-config.application-json[the `SPRING_APPLICATION_JSON`] variable.
[[features.logging.log-groups]]
=== Log Groups
== Log Groups
It is often useful to be able to group related loggers together so that they can all be configured at the same time.
For example, you might commonly change the logging levels for _all_ Tomcat related loggers, but you can not easily remember top level packages.
To help with this, Spring Boot allows you to define logging groups in your Spring `Environment`.
For example, here is how you could define a "`tomcat`" group by adding it to your `application.properties`:
[source,yaml,indent=0,subs="verbatim",configprops,configblocks]
[configprops,yaml]
----
logging:
group:
tomcat: "org.apache.catalina,org.apache.coyote,org.apache.tomcat"
logging:
group:
tomcat: "org.apache.catalina,org.apache.coyote,org.apache.tomcat"
----
Once defined, you can change the level for all the loggers in the group with a single line:
[source,yaml,indent=0,subs="verbatim",configprops,configblocks]
[configprops,yaml]
----
logging:
level:
tomcat: "trace"
logging:
level:
tomcat: "trace"
----
Spring Boot includes the following pre-defined logging groups that can be used out-of-the-box:
@ -260,7 +260,8 @@ Spring Boot includes the following pre-defined logging groups that can be used o
[[features.logging.shutdown-hook]]
=== Using a Log Shutdown Hook
== Using a Log Shutdown Hook
In order to release logging resources when your application terminates, a shutdown hook that will trigger log system cleanup when the JVM exits is provided.
This shutdown hook is registered automatically unless your application is deployed as a war file.
If your application has complex context hierarchies the shutdown hook may not meet your needs.
@ -270,16 +271,17 @@ You can use the configprop:logging.register-shutdown-hook[] property to disable
Setting it to `false` will disable the registration.
You can set the property in your `application.properties` or `application.yaml` file:
[source,yaml,indent=0,subs="verbatim",configprops,configblocks]
[configprops,yaml]
----
logging:
register-shutdown-hook: false
logging:
register-shutdown-hook: false
----
[[features.logging.custom-log-configuration]]
=== Custom Log Configuration
== Custom Log Configuration
The various logging systems can be activated by including the appropriate libraries on the classpath and can be further customized by providing a suitable configuration file in the root of the classpath or in a location specified by the following Spring `Environment` property: configprop:logging.config[].
You can force Spring Boot to use a particular logging system by using the `org.springframework.boot.logging.LoggingSystem` system property.
@ -396,13 +398,13 @@ If you use Logback, the following properties are also transferred:
All the supported logging systems can consult System properties when parsing their configuration files.
See the default configurations in `spring-boot.jar` for examples:
* {spring-boot-code}/spring-boot-project/spring-boot/src/main/resources/org/springframework/boot/logging/logback/defaults.xml[Logback]
* {spring-boot-code}/spring-boot-project/spring-boot/src/main/resources/org/springframework/boot/logging/log4j2/log4j2.xml[Log4j 2]
* {spring-boot-code}/spring-boot-project/spring-boot/src/main/resources/org/springframework/boot/logging/java/logging-file.properties[Java Util logging]
* {code-spring-boot}/spring-boot-project/spring-boot/src/main/resources/org/springframework/boot/logging/logback/defaults.xml[Logback]
* {code-spring-boot}/spring-boot-project/spring-boot/src/main/resources/org/springframework/boot/logging/log4j2/log4j2.xml[Log4j 2]
* {code-spring-boot}/spring-boot-project/spring-boot/src/main/resources/org/springframework/boot/logging/java/logging-file.properties[Java Util logging]
[TIP]
====
If you want to use a placeholder in a logging property, you should use <<features#features.external-config.files.property-placeholders,Spring Boot's syntax>> and not the syntax of the underlying framework.
If you want to use a placeholder in a logging property, you should use xref:features/external-config.adoc#features.external-config.files.property-placeholders[Spring Boot's syntax] and not the syntax of the underlying framework.
Notably, if you use Logback, you should use `:` as the delimiter between a property name and its default value and not use `:-`.
====
@ -411,17 +413,18 @@ Notably, if you use Logback, you should use `:` as the delimiter between a prope
You can add MDC and other ad-hoc content to log lines by overriding only the `LOG_LEVEL_PATTERN` (or `logging.pattern.level` with Logback).
For example, if you use `logging.pattern.level=user:%X\{user} %5p`, then the default log format contains an MDC entry for "user", if it exists, as shown in the following example.
[indent=0]
[source]
----
2019-08-30 12:30:04.031 user:someone INFO 22174 --- [ nio-8080-exec-0] demo.Controller
Handling authenticated request
2019-08-30 12:30:04.031 user:someone INFO 22174 --- [ nio-8080-exec-0] demo.Controller
Handling authenticated request
----
====
[[features.logging.logback-extensions]]
=== Logback Extensions
== Logback Extensions
Spring Boot includes a number of extensions to Logback that can help with advanced configuration.
You can use these extensions in your `logback-spring.xml` configuration file.
@ -431,43 +434,45 @@ You need to either use `logback-spring.xml` or define a configprop:logging.confi
WARNING: The extensions cannot be used with Logback's https://logback.qos.ch/manual/configuration.html#autoScan[configuration scanning].
If you attempt to do so, making changes to the configuration file results in an error similar to one of the following being logged:
[indent=0]
[source]
----
ERROR in ch.qos.logback.core.joran.spi.Interpreter@4:71 - no applicable action for [springProperty], current ElementPath is [[configuration][springProperty]]
ERROR in ch.qos.logback.core.joran.spi.Interpreter@4:71 - no applicable action for [springProfile], current ElementPath is [[configuration][springProfile]]
ERROR in ch.qos.logback.core.joran.spi.Interpreter@4:71 - no applicable action for [springProperty], current ElementPath is [[configuration][springProperty]]
ERROR in ch.qos.logback.core.joran.spi.Interpreter@4:71 - no applicable action for [springProfile], current ElementPath is [[configuration][springProfile]]
----
[[features.logging.logback-extensions.profile-specific]]
==== Profile-specific Configuration
=== Profile-specific Configuration
The `<springProfile>` tag lets you optionally include or exclude sections of configuration based on the active Spring profiles.
Profile sections are supported anywhere within the `<configuration>` element.
Use the `name` attribute to specify which profile accepts the configuration.
The `<springProfile>` tag can contain a profile name (for example `staging`) or a profile expression.
A profile expression allows for more complicated profile logic to be expressed, for example `production & (eu-central | eu-west)`.
Check the {spring-framework-docs}/core/beans/environment.html#beans-definition-profiles-java[Spring Framework reference guide] for more details.
Check the {url-spring-framework-docs}/core/beans/environment.html#beans-definition-profiles-java[Spring Framework reference guide] for more details.
The following listing shows three sample profiles:
[source,xml,subs="verbatim",indent=0]
[source,xml]
----
<springProfile name="staging">
<!-- configuration to be enabled when the "staging" profile is active -->
</springProfile>
<springProfile name="staging">
<!-- configuration to be enabled when the "staging" profile is active -->
</springProfile>
<springProfile name="dev | staging">
<!-- configuration to be enabled when the "dev" or "staging" profiles are active -->
</springProfile>
<springProfile name="dev | staging">
<!-- configuration to be enabled when the "dev" or "staging" profiles are active -->
</springProfile>
<springProfile name="!production">
<!-- configuration to be enabled when the "production" profile is not active -->
</springProfile>
<springProfile name="!production">
<!-- configuration to be enabled when the "production" profile is not active -->
</springProfile>
----
[[features.logging.logback-extensions.environment-properties]]
==== Environment Properties
=== Environment Properties
The `<springProperty>` tag lets you expose properties from the Spring `Environment` for use within Logback.
Doing so can be useful if you want to access values from your `application.properties` file in your Logback configuration.
The tag works in a similar way to Logback's standard `<property>` tag.
@ -476,14 +481,14 @@ If you need to store the property somewhere other than in `local` scope, you can
If you need a fallback value (in case the property is not set in the `Environment`), you can use the `defaultValue` attribute.
The following example shows how to expose properties for use within Logback:
[source,xml,subs="verbatim",indent=0]
[source,xml]
----
<springProperty scope="context" name="fluentHost" source="myapp.fluentd.host"
defaultValue="localhost"/>
<appender name="FLUENT" class="ch.qos.logback.more.appenders.DataFluentAppender">
<remoteHost>${fluentHost}</remoteHost>
...
</appender>
<springProperty scope="context" name="fluentHost" source="myapp.fluentd.host"
defaultValue="localhost"/>
<appender name="FLUENT" class="ch.qos.logback.more.appenders.DataFluentAppender">
<remoteHost>${fluentHost}</remoteHost>
...
</appender>
----
NOTE: The `source` must be specified in kebab case (such as `my.property-name`).
@ -492,7 +497,8 @@ However, properties can be added to the `Environment` by using the relaxed rules
[[features.logging.log4j2-extensions]]
=== Log4j2 Extensions
== Log4j2 Extensions
Spring Boot includes a number of extensions to Log4j2 that can help with advanced configuration.
You can use these extensions in any `log4j2-spring.xml` configuration file.
@ -505,44 +511,46 @@ You should make sure not to include the `org.apache.logging.log4j:log4j-spring-b
[[features.logging.log4j2-extensions.profile-specific]]
==== Profile-specific Configuration
=== Profile-specific Configuration
The `<SpringProfile>` tag lets you optionally include or exclude sections of configuration based on the active Spring profiles.
Profile sections are supported anywhere within the `<Configuration>` element.
Use the `name` attribute to specify which profile accepts the configuration.
The `<SpringProfile>` tag can contain a profile name (for example `staging`) or a profile expression.
A profile expression allows for more complicated profile logic to be expressed, for example `production & (eu-central | eu-west)`.
Check the {spring-framework-docs}/core/beans/environment.html#beans-definition-profiles-java[Spring Framework reference guide] for more details.
Check the {url-spring-framework-docs}/core/beans/environment.html#beans-definition-profiles-java[Spring Framework reference guide] for more details.
The following listing shows three sample profiles:
[source,xml,subs="verbatim",indent=0]
[source,xml]
----
<SpringProfile name="staging">
<!-- configuration to be enabled when the "staging" profile is active -->
</SpringProfile>
<SpringProfile name="staging">
<!-- configuration to be enabled when the "staging" profile is active -->
</SpringProfile>
<SpringProfile name="dev | staging">
<!-- configuration to be enabled when the "dev" or "staging" profiles are active -->
</SpringProfile>
<SpringProfile name="dev | staging">
<!-- configuration to be enabled when the "dev" or "staging" profiles are active -->
</SpringProfile>
<SpringProfile name="!production">
<!-- configuration to be enabled when the "production" profile is not active -->
</SpringProfile>
<SpringProfile name="!production">
<!-- configuration to be enabled when the "production" profile is not active -->
</SpringProfile>
----
[[features.logging.log4j2-extensions.environment-properties-lookup]]
==== Environment Properties Lookup
=== Environment Properties Lookup
If you want to refer to properties from your Spring `Environment` within your Log4j2 configuration you can use `spring:` prefixed https://logging.apache.org/log4j/2.x/manual/lookups.html[lookups].
Doing so can be useful if you want to access values from your `application.properties` file in your Log4j2 configuration.
The following example shows how to set a Log4j2 property named `applicationName` that reads `spring.application.name` from the Spring `Environment`:
[source,xml,subs="verbatim",indent=0]
[source,xml]
----
<Properties>
<Property name="applicationName">${spring:spring.application.name}</Property>
</Properties>
<Properties>
<Property name="applicationName">${spring:spring.application.name}</Property>
</Properties>
----
NOTE: The lookup key should be specified in kebab case (such as `my.property-name`).
@ -550,7 +558,8 @@ NOTE: The lookup key should be specified in kebab case (such as `my.property-nam
[[features.logging.log4j2-extensions.environment-property-source]]
==== Log4j2 System Properties
=== Log4j2 System Properties
Log4j2 supports a number of https://logging.apache.org/log4j/2.x/manual/configuration.html#SystemProperties[System Properties] that can be used to configure various items.
For example, the `log4j2.skipJansi` system property can be used to configure if the `ConsoleAppender` will try to use a https://github.com/fusesource/jansi[Jansi] output stream on Windows.

View File

@ -1,9 +1,10 @@
[[features.profiles]]
== Profiles
= Profiles
Spring Profiles provide a way to segregate parts of your application configuration and make it be available only in certain environments.
Any `@Component`, `@Configuration` or `@ConfigurationProperties` can be marked with `@Profile` to limit when it is loaded, as shown in the following example:
include::code:ProductionConfiguration[]
include-code::ProductionConfiguration[]
NOTE: If `@ConfigurationProperties` beans are registered through `@EnableConfigurationProperties` instead of automatic scanning, the `@Profile` annotation needs to be specified on the `@Configuration` class that has the `@EnableConfigurationProperties` annotation.
In the case where `@ConfigurationProperties` are scanned, `@Profile` can be specified on the `@ConfigurationProperties` class itself.
@ -12,11 +13,11 @@ You can use a configprop:spring.profiles.active[] `Environment` property to spec
You can specify the property in any of the ways described earlier in this chapter.
For example, you could include it in your `application.properties`, as shown in the following example:
[source,yaml,indent=0,subs="verbatim",configprops,configblocks]
[configprops,yaml]
----
spring:
profiles:
active: "dev,hsqldb"
spring:
profiles:
active: "dev,hsqldb"
----
You could also specify it on the command line by using the following switch: `--spring.profiles.active=dev,hsqldb`.
@ -24,66 +25,68 @@ You could also specify it on the command line by using the following switch: `--
If no profile is active, a default profile is enabled.
The name of the default profile is `default` and it can be tuned using the configprop:spring.profiles.default[] `Environment` property, as shown in the following example:
[source,yaml,indent=0,subs="verbatim",configprops,configblocks]
[configprops,yaml]
----
spring:
profiles:
default: "none"
spring:
profiles:
default: "none"
----
`spring.profiles.active` and `spring.profiles.default` can only be used in non-profile specific documents.
This means they cannot be included in <<features#features.external-config.files.profile-specific,profile specific files>> or <<features#features.external-config.files.activation-properties,documents activated>> by `spring.config.activate.on-profile`.
This means they cannot be included in xref:features/external-config.adoc#features.external-config.files.profile-specific[profile specific files] or xref:features/external-config.adoc#features.external-config.files.activation-properties[documents activated] by `spring.config.activate.on-profile`.
For example, the second document configuration is invalid:
[source,yaml,indent=0,subs="verbatim",configprops,configblocks]
[configprops,yaml]
----
# this document is valid
spring:
profiles:
active: "prod"
---
# this document is invalid
spring:
config:
activate:
on-profile: "prod"
profiles:
active: "metrics"
# this document is valid
spring:
profiles:
active: "prod"
---
# this document is invalid
spring:
config:
activate:
on-profile: "prod"
profiles:
active: "metrics"
----
[[features.profiles.adding-active-profiles]]
=== Adding Active Profiles
== Adding Active Profiles
The configprop:spring.profiles.active[] property follows the same ordering rules as other properties: The highest `PropertySource` wins.
This means that you can specify active profiles in `application.properties` and then *replace* them by using the command line switch.
Sometimes, it is useful to have properties that *add* to the active profiles rather than replace them.
The `spring.profiles.include` property can be used to add active profiles on top of those activated by the configprop:spring.profiles.active[] property.
The `SpringApplication` entry point also has a Java API for setting additional profiles.
See the `setAdditionalProfiles()` method in {spring-boot-module-api}/SpringApplication.html[SpringApplication].
See the `setAdditionalProfiles()` method in xref:api:java/org/springframework/boot/SpringApplication.html[SpringApplication].
For example, when an application with the following properties is run, the common and local profiles will be activated even when it runs using the `--spring.profiles.active` switch:
[source,yaml,indent=0,subs="verbatim",configprops,configblocks]
[configprops,yaml]
----
spring:
profiles:
include:
- "common"
- "local"
spring:
profiles:
include:
- "common"
- "local"
----
WARNING: Similar to `spring.profiles.active`, `spring.profiles.include` can only be used in non-profile specific documents.
This means it cannot be included in <<features#features.external-config.files.profile-specific,profile specific files>> or <<features#features.external-config.files.activation-properties,documents activated>> by `spring.config.activate.on-profile`.
This means it cannot be included in xref:features/external-config.adoc#features.external-config.files.profile-specific[profile specific files] or xref:features/external-config.adoc#features.external-config.files.activation-properties[documents activated] by `spring.config.activate.on-profile`.
Profile groups, which are described in the <<features#features.profiles.groups,next section>> can also be used to add active profiles if a given profile is active.
Profile groups, which are described in the xref:features/profiles.adoc#features.profiles.groups[next section] can also be used to add active profiles if a given profile is active.
[[features.profiles.groups]]
=== Profile Groups
== Profile Groups
Occasionally the profiles that you define and use in your application are too fine-grained and become cumbersome to use.
For example, you might have `proddb` and `prodmq` profiles that you use to enable database and messaging features independently.
@ -92,14 +95,14 @@ A profile group allows you to define a logical name for a related group of profi
For example, we can create a `production` group that consists of our `proddb` and `prodmq` profiles.
[source,yaml,indent=0,subs="verbatim",configblocks]
[configprops,yaml]
----
spring:
profiles:
group:
production:
- "proddb"
- "prodmq"
spring:
profiles:
group:
production:
- "proddb"
- "prodmq"
----
Our application can now be started using `--spring.profiles.active=production` to activate the `production`, `proddb` and `prodmq` profiles in one hit.
@ -107,13 +110,15 @@ Our application can now be started using `--spring.profiles.active=production` t
[[features.profiles.programmatically-setting-profiles]]
=== Programmatically Setting Profiles
== Programmatically Setting Profiles
You can programmatically set active profiles by calling `SpringApplication.setAdditionalProfiles(...)` before your application runs.
It is also possible to activate profiles by using Spring's `ConfigurableEnvironment` interface.
[[features.profiles.profile-specific-configuration-files]]
=== Profile-specific Configuration Files
== Profile-specific Configuration Files
Profile-specific variants of both `application.properties` (or `application.yaml`) and files referenced through `@ConfigurationProperties` are considered as files and loaded.
See "<<features#features.external-config.files.profile-specific>>" for details.
See "xref:features/external-config.adoc#features.external-config.files.profile-specific[Profile Specific Files]" for details.

View File

@ -1,21 +1,22 @@
[[features.spring-application]]
== SpringApplication
= SpringApplication
The `SpringApplication` class provides a convenient way to bootstrap a Spring application that is started from a `main()` method.
In many situations, you can delegate to the static `SpringApplication.run` method, as shown in the following example:
include::code:MyApplication[]
include-code::MyApplication[]
When your application starts, you should see something similar to the following output:
[indent=0,subs="verbatim,attributes"]
[source,subs="verbatim,attributes"]
----
include::{spring-application-output}[]
include::ROOT:partial$application/spring-application.txt[]
----
By default, `INFO` logging messages are shown, including some relevant startup details, such as the user that launched the application.
If you need a log level other than `INFO`, you can set it, as described in <<features#features.logging.log-levels>>.
If you need a log level other than `INFO`, you can set it, as described in xref:features/logging.adoc#features.logging.log-levels[Log Levels].
The application version is determined using the implementation version from the main application class's package.
Startup information logging can be turned off by setting `spring.main.log-startup-info` to `false`.
This will also turn off logging of the application's active profiles.
@ -25,41 +26,43 @@ TIP: To add additional logging during startup, you can override `logStartupInfo(
[[features.spring-application.startup-failure]]
=== Startup Failure
== Startup Failure
If your application fails to start, registered `FailureAnalyzers` get a chance to provide a dedicated error message and a concrete action to fix the problem.
For instance, if you start a web application on port `8080` and that port is already in use, you should see something similar to the following message:
[indent=0]
[source]
----
***************************
APPLICATION FAILED TO START
***************************
***************************
APPLICATION FAILED TO START
***************************
Description:
Description:
Embedded servlet container failed to start. Port 8080 was already in use.
Embedded servlet container failed to start. Port 8080 was already in use.
Action:
Action:
Identify and stop the process that is listening on port 8080 or configure this application to listen on another port.
Identify and stop the process that is listening on port 8080 or configure this application to listen on another port.
----
NOTE: Spring Boot provides numerous `FailureAnalyzer` implementations, and you can <<howto#howto.application.failure-analyzer,add your own>>.
NOTE: Spring Boot provides numerous `FailureAnalyzer` implementations, and you can xref:how-to:application.adoc#howto.application.failure-analyzer[add your own].
If no failure analyzers are able to handle the exception, you can still display the full conditions report to better understand what went wrong.
To do so, you need to <<features#features.external-config,enable the `debug` property>> or <<features#features.logging.log-levels,enable `DEBUG` logging>> for `org.springframework.boot.autoconfigure.logging.ConditionEvaluationReportLoggingListener`.
To do so, you need to xref:features/external-config.adoc[enable the `debug` property] or xref:features/logging.adoc#features.logging.log-levels[enable `DEBUG` logging] for `org.springframework.boot.autoconfigure.logging.ConditionEvaluationReportLoggingListener`.
For instance, if you are running your application by using `java -jar`, you can enable the `debug` property as follows:
[source,shell,indent=0,subs="verbatim"]
[source,shell]
----
$ java -jar myproject-0.0.1-SNAPSHOT.jar --debug
$ java -jar myproject-0.0.1-SNAPSHOT.jar --debug
----
[[features.spring-application.lazy-initialization]]
=== Lazy Initialization
== Lazy Initialization
`SpringApplication` allows an application to be initialized lazily.
When lazy initialization is enabled, beans are created as they are needed rather than during application startup.
As a result, enabling lazy initialization can reduce the time that it takes your application to start.
@ -73,11 +76,11 @@ For these reasons, lazy initialization is not enabled by default and it is recom
Lazy initialization can be enabled programmatically using the `lazyInitialization` method on `SpringApplicationBuilder` or the `setLazyInitialization` method on `SpringApplication`.
Alternatively, it can be enabled using the configprop:spring.main.lazy-initialization[] property as shown in the following example:
[source,yaml,indent=0,subs="verbatim",configprops,configblocks]
[configprops,yaml]
----
spring:
main:
lazy-initialization: true
spring:
main:
lazy-initialization: true
----
TIP: If you want to disable lazy initialization for certain beans while using lazy initialization for the rest of the application, you can explicitly set their lazy attribute to false using the `@Lazy(false)` annotation.
@ -85,7 +88,8 @@ TIP: If you want to disable lazy initialization for certain beans while using la
[[features.spring-application.banner]]
=== Customizing the Banner
== Customizing the Banner
The banner that is printed on start up can be changed by adding a `banner.txt` file to your classpath or by setting the configprop:spring.banner.location[] property to the location of such a file.
If the file has an encoding other than UTF-8, you can set `spring.banner.charset`.
@ -105,15 +109,15 @@ Inside your `banner.txt` file, you can use any key available in the `Environment
| `${spring-boot.version}`
| The Spring Boot version that you are using.
For example `{spring-boot-version}`.
For example `{version-spring-boot}`.
| `${spring-boot.formatted-version}`
| The Spring Boot version that you are using, formatted for display (surrounded with brackets and prefixed with `v`).
For example `(v{spring-boot-version})`.
For example `(v{version-spring-boot})`.
| `${Ansi.NAME}` (or `${AnsiColor.NAME}`, `${AnsiBackground.NAME}`, `${AnsiStyle.NAME}`)
| Where `NAME` is the name of an ANSI escape code.
See {spring-boot-module-code}/ansi/AnsiPropertySource.java[`AnsiPropertySource`] for details.
See {code-spring-boot-src}/ansi/AnsiPropertySource.java[`AnsiPropertySource`] for details.
| `${application.title}`
| The title of your application, as declared in `MANIFEST.MF`.
@ -140,38 +144,41 @@ This will initialize the `application.*` banner properties before building the c
[[features.spring-application.customizing-spring-application]]
=== Customizing SpringApplication
== Customizing SpringApplication
If the `SpringApplication` defaults are not to your taste, you can instead create a local instance and customize it.
For example, to turn off the banner, you could write:
include::code:MyApplication[]
include-code::MyApplication[]
NOTE: The constructor arguments passed to `SpringApplication` are configuration sources for Spring beans.
In most cases, these are references to `@Configuration` classes, but they could also be direct references `@Component` classes.
It is also possible to configure the `SpringApplication` by using an `application.properties` file.
See _<<features#features.external-config>>_ for details.
See _xref:features/external-config.adoc[Externalized Configuration]_ for details.
For a complete list of the configuration options, see the {spring-boot-module-api}/SpringApplication.html[`SpringApplication` Javadoc].
For a complete list of the configuration options, see the xref:api:java/org/springframework/boot/SpringApplication.html[`SpringApplication` Javadoc].
[[features.spring-application.fluent-builder-api]]
=== Fluent Builder API
== Fluent Builder API
If you need to build an `ApplicationContext` hierarchy (multiple contexts with a parent/child relationship) or if you prefer using a "`fluent`" builder API, you can use the `SpringApplicationBuilder`.
The `SpringApplicationBuilder` lets you chain together multiple method calls and includes `parent` and `child` methods that let you create a hierarchy, as shown in the following example:
include::code:MyApplication[tag=*]
include-code::MyApplication[tag=*]
NOTE: There are some restrictions when creating an `ApplicationContext` hierarchy.
For example, Web components *must* be contained within the child context, and the same `Environment` is used for both parent and child contexts.
See the {spring-boot-module-api}/builder/SpringApplicationBuilder.html[`SpringApplicationBuilder` Javadoc] for full details.
See the xref:api:java/org/springframework/boot/builder/SpringApplicationBuilder.html[`SpringApplicationBuilder` Javadoc] for full details.
[[features.spring-application.application-availability]]
=== Application Availability
== Application Availability
When deployed on platforms, applications can provide information about their availability to the platform using infrastructure such as https://kubernetes.io/docs/tasks/configure-pod-container/configure-liveness-readiness-startup-probes/[Kubernetes Probes].
Spring Boot includes out-of-the box support for the commonly used "`liveness`" and "`readiness`" availability states.
If you are using Spring Boot's "`actuator`" support then these states are exposed as health endpoint groups.
@ -181,52 +188,56 @@ In addition, you can also obtain availability states by injecting the `Applicati
[[features.spring-application.application-availability.liveness]]
==== Liveness State
=== Liveness State
The "`Liveness`" state of an application tells whether its internal state allows it to work correctly, or recover by itself if it is currently failing.
A broken "`Liveness`" state means that the application is in a state that it cannot recover from, and the infrastructure should restart the application.
NOTE: In general, the "Liveness" state should not be based on external checks, such as <<actuator#actuator.endpoints.health, Health checks>>.
NOTE: In general, the "Liveness" state should not be based on external checks, such as xref:actuator/endpoints.adoc#actuator.endpoints.health[Health checks].
If it did, a failing external system (a database, a Web API, an external cache) would trigger massive restarts and cascading failures across the platform.
The internal state of Spring Boot applications is mostly represented by the Spring `ApplicationContext`.
If the application context has started successfully, Spring Boot assumes that the application is in a valid state.
An application is considered live as soon as the context has been refreshed, see <<features#features.spring-application.application-events-and-listeners, Spring Boot application lifecycle and related Application Events>>.
An application is considered live as soon as the context has been refreshed, see xref:features/spring-application.adoc#features.spring-application.application-events-and-listeners[Spring Boot application lifecycle and related Application Events].
[[features.spring-application.application-availability.readiness]]
==== Readiness State
=== Readiness State
The "`Readiness`" state of an application tells whether the application is ready to handle traffic.
A failing "`Readiness`" state tells the platform that it should not route traffic to the application for now.
This typically happens during startup, while `CommandLineRunner` and `ApplicationRunner` components are being processed, or at any time if the application decides that it is too busy for additional traffic.
An application is considered ready as soon as application and command-line runners have been called, see <<features#features.spring-application.application-events-and-listeners, Spring Boot application lifecycle and related Application Events>>.
An application is considered ready as soon as application and command-line runners have been called, see xref:features/spring-application.adoc#features.spring-application.application-events-and-listeners[Spring Boot application lifecycle and related Application Events].
TIP: Tasks expected to run during startup should be executed by `CommandLineRunner` and `ApplicationRunner` components instead of using Spring component lifecycle callbacks such as `@PostConstruct`.
[[features.spring-application.application-availability.managing]]
==== Managing the Application Availability State
=== Managing the Application Availability State
Application components can retrieve the current availability state at any time, by injecting the `ApplicationAvailability` interface and calling methods on it.
More often, applications will want to listen to state updates or update the state of the application.
For example, we can export the "Readiness" state of the application to a file so that a Kubernetes "exec Probe" can look at this file:
include::code:MyReadinessStateExporter[]
include-code::MyReadinessStateExporter[]
We can also update the state of the application, when the application breaks and cannot recover:
include::code:MyLocalCacheVerifier[]
include-code::MyLocalCacheVerifier[]
Spring Boot provides <<actuator#actuator.endpoints.kubernetes-probes,Kubernetes HTTP probes for "Liveness" and "Readiness" with Actuator Health Endpoints>>.
You can get more guidance about <<deployment#deployment.cloud.kubernetes,deploying Spring Boot applications on Kubernetes in the dedicated section>>.
Spring Boot provides xref:actuator/endpoints.adoc#actuator.endpoints.kubernetes-probes[Kubernetes HTTP probes for "Liveness" and "Readiness" with Actuator Health Endpoints].
You can get more guidance about xref:deployment/cloud.adoc#deployment.cloud.kubernetes[deploying Spring Boot applications on Kubernetes in the dedicated section].
[[features.spring-application.application-events-and-listeners]]
=== Application Events and Listeners
In addition to the usual Spring Framework events, such as {spring-framework-api}/context/event/ContextRefreshedEvent.html[`ContextRefreshedEvent`], a `SpringApplication` sends some additional application events.
== Application Events and Listeners
In addition to the usual Spring Framework events, such as {url-spring-framework-javadoc}/org/springframework/context/event/ContextRefreshedEvent.html[`ContextRefreshedEvent`], a `SpringApplication` sends some additional application events.
[NOTE]
====
@ -235,9 +246,9 @@ You can register them with the `SpringApplication.addListeners(...)` method or t
If you want those listeners to be registered automatically, regardless of the way the application is created, you can add a `META-INF/spring.factories` file to your project and reference your listener(s) by using the `org.springframework.context.ApplicationListener` key, as shown in the following example:
[indent=0]
[source]
----
org.springframework.context.ApplicationListener=com.example.project.MyListener
org.springframework.context.ApplicationListener=com.example.project.MyListener
----
====
@ -250,7 +261,7 @@ Application events are sent in the following order, as your application runs:
. An `ApplicationPreparedEvent` is sent just before the refresh is started but after bean definitions have been loaded.
. An `ApplicationStartedEvent` is sent after the context has been refreshed but before any application and command-line runners have been called.
. An `AvailabilityChangeEvent` is sent right after with `LivenessState.CORRECT` to indicate that the application is considered as live.
. An `ApplicationReadyEvent` is sent after any <<features#features.spring-application.command-line-runner,application and command-line runners>> have been called.
. An `ApplicationReadyEvent` is sent after any xref:features/spring-application.adoc#features.spring-application.command-line-runner[application and command-line runners] have been called.
. An `AvailabilityChangeEvent` is sent right after with `ReadinessState.ACCEPTING_TRAFFIC` to indicate that the application is ready to service requests.
. An `ApplicationFailedEvent` is sent if there is an exception on startup.
@ -265,7 +276,7 @@ TIP: You often need not use application events, but it can be handy to know that
Internally, Spring Boot uses events to handle a variety of tasks.
NOTE: Event listeners should not run potentially lengthy tasks as they execute in the same thread by default.
Consider using <<features#features.spring-application.command-line-runner,application and command-line runners>> instead.
Consider using xref:features/spring-application.adoc#features.spring-application.command-line-runner[application and command-line runners] instead.
Application events are sent by using Spring Framework's event publishing mechanism.
Part of this mechanism ensures that an event published to the listeners in a child context is also published to the listeners in any ancestor contexts.
@ -277,7 +288,8 @@ The context can be injected by implementing `ApplicationContextAware` or, if the
[[features.spring-application.web-environment]]
=== Web Environment
== Web Environment
A `SpringApplication` attempts to create the right type of `ApplicationContext` on your behalf.
The algorithm used to determine a `WebApplicationType` is the following:
@ -295,11 +307,12 @@ TIP: It is often desirable to call `setWebApplicationType(WebApplicationType.NON
[[features.spring-application.application-arguments]]
=== Accessing Application Arguments
== Accessing Application Arguments
If you need to access the application arguments that were passed to `SpringApplication.run(...)`, you can inject a `org.springframework.boot.ApplicationArguments` bean.
The `ApplicationArguments` interface provides access to both the raw `String[]` arguments as well as parsed `option` and `non-option` arguments, as shown in the following example:
include::code:MyBean[]
include-code::MyBean[]
TIP: Spring Boot also registers a `CommandLinePropertySource` with the Spring `Environment`.
This lets you also inject single application arguments by using the `@Value` annotation.
@ -307,7 +320,8 @@ This lets you also inject single application arguments by using the `@Value` ann
[[features.spring-application.command-line-runner]]
=== Using the ApplicationRunner or CommandLineRunner
== Using the ApplicationRunner or CommandLineRunner
If you need to run some specific code once the `SpringApplication` has started, you can implement the `ApplicationRunner` or `CommandLineRunner` interfaces.
Both interfaces work in the same way and offer a single `run` method, which is called just before `SpringApplication.run(...)` completes.
@ -317,21 +331,22 @@ NOTE: This contract is well suited for tasks that should run after application s
The `CommandLineRunner` interfaces provides access to application arguments as a string array, whereas the `ApplicationRunner` uses the `ApplicationArguments` interface discussed earlier.
The following example shows a `CommandLineRunner` with a `run` method:
include::code:MyCommandLineRunner[]
include-code::MyCommandLineRunner[]
If several `CommandLineRunner` or `ApplicationRunner` beans are defined that must be called in a specific order, you can additionally implement the `org.springframework.core.Ordered` interface or use the `org.springframework.core.annotation.Order` annotation.
[[features.spring-application.application-exit]]
=== Application Exit
== Application Exit
Each `SpringApplication` registers a shutdown hook with the JVM to ensure that the `ApplicationContext` closes gracefully on exit.
All the standard Spring lifecycle callbacks (such as the `DisposableBean` interface or the `@PreDestroy` annotation) can be used.
In addition, beans may implement the `org.springframework.boot.ExitCodeGenerator` interface if they wish to return a specific exit code when `SpringApplication.exit()` is called.
This exit code can then be passed to `System.exit()` to return it as a status code, as shown in the following example:
include::code:MyApplication[]
include-code::MyApplication[]
Also, the `ExitCodeGenerator` interface may be implemented by exceptions.
When such an exception is encountered, Spring Boot returns the exit code provided by the implemented `getExitCode()` method.
@ -342,9 +357,10 @@ To control the order in which the generators are called, additionally implement
[[features.spring-application.admin]]
=== Admin Features
== Admin Features
It is possible to enable admin-related features for the application by specifying the configprop:spring.application.admin.enabled[] property.
This exposes the {spring-boot-module-code}/admin/SpringApplicationAdminMXBean.java[`SpringApplicationAdminMXBean`] on the platform `MBeanServer`.
This exposes the {code-spring-boot-src}/admin/SpringApplicationAdminMXBean.java[`SpringApplicationAdminMXBean`] on the platform `MBeanServer`.
You could use this feature to administer your Spring Boot application remotely.
This feature could also be useful for any service wrapper implementation.
@ -353,35 +369,37 @@ TIP: If you want to know on which HTTP port the application is running, get the
[[features.spring-application.startup-tracking]]
=== Application Startup tracking
== Application Startup tracking
During the application startup, the `SpringApplication` and the `ApplicationContext` perform many tasks related to the application lifecycle,
the beans lifecycle or even processing application events.
With {spring-framework-api}/core/metrics/ApplicationStartup.html[`ApplicationStartup`], Spring Framework {spring-framework-docs}/core/beans/context-introduction.html#context-functionality-startup[allows you to track the application startup sequence with `StartupStep` objects].
With {url-spring-framework-javadoc}/org/springframework/core/metrics/ApplicationStartup.html[`ApplicationStartup`], Spring Framework {url-spring-framework-docs}/core/beans/context-introduction.html#context-functionality-startup[allows you to track the application startup sequence with `StartupStep` objects].
This data can be collected for profiling purposes, or just to have a better understanding of an application startup process.
You can choose an `ApplicationStartup` implementation when setting up the `SpringApplication` instance.
For example, to use the `BufferingApplicationStartup`, you could write:
include::code:MyApplication[]
include-code::MyApplication[]
The first available implementation, `FlightRecorderApplicationStartup` is provided by Spring Framework.
It adds Spring-specific startup events to a Java Flight Recorder session and is meant for profiling applications and correlating their Spring context lifecycle with JVM events (such as allocations, GCs, class loading...).
Once configured, you can record data by running the application with the Flight Recorder enabled:
[source,shell,indent=0,subs="verbatim"]
[source,shell]
----
$ java -XX:StartFlightRecording:filename=recording.jfr,duration=10s -jar demo.jar
$ java -XX:StartFlightRecording:filename=recording.jfr,duration=10s -jar demo.jar
----
Spring Boot ships with the `BufferingApplicationStartup` variant; this implementation is meant for buffering the startup steps and draining them into an external metrics system.
Applications can ask for the bean of type `BufferingApplicationStartup` in any component.
Spring Boot can also be configured to expose a {spring-boot-actuator-restapi-docs}/#startup[`startup` endpoint] that provides this information as a JSON document.
Spring Boot can also be configured to expose a xref:api:rest/actuator/startup.adoc[`startup` endpoint] that provides this information as a JSON document.
[[features.spring-application.virtual-threads]]
=== Virtual threads
== Virtual threads
If you're running on Java 21 or up, you can enable virtual threads by setting the property configprop:spring.threads.virtual.enabled[] to `true`.
Before turning on this option for your application, you should consider https://docs.oracle.com/en/java/javase/21/core/virtual-threads.html[reading the official Java virtual threads documentation].

View File

@ -1,18 +1,20 @@
[[features.ssl]]
== SSL
= SSL
Spring Boot provides the ability to configure SSL trust material that can be applied to several types of connections in order to support secure communications.
Configuration properties with the prefix `spring.ssl.bundle` can be used to specify named sets of trust material and associated information.
[[features.ssl.jks]]
=== Configuring SSL With Java KeyStore Files
== Configuring SSL With Java KeyStore Files
Configuration properties with the prefix `spring.ssl.bundle.jks` can be used to configure bundles of trust material created with the Java `keytool` utility and stored in Java KeyStore files in the JKS or PKCS12 format.
Each bundle has a user-provided name that can be used to reference the bundle.
When used to secure an embedded web server, a `keystore` is typically configured with a Java KeyStore containing a certificate and private key as shown in this example:
[source,yaml,indent=0,subs="verbatim",configblocks]
[configprops,yaml]
----
spring:
ssl:
@ -29,7 +31,7 @@ When used to secure an embedded web server, a `keystore` is typically configured
When used to secure a client-side connection, a `truststore` is typically configured with a Java KeyStore containing the server certificate as shown in this example:
[source,yaml,indent=0,subs="verbatim",configblocks]
[configprops,yaml]
----
spring:
ssl:
@ -41,18 +43,19 @@ When used to secure a client-side connection, a `truststore` is typically config
password: "secret"
----
See {spring-boot-autoconfigure-module-code}/ssl/JksSslBundleProperties.java[JksSslBundleProperties] for the full set of supported properties.
See {code-spring-boot-autoconfigure-src}/ssl/JksSslBundleProperties.java[JksSslBundleProperties] for the full set of supported properties.
[[features.ssl.pem]]
=== Configuring SSL With PEM-encoded Certificates
== Configuring SSL With PEM-encoded Certificates
Configuration properties with the prefix `spring.ssl.bundle.pem` can be used to configure bundles of trust material in the form of PEM-encoded text.
Each bundle has a user-provided name that can be used to reference the bundle.
When used to secure an embedded web server, a `keystore` is typically configured with a certificate and private key as shown in this example:
[source,yaml,indent=0,subs="verbatim",configblocks]
[configprops,yaml]
----
spring:
ssl:
@ -66,7 +69,7 @@ When used to secure an embedded web server, a `keystore` is typically configured
When used to secure a client-side connection, a `truststore` is typically configured with the server certificate as shown in this example:
[source,yaml,indent=0,subs="verbatim",configblocks]
[configprops,yaml]
----
spring:
ssl:
@ -84,7 +87,7 @@ If the property values contain `BEGIN` and `END` markers then they will be treat
The following example shows how a truststore certificate can be defined:
[source,yaml,indent=0,subs="verbatim",configblocks]
[configprops,yaml]
----
spring:
ssl:
@ -104,19 +107,21 @@ The following example shows how a truststore certificate can be defined:
----
====
See {spring-boot-autoconfigure-module-code}/ssl/PemSslBundleProperties.java[PemSslBundleProperties] for the full set of supported properties.
See {code-spring-boot-autoconfigure-src}/ssl/PemSslBundleProperties.java[PemSslBundleProperties] for the full set of supported properties.
[[features.ssl.applying]]
=== Applying SSL Bundles
== Applying SSL Bundles
Once configured using properties, SSL bundles can be referred to by name in configuration properties for various types of connections that are auto-configured by Spring Boot.
See the sections on <<howto#howto.webserver.configure-ssl,embedded web servers>>, <<data#data,data technologies>>, and <<io#io.rest-client,REST clients>> for further information.
See the sections on xref:how-to:webserver.adoc#howto.webserver.configure-ssl[embedded web servers], xref:data/index.adoc[data technologies], and xref:io/rest-client.adoc[REST clients] for further information.
[[features.ssl.bundles]]
=== Using SSL Bundles
== Using SSL Bundles
Spring Boot auto-configures a bean of type `SslBundles` that provides access to each of the named bundles configured using the `spring.ssl.bundle` properties.
An `SslBundle` can be retrieved from the auto-configured `SslBundles` bean and used to create objects that are used to configure SSL connectivity in client libraries.
@ -130,12 +135,13 @@ In addition, the `SslBundle` provides details about the key being used, the prot
The following example shows retrieving an `SslBundle` and using it to create an `SSLContext`:
include::code:MyComponent[]
include-code::MyComponent[]
[[features.ssl.reloading]]
=== Reloading SSL bundles
== Reloading SSL bundles
SSL bundles can be reloaded when the key material changes.
The component consuming the bundle has to be compatible with reloadable SSL bundles.
Currently the following components are compatible:
@ -145,7 +151,7 @@ Currently the following components are compatible:
To enable reloading, you need to opt-in via a configuration property as shown in this example:
[source,yaml,indent=0,subs="verbatim",configblocks]
[configprops,yaml]
----
spring:
ssl:

View File

@ -1,5 +1,6 @@
[[features.task-execution-and-scheduling]]
== Task Execution and Scheduling
= Task Execution and Scheduling
In the absence of an `Executor` bean in the context, Spring Boot auto-configures an `AsyncTaskExecutor`.
When virtual threads are enabled (using Java 21+ and configprop:spring.threads.virtual.enabled[] set to `true`) this will be a `SimpleAsyncTaskExecutor` that uses virtual threads.
Otherwise, it will be a `ThreadPoolTaskExecutor` with sensible defaults.
@ -22,15 +23,15 @@ The auto-configured `ThreadPoolTaskExecutorBuilder` allows you to easily create
When a `ThreadPoolTaskExecutor` is auto-configured, the thread pool uses 8 core threads that can grow and shrink according to the load.
Those default settings can be fine-tuned using the `spring.task.execution` namespace, as shown in the following example:
[source,yaml,indent=0,subs="verbatim",configprops,configblocks]
[configprops,yaml]
----
spring:
task:
execution:
pool:
max-size: 16
queue-capacity: 100
keep-alive: "10s"
spring:
task:
execution:
pool:
max-size: 16
queue-capacity: 100
keep-alive: "10s"
----
This changes the thread pool to use a bounded queue so that when the queue is full (100 tasks), the thread pool increases to maximum 16 threads.
@ -44,14 +45,14 @@ This `SimpleAsyncTaskScheduler` will ignore any pooling related properties.
If virtual threads are not enabled, it will be a `ThreadPoolTaskScheduler` with sensible defaults.
The `ThreadPoolTaskScheduler` uses one thread by default and its settings can be fine-tuned using the `spring.task.scheduling` namespace, as shown in the following example:
[source,yaml,indent=0,subs="verbatim",configprops,configblocks]
[configprops,yaml]
----
spring:
task:
scheduling:
thread-name-prefix: "scheduling-"
pool:
size: 2
spring:
task:
scheduling:
thread-name-prefix: "scheduling-"
pool:
size: 2
----
A `ThreadPoolTaskExecutorBuilder` bean, a `SimpleAsyncTaskExecutorBuilder` bean, a `ThreadPoolTaskSchedulerBuilder` bean and a `SimpleAsyncTaskSchedulerBuilder` are made available in the context if a custom executor or scheduler needs to be created.

View File

@ -1,12 +1,14 @@
[[features.testcontainers]]
== Testcontainers Support
As well as <<features#features.testing.testcontainers, using Testcontainers for integration testing>>, it's also possible to use them at development time.
= Testcontainers Support
As well as xref:features/testing.adoc#features.testing.testcontainers[using Testcontainers for integration testing], it's also possible to use them at development time.
The next sections will provide more details about that.
[[features.testcontainers.at-development-time]]
=== Using Testcontainers at Development Time
== Using Testcontainers at Development Time
This approach allows developers to quickly start containers for the services that the application depends on, removing the need to manually provision things like database servers.
Using Testcontainers in this way provides functionality similar to Docker Compose, except that your container configuration is in Java rather than YAML.
@ -18,18 +20,18 @@ For example, if your main application is in `src/main/java/com/example/MyApplica
The `TestMyApplication` class can use the `SpringApplication.from(...)` method to launch the real application:
include::code:launch/TestMyApplication[]
include-code::launch/TestMyApplication[]
You'll also need to define the `Container` instances that you want to start along with your application.
To do this, you need to make sure that the `spring-boot-testcontainers` module has been added as a `test` dependency.
Once that has been done, you can create a `@TestConfiguration` class that declares `@Bean` methods for the containers you want to start.
You can also annotate your `@Bean` methods with `@ServiceConnection` in order to create `ConnectionDetails` beans.
See <<features#features.testing.testcontainers.service-connections, the service connections>> section for details of the supported technologies.
See xref:features/testing.adoc#features.testing.testcontainers.service-connections[the service connections] section for details of the supported technologies.
A typical Testcontainers configuration would look like this:
include::code:test/MyContainersConfiguration[]
include-code::test/MyContainersConfiguration[]
NOTE: The lifecycle of `Container` beans is automatically managed by Spring Boot.
Containers will be started and stopped automatically.
@ -39,7 +41,7 @@ By default `sequential` startup is used, but you may also choose `parallel` if y
Once you have defined your test configuration, you can use the `with(...)` method to attach it to your test launcher:
include::code:test/TestMyApplication[]
include-code::test/TestMyApplication[]
You can now launch `TestMyApplication` as you would any regular Java `main` method application to start your application and the containers that it needs to run.
@ -48,46 +50,49 @@ TIP: You can use the Maven goal `spring-boot:test-run` or the Gradle task `bootT
[[features.testcontainers.at-development-time.dynamic-properties]]
==== Contributing Dynamic Properties at Development Time
=== Contributing Dynamic Properties at Development Time
If you want to contribute dynamic properties at development time from your `Container` `@Bean` methods, you can do so by injecting a `DynamicPropertyRegistry`.
This works in a similar way to the <<features#features.testing.testcontainers.dynamic-properties,`@DynamicPropertySource` annotation>> that you can use in your tests.
This works in a similar way to the xref:features/testing.adoc#features.testing.testcontainers.dynamic-properties[`@DynamicPropertySource` annotation] that you can use in your tests.
It allows you to add properties that will become available once your container has started.
A typical configuration would look like this:
include::code:MyContainersConfiguration[]
include-code::MyContainersConfiguration[]
NOTE: Using a `@ServiceConnection` is recommended whenever possible, however, dynamic properties can be a useful fallback for technologies that don't yet have `@ServiceConnection` support.
[[features.testcontainers.at-development-time.importing-container-declarations]]
==== Importing Testcontainer Declaration Classes
=== Importing Testcontainer Declaration Classes
A common pattern when using Testcontainers is to declare `Container` instances as static fields.
Often these fields are defined directly on the test class.
They can also be declared on a parent class or on an interface that the test implements.
For example, the following `MyContainers` interface declares `mongo` and `neo4j` containers:
include::code:MyContainers[]
include-code::MyContainers[]
If you already have containers defined in this way, or you just prefer this style, you can import these declaration classes rather than defining you containers as `@Bean` methods.
To do so, add the `@ImportTestcontainers` annotation to your test configuration class:
include::code:MyContainersConfiguration[]
include-code::MyContainersConfiguration[]
TIP: If you don't intend to use the <<features#features.testing.testcontainers.service-connections, service connections feature>> but want to use <<features#features.testing.testcontainers.dynamic-properties, `@DynamicPropertySource`>> instead, remove the `@ServiceConnection` annotation from the `Container` fields.
TIP: If you don't intend to use the xref:features/testing.adoc#features.testing.testcontainers.service-connections[service connections feature] but want to use xref:features/testing.adoc#features.testing.testcontainers.dynamic-properties[`@DynamicPropertySource`] instead, remove the `@ServiceConnection` annotation from the `Container` fields.
You can also add `@DynamicPropertySource` annotated methods to your declaration class.
[[features.testcontainers.at-development-time.devtools]]
==== Using DevTools with Testcontainers at Development Time
=== Using DevTools with Testcontainers at Development Time
When using devtools, you can annotate beans and bean methods with `@RestartScope`.
Such beans won't be recreated when the devtools restart the application.
This is especially useful for Testcontainer `Container` beans, as they keep their state despite the application restart.
include::code:MyContainersConfiguration[]
include-code::MyContainersConfiguration[]
WARNING: If you're using Gradle and want to use this feature, you need to change the configuration of the `spring-boot-devtools` dependency from `developmentOnly` to `testAndDevelopmentOnly`.
With the default scope of `developmentOnly`, the `bootTestRun` task will not pick up changes in your code, as the devtools are not active.

View File

@ -1,15 +1,16 @@
[[io.caching]]
== Caching
= Caching
The Spring Framework provides support for transparently adding caching to an application.
At its core, the abstraction applies caching to methods, thus reducing the number of executions based on the information available in the cache.
The caching logic is applied transparently, without any interference to the invoker.
Spring Boot auto-configures the cache infrastructure as long as caching support is enabled by using the `@EnableCaching` annotation.
NOTE: Check the {spring-framework-docs}/integration/cache.html[relevant section] of the Spring Framework reference for more details.
NOTE: Check the {url-spring-framework-docs}/integration/cache.html[relevant section] of the Spring Framework reference for more details.
In a nutshell, to add caching to an operation of your service add the relevant annotation to its method, as shown in the following example:
include::code:MyMathService[]
include-code::MyMathService[]
This example demonstrates the use of caching on a potentially costly operation.
Before invoking `computePiDecimal`, the abstraction looks for an entry in the `piDecimals` cache that matches the `i` argument.
@ -19,37 +20,38 @@ Otherwise, the method is invoked, and the cache is updated before returning the
CAUTION: You can also use the standard JSR-107 (JCache) annotations (such as `@CacheResult`) transparently.
However, we strongly advise you to not mix and match the Spring Cache and JCache annotations.
If you do not add any specific cache library, Spring Boot auto-configures a <<io#io.caching.provider.simple,simple provider>> that uses concurrent maps in memory.
If you do not add any specific cache library, Spring Boot auto-configures a xref:io/caching.adoc#io.caching.provider.simple[simple provider] that uses concurrent maps in memory.
When a cache is required (such as `piDecimals` in the preceding example), this provider creates it for you.
The simple provider is not really recommended for production usage, but it is great for getting started and making sure that you understand the features.
When you have made up your mind about the cache provider to use, please make sure to read its documentation to figure out how to configure the caches that your application uses.
Nearly all providers require you to explicitly configure every cache that you use in the application.
Some offer a way to customize the default caches defined by the configprop:spring.cache.cache-names[] property.
TIP: It is also possible to transparently {spring-framework-docs}/integration/cache/annotations.html#cache-annotations-put[update] or {spring-framework-docs}/integration/cache/annotations.html#cache-annotations-evict[evict] data from the cache.
TIP: It is also possible to transparently {url-spring-framework-docs}/integration/cache/annotations.html#cache-annotations-put[update] or {url-spring-framework-docs}/integration/cache/annotations.html#cache-annotations-evict[evict] data from the cache.
[[io.caching.provider]]
=== Supported Cache Providers
== Supported Cache Providers
The cache abstraction does not provide an actual store and relies on abstraction materialized by the `org.springframework.cache.Cache` and `org.springframework.cache.CacheManager` interfaces.
If you have not defined a bean of type `CacheManager` or a `CacheResolver` named `cacheResolver` (see {spring-framework-api}/cache/annotation/CachingConfigurer.html[`CachingConfigurer`]), Spring Boot tries to detect the following providers (in the indicated order):
If you have not defined a bean of type `CacheManager` or a `CacheResolver` named `cacheResolver` (see {url-spring-framework-javadoc}/org/springframework/cache/annotation/CachingConfigurer.html[`CachingConfigurer`]), Spring Boot tries to detect the following providers (in the indicated order):
. <<io#io.caching.provider.generic,Generic>>
. <<io#io.caching.provider.jcache,JCache (JSR-107)>> (EhCache 3, Hazelcast, Infinispan, and others)
. <<io#io.caching.provider.hazelcast,Hazelcast>>
. <<io#io.caching.provider.infinispan,Infinispan>>
. <<io#io.caching.provider.couchbase,Couchbase>>
. <<io#io.caching.provider.redis,Redis>>
. <<io#io.caching.provider.caffeine,Caffeine>>
. <<io#io.caching.provider.cache2k,Cache2k>>
. <<io#io.caching.provider.simple,Simple>>
. xref:io/caching.adoc#io.caching.provider.generic[Generic]
. xref:io/caching.adoc#io.caching.provider.jcache[JCache (JSR-107)] (EhCache 3, Hazelcast, Infinispan, and others)
. xref:io/caching.adoc#io.caching.provider.hazelcast[Hazelcast]
. xref:io/caching.adoc#io.caching.provider.infinispan[Infinispan]
. xref:io/caching.adoc#io.caching.provider.couchbase[Couchbase]
. xref:io/caching.adoc#io.caching.provider.redis[Redis]
. xref:io/caching.adoc#io.caching.provider.caffeine[Caffeine]
. xref:io/caching.adoc#io.caching.provider.cache2k[Cache2k]
. xref:io/caching.adoc#io.caching.provider.simple[Simple]
Additionally, {spring-boot-for-apache-geode}[Spring Boot for Apache Geode] provides {spring-boot-for-apache-geode-docs}#geode-caching-provider[auto-configuration for using Apache Geode as a cache provider].
Additionally, {url-spring-boot-for-apache-geode-site}[Spring Boot for Apache Geode] provides {url-spring-boot-for-apache-geode-docs}#geode-caching-provider[auto-configuration for using Apache Geode as a cache provider].
TIP: If the `CacheManager` is auto-configured by Spring Boot, it is possible to _force_ a particular cache provider by setting the configprop:spring.cache.type[] property.
Use this property if you need to <<io#io.caching.provider.none,use no-op caches>> in certain environments (such as tests).
Use this property if you need to xref:io/caching.adoc#io.caching.provider.none[use no-op caches] in certain environments (such as tests).
TIP: Use the `spring-boot-starter-cache` "`Starter`" to quickly add basic caching dependencies.
The starter brings in `spring-context-support`.
@ -58,7 +60,7 @@ If you add dependencies manually, you must include `spring-context-support` in o
If the `CacheManager` is auto-configured by Spring Boot, you can further tune its configuration before it is fully initialized by exposing a bean that implements the `CacheManagerCustomizer` interface.
The following example sets a flag to say that `null` values should not be passed down to the underlying map:
include::code:MyCacheManagerConfiguration[]
include-code::MyCacheManagerConfiguration[]
NOTE: In the preceding example, an auto-configured `ConcurrentMapCacheManager` is expected.
If that is not the case (either you provided your own config or a different cache provider was auto-configured), the customizer is not invoked at all.
@ -67,14 +69,16 @@ You can have as many customizers as you want, and you can also order them by usi
[[io.caching.provider.generic]]
==== Generic
=== Generic
Generic caching is used if the context defines _at least_ one `org.springframework.cache.Cache` bean.
A `CacheManager` wrapping all beans of that type is created.
[[io.caching.provider.jcache]]
==== JCache (JSR-107)
=== JCache (JSR-107)
https://jcp.org/en/jsr/detail?id=107[JCache] is bootstrapped through the presence of a `javax.cache.spi.CachingProvider` on the classpath (that is, a JSR-107 compliant caching library exists on the classpath), and the `JCacheCacheManager` is provided by the `spring-boot-starter-cache` "`Starter`".
Various compliant libraries are available, and Spring Boot provides dependency management for Ehcache 3, Hazelcast, and Infinispan.
Any other compliant library can be added as well.
@ -82,7 +86,7 @@ Any other compliant library can be added as well.
It might happen that more than one provider is present, in which case the provider must be explicitly specified.
Even if the JSR-107 standard does not enforce a standardized way to define the location of the configuration file, Spring Boot does its best to accommodate setting a cache with implementation details, as shown in the following example:
[source,yaml,indent=0,subs="verbatim",configprops,configblocks]
[configprops,yaml]
----
# Only necessary if more than one provider is present
spring:
@ -94,7 +98,7 @@ Even if the JSR-107 standard does not enforce a standardized way to define the l
NOTE: When a cache library offers both a native implementation and JSR-107 support, Spring Boot prefers the JSR-107 support, so that the same features are available if you switch to a different JSR-107 implementation.
TIP: Spring Boot has <<io#io.hazelcast,general support for Hazelcast>>.
TIP: Spring Boot has xref:io/hazelcast.adoc[general support for Hazelcast].
If a single `HazelcastInstance` is available, it is automatically reused for the `CacheManager` as well, unless the configprop:spring.cache.jcache.config[] property is specified.
There are two ways to customize the underlying `javax.cache.cacheManager`:
@ -109,28 +113,30 @@ No further customization is applied to it.
[[io.caching.provider.hazelcast]]
==== Hazelcast
=== Hazelcast
Spring Boot has <<io#io.hazelcast,general support for Hazelcast>>.
Spring Boot has xref:io/hazelcast.adoc[general support for Hazelcast].
If a `HazelcastInstance` has been auto-configured and `com.hazelcast:hazelcast-spring` is on the classpath, it is automatically wrapped in a `CacheManager`.
NOTE: Hazelcast can be used as a JCache compliant cache or as a Spring `CacheManager` compliant cache.
When setting configprop:spring.cache.type[] to `hazelcast`, Spring Boot will use the `CacheManager` based implementation.
If you want to use Hazelcast as a JCache compliant cache, set configprop:spring.cache.type[] to `jcache`.
If you have multiple JCache compliant cache providers and want to force the use of Hazelcast, you have to <<io#io.caching.provider.jcache,explicitly set the JCache provider>>.
If you have multiple JCache compliant cache providers and want to force the use of Hazelcast, you have to xref:io/caching.adoc#io.caching.provider.jcache[explicitly set the JCache provider].
[[io.caching.provider.infinispan]]
==== Infinispan
=== Infinispan
https://infinispan.org/[Infinispan] has no default configuration file location, so it must be specified explicitly.
Otherwise, the default bootstrap is used.
[source,yaml,indent=0,subs="verbatim",configprops,configblocks]
[configprops,yaml]
----
spring:
cache:
infinispan:
config: "infinispan.xml"
spring:
cache:
infinispan:
config: "infinispan.xml"
----
Caches can be created on startup by setting the configprop:spring.cache.cache-names[] property.
@ -143,40 +149,42 @@ For example, `infinispan-core-jakarta` and `infinispan-commons-jakarta` must be
[[io.caching.provider.couchbase]]
==== Couchbase
If Spring Data Couchbase is available and Couchbase is <<data#data.nosql.couchbase,configured>>, a `CouchbaseCacheManager` is auto-configured.
=== Couchbase
If Spring Data Couchbase is available and Couchbase is xref:data/nosql.adoc#data.nosql.couchbase[configured], a `CouchbaseCacheManager` is auto-configured.
It is possible to create additional caches on startup by setting the configprop:spring.cache.cache-names[] property and cache defaults can be configured by using `spring.cache.couchbase.*` properties.
For instance, the following configuration creates `cache1` and `cache2` caches with an entry _expiration_ of 10 minutes:
[source,yaml,indent=0,subs="verbatim",configprops,configblocks]
[configprops,yaml]
----
spring:
cache:
cache-names: "cache1,cache2"
couchbase:
expiration: "10m"
spring:
cache:
cache-names: "cache1,cache2"
couchbase:
expiration: "10m"
----
If you need more control over the configuration, consider registering a `CouchbaseCacheManagerBuilderCustomizer` bean.
The following example shows a customizer that configures a specific entry expiration for `cache1` and `cache2`:
include::code:MyCouchbaseCacheManagerConfiguration[]
include-code::MyCouchbaseCacheManagerConfiguration[]
[[io.caching.provider.redis]]
==== Redis
=== Redis
If https://redis.io/[Redis] is available and configured, a `RedisCacheManager` is auto-configured.
It is possible to create additional caches on startup by setting the configprop:spring.cache.cache-names[] property and cache defaults can be configured by using `spring.cache.redis.*` properties.
For instance, the following configuration creates `cache1` and `cache2` caches with a _time to live_ of 10 minutes:
[source,yaml,indent=0,subs="verbatim",configprops,configblocks]
[configprops,yaml]
----
spring:
cache:
cache-names: "cache1,cache2"
redis:
time-to-live: "10m"
spring:
cache:
cache-names: "cache1,cache2"
redis:
time-to-live: "10m"
----
NOTE: By default, a key prefix is added so that, if two separate caches use the same key, Redis does not have overlapping keys and cannot return invalid values.
@ -188,12 +196,13 @@ This can be useful if you need to customize the default serialization strategy.
If you need more control over the configuration, consider registering a `RedisCacheManagerBuilderCustomizer` bean.
The following example shows a customizer that configures a specific time to live for `cache1` and `cache2`:
include::code:MyRedisCacheManagerConfiguration[]
include-code::MyRedisCacheManagerConfiguration[]
[[io.caching.provider.caffeine]]
==== Caffeine
=== Caffeine
https://github.com/ben-manes/caffeine[Caffeine] is a Java 8 rewrite of Guava's cache that supersedes support for Guava.
If Caffeine is present, a `CaffeineCacheManager` (provided by the `spring-boot-starter-cache` "`Starter`") is auto-configured.
Caches can be created on startup by setting the configprop:spring.cache.cache-names[] property and can be customized by one of the following (in the indicated order):
@ -204,13 +213,13 @@ Caches can be created on startup by setting the configprop:spring.cache.cache-na
For instance, the following configuration creates `cache1` and `cache2` caches with a maximum size of 500 and a _time to live_ of 10 minutes
[source,yaml,indent=0,subs="verbatim",configprops,configblocks]
[configprops,yaml]
----
spring:
cache:
cache-names: "cache1,cache2"
caffeine:
spec: "maximumSize=500,expireAfterAccess=600s"
spring:
cache:
cache-names: "cache1,cache2"
caffeine:
spec: "maximumSize=500,expireAfterAccess=600s"
----
If a `com.github.benmanes.caffeine.cache.CacheLoader` bean is defined, it is automatically associated to the `CaffeineCacheManager`.
@ -220,7 +229,8 @@ The auto-configuration ignores any other generic type.
[[io.caching.provider.cache2k]]
==== Cache2k
=== Cache2k
https://cache2k.org/[Cache2k] is an in-memory cache.
If the Cache2k spring integration is present, a `SpringCache2kCacheManager` is auto-configured.
@ -228,22 +238,23 @@ Caches can be created on startup by setting the configprop:spring.cache.cache-na
Cache defaults can be customized using a `Cache2kBuilderCustomizer` bean.
The following example shows a customizer that configures the capacity of the cache to 200 entries, with an expiration of 5 minutes:
include::code:MyCache2kDefaultsConfiguration[]
include-code::MyCache2kDefaultsConfiguration[]
[[io.caching.provider.simple]]
==== Simple
=== Simple
If none of the other providers can be found, a simple implementation using a `ConcurrentHashMap` as the cache store is configured.
This is the default if no caching library is present in your application.
By default, caches are created as needed, but you can restrict the list of available caches by setting the `cache-names` property.
For instance, if you want only `cache1` and `cache2` caches, set the `cache-names` property as follows:
[source,yaml,indent=0,subs="verbatim",configprops,configblocks]
[configprops,yaml]
----
spring:
cache:
cache-names: "cache1,cache2"
spring:
cache:
cache-names: "cache1,cache2"
----
If you do so and your application uses a cache not listed, then it fails at runtime when the cache is needed, but not on startup.
@ -252,16 +263,17 @@ This is similar to the way the "real" cache providers behave if you use an undec
[[io.caching.provider.none]]
==== None
=== None
When `@EnableCaching` is present in your configuration, a suitable cache configuration is expected as well.
If you have a custom `CacheManager`, consider defining it in a separate `@Configuration` class so that you can override it if necessary.
None uses a no-op implementation that is useful in tests, and slice tests use that by default via `@AutoConfigureCache`.
If you need to use a no-op cache rather than the auto-configured cache manager in a certain environment, set the cache type to `none`, as shown in the following example:
[source,yaml,indent=0,subs="verbatim",configprops,configblocks]
[configprops,yaml]
----
spring:
cache:
type: "none"
spring:
cache:
type: "none"
----

View File

@ -1,32 +1,33 @@
[[io.email]]
== Sending Email
= Sending Email
The Spring Framework provides an abstraction for sending email by using the `JavaMailSender` interface, and Spring Boot provides auto-configuration for it as well as a starter module.
TIP: See the {spring-framework-docs}/integration/email.html[reference documentation] for a detailed explanation of how you can use `JavaMailSender`.
TIP: See the {url-spring-framework-docs}/integration/email.html[reference documentation] for a detailed explanation of how you can use `JavaMailSender`.
If `spring.mail.host` and the relevant libraries (as defined by `spring-boot-starter-mail`) are available, a default `JavaMailSender` is created if none exists.
The sender can be further customized by configuration items from the `spring.mail` namespace.
See {spring-boot-autoconfigure-module-code}/mail/MailProperties.java[`MailProperties`] for more details.
See {code-spring-boot-autoconfigure-src}/mail/MailProperties.java[`MailProperties`] for more details.
In particular, certain default timeout values are infinite, and you may want to change that to avoid having a thread blocked by an unresponsive mail server, as shown in the following example:
[source,yaml,indent=0,subs="verbatim",configprops,configblocks]
[configprops,yaml]
----
spring:
mail:
properties:
"[mail.smtp.connectiontimeout]": 5000
"[mail.smtp.timeout]": 3000
"[mail.smtp.writetimeout]": 5000
spring:
mail:
properties:
"[mail.smtp.connectiontimeout]": 5000
"[mail.smtp.timeout]": 3000
"[mail.smtp.writetimeout]": 5000
----
It is also possible to configure a `JavaMailSender` with an existing `Session` from JNDI:
[source,yaml,indent=0,subs="verbatim",configprops,configblocks]
[configprops,yaml]
----
spring:
mail:
jndi-name: "mail/Session"
spring:
mail:
jndi-name: "mail/Session"
----
When a `jndi-name` is set, it takes precedence over all other Session-related settings.

View File

@ -1,5 +1,6 @@
[[io.hazelcast]]
== Hazelcast
= Hazelcast
If https://hazelcast.com/[Hazelcast] is on the classpath and a suitable configuration is found, Spring Boot auto-configures a `HazelcastInstance` that you can inject in your application.
Spring Boot first attempts to create a client by checking the following configuration options:
@ -16,11 +17,11 @@ If your configuration defines an instance name, Spring Boot tries to locate an e
You could also specify the Hazelcast configuration file to use through configuration, as shown in the following example:
[source,yaml,indent=0,subs="verbatim",configprops,configblocks]
[configprops,yaml]
----
spring:
hazelcast:
config: "classpath:config/my-hazelcast.xml"
spring:
hazelcast:
config: "classpath:config/my-hazelcast.xml"
----
Otherwise, Spring Boot tries to find the Hazelcast configuration from the default locations: `hazelcast.xml` in the working directory or at the root of the classpath, or a YAML counterpart in the same locations.
@ -30,5 +31,5 @@ See the https://docs.hazelcast.org/docs/latest/manual/html-single/[Hazelcast doc
TIP: By default, `@SpringAware` on Hazelcast components is supported.
The `ManagementContext` can be overridden by declaring a `HazelcastConfigCustomizer` bean with an `@Order` higher than zero.
NOTE: Spring Boot also has <<io#io.caching.provider.hazelcast,explicit caching support for Hazelcast>>.
NOTE: Spring Boot also has xref:io/caching.adoc#io.caching.provider.hazelcast[explicit caching support for Hazelcast].
If caching is enabled, the `HazelcastInstance` is automatically wrapped in a `CacheManager` implementation.

View File

@ -1,26 +1,8 @@
[[io]]
= IO
include::attributes.adoc[]
Most applications will need to deal with input and output concerns at some point.
Spring Boot provides utilities and integrations with a range of technologies to help when you need IO capabilities.
This section covers standard IO features such as caching and validation as well as more advanced topics such as scheduling and distributed transactions.
We will also cover calling remote REST or SOAP services and sending email.
include::io/caching.adoc[]
include::io/hazelcast.adoc[]
include::io/quartz.adoc[]
include::io/email.adoc[]
include::io/validation.adoc[]
include::io/rest-client.adoc[]
include::io/webservices.adoc[]
include::io/jta.adoc[]
include::io/whats-next.adoc[]

View File

@ -1,5 +1,6 @@
[[io.jta]]
== Distributed Transactions With JTA
= Distributed Transactions With JTA
Spring Boot supports distributed JTA transactions across multiple XA resources by using a transaction manager retrieved from JNDI.
When a JTA environment is detected, Spring's `JtaTransactionManager` is used to manage transactions.
@ -10,36 +11,39 @@ If you are within a JTA environment and still want to use local transactions, yo
[[io.jta.jakartaee]]
=== Using a Jakarta EE Managed Transaction Manager
== Using a Jakarta EE Managed Transaction Manager
If you package your Spring Boot application as a `war` or `ear` file and deploy it to a Jakarta EE application server, you can use your application server's built-in transaction manager.
Spring Boot tries to auto-configure a transaction manager by looking at common JNDI locations (`java:comp/UserTransaction`, `java:comp/TransactionManager`, and so on).
When using a transaction service provided by your application server, you generally also want to ensure that all resources are managed by the server and exposed over JNDI.
Spring Boot tries to auto-configure JMS by looking for a `ConnectionFactory` at the JNDI path (`java:/JmsXA` or `java:/XAConnectionFactory`), and you can use the <<data#data.sql.datasource.jndi, configprop:spring.datasource.jndi-name[] property>> to configure your `DataSource`.
Spring Boot tries to auto-configure JMS by looking for a `ConnectionFactory` at the JNDI path (`java:/JmsXA` or `java:/XAConnectionFactory`), and you can use the xref:data/sql.adoc#data.sql.datasource.jndi[configprop:spring.datasource.jndi-name[] property] to configure your `DataSource`.
[[io.jta.mixing-xa-and-non-xa-connections]]
=== Mixing XA and Non-XA JMS Connections
== Mixing XA and Non-XA JMS Connections
When using JTA, the primary JMS `ConnectionFactory` bean is XA-aware and participates in distributed transactions.
You can inject into your bean without needing to use any `@Qualifier`:
include::code:primary/MyBean[tag=*]
include-code::primary/MyBean[tag=*]
In some situations, you might want to process certain JMS messages by using a non-XA `ConnectionFactory`.
For example, your JMS processing logic might take longer than the XA timeout.
If you want to use a non-XA `ConnectionFactory`, you can the `nonXaJmsConnectionFactory` bean:
include::code:nonxa/MyBean[tag=*]
include-code::nonxa/MyBean[tag=*]
For consistency, the `jmsConnectionFactory` bean is also provided by using the bean alias `xaJmsConnectionFactory`:
include::code:xa/MyBean[tag=*]
include-code::xa/MyBean[tag=*]
[[io.jta.supporting-embedded-transaction-manager]]
=== Supporting an Embedded Transaction Manager
The {spring-boot-module-code}/jms/XAConnectionFactoryWrapper.java[`XAConnectionFactoryWrapper`] and {spring-boot-module-code}/jdbc/XADataSourceWrapper.java[`XADataSourceWrapper`] interfaces can be used to support embedded transaction managers.
== Supporting an Embedded Transaction Manager
The {code-spring-boot-src}/jms/XAConnectionFactoryWrapper.java[`XAConnectionFactoryWrapper`] and {code-spring-boot-src}/jdbc/XADataSourceWrapper.java[`XADataSourceWrapper`] interfaces can be used to support embedded transaction managers.
The interfaces are responsible for wrapping `XAConnectionFactory` and `XADataSource` beans and exposing them as regular `ConnectionFactory` and `DataSource` beans, which transparently enroll in the distributed transaction.
DataSource and JMS auto-configuration use JTA variants, provided you have a `JtaTransactionManager` bean and appropriate XA wrapper beans registered within your `ApplicationContext`.

View File

@ -1,5 +1,6 @@
[[io.quartz]]
== Quartz Scheduler
= Quartz Scheduler
Spring Boot offers several conveniences for working with the https://www.quartz-scheduler.org/[Quartz scheduler], including the `spring-boot-starter-quartz` "`Starter`".
If Quartz is available, a `Scheduler` is auto-configured (through the `SchedulerFactoryBean` abstraction).
@ -13,21 +14,21 @@ Beans of the following types are automatically picked up and associated with the
By default, an in-memory `JobStore` is used.
However, it is possible to configure a JDBC-based store if a `DataSource` bean is available in your application and if the configprop:spring.quartz.job-store-type[] property is configured accordingly, as shown in the following example:
[source,yaml,indent=0,subs="verbatim",configprops,configblocks]
[configprops,yaml]
----
spring:
quartz:
job-store-type: "jdbc"
spring:
quartz:
job-store-type: "jdbc"
----
When the JDBC store is used, the schema can be initialized on startup, as shown in the following example:
[source,yaml,indent=0,subs="verbatim",configprops,configblocks]
[configprops,yaml]
----
spring:
quartz:
jdbc:
initialize-schema: "always"
spring:
quartz:
jdbc:
initialize-schema: "always"
----
WARNING: By default, the database is detected and initialized by using the standard scripts provided with the Quartz library.
@ -50,4 +51,4 @@ If you need to customize the task executor, consider implementing `SchedulerFact
Jobs can define setters to inject data map properties.
Regular beans can also be injected in a similar manner, as shown in the following example:
include::code:MySampleJob[]
include-code::MySampleJob[]

Some files were not shown because too many files have changed in this diff Show More