diff --git a/spring-boot-project/spring-boot-docs/src/docs/asciidoc/appendix-configuration-metadata.adoc b/spring-boot-project/spring-boot-docs/src/docs/asciidoc/appendix-configuration-metadata.adoc index 0fbf4639688..0a8fa6f4872 100644 --- a/spring-boot-project/spring-boot-docs/src/docs/asciidoc/appendix-configuration-metadata.adoc +++ b/spring-boot-project/spring-boot-docs/src/docs/asciidoc/appendix-configuration-metadata.adoc @@ -220,7 +220,7 @@ Deprecation can also be specified declaratively in code by adding the `@Deprecat For instance, assume that the `app.acme.target` property was confusing and was renamed to `app.acme.name`. The following example shows how to handle that situation: -[source,java,indent=0] +[source,java,pending-extract=true,indent=0] ---- @ConfigurationProperties("app.acme") public class AcmeProperties { @@ -343,7 +343,7 @@ The special `.keys` and `.values` suffixes must refer to the keys and the values Assume a `sample.contexts` maps magic `String` values to an integer, as shown in the following example: -[source,java,indent=0] +[source,java,pending-extract=true,indent=0] ---- @ConfigurationProperties("sample") public class SampleProperties { @@ -762,7 +762,7 @@ The annotation processor also supports the use of the `@Data`, `@Getter`, and `@ Consider the following example: -[source,java,indent=0,subs="verbatim,attributes"] +[source,java,pending-extract=true,indent=0,subs="verbatim,attributes"] ---- @ConfigurationProperties(prefix="server") public class ServerProperties { @@ -799,7 +799,7 @@ Also, the annotation processor cannot auto-detect default values for ``Enum``s a For cases where the default value could not be detected, <> should be provided. Consider the following example: -[source,java,indent=0,subs="verbatim,quotes,attributes"] +[source,java,pending-extract=true,indent=0,subs="verbatim,quotes,attributes"] ---- @ConfigurationProperties(prefix = "acme.messaging") public class MessagingProperties { @@ -845,7 +845,7 @@ The annotation processor automatically considers inner classes as nested propert Rather than documenting the `ip` and `port` at the root of the namespace, we could create a sub-namespace for it. Consider the updated example: -[source,java,indent=0,subs="verbatim,quotes,attributes"] +[source,java,pending-extract=true,indent=0,subs="verbatim,quotes,attributes"] ---- @ConfigurationProperties(prefix="server") public class ServerProperties { diff --git a/spring-boot-project/spring-boot-docs/src/docs/asciidoc/build-tool-plugins.adoc b/spring-boot-project/spring-boot-docs/src/docs/asciidoc/build-tool-plugins.adoc index 9cb3488060e..2b086d1dc34 100644 --- a/spring-boot-project/spring-boot-docs/src/docs/asciidoc/build-tool-plugins.adoc +++ b/spring-boot-project/spring-boot-docs/src/docs/asciidoc/build-tool-plugins.adoc @@ -223,7 +223,7 @@ An exception is thrown if more than one candidate is found. === Example Repackage Implementation The following example shows a typical repackage implementation: -[source,java,indent=0] +[source,java,pending-extract=true,indent=0] ---- Repackager repackager = new Repackager(sourceJarFile); repackager.setBackupSource(false); diff --git a/spring-boot-project/spring-boot-docs/src/docs/asciidoc/deployment.adoc b/spring-boot-project/spring-boot-docs/src/docs/asciidoc/deployment.adoc index 8a42e7eb9ef..d6879eee5ac 100644 --- a/spring-boot-project/spring-boot-docs/src/docs/asciidoc/deployment.adoc +++ b/spring-boot-project/spring-boot-docs/src/docs/asciidoc/deployment.adoc @@ -135,7 +135,7 @@ 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: -[source,java,indent=0] +[source,java,pending-extract=true,indent=0] ---- @Component class MyBean implements EnvironmentAware { diff --git a/spring-boot-project/spring-boot-docs/src/docs/asciidoc/getting-started.adoc b/spring-boot-project/spring-boot-docs/src/docs/asciidoc/getting-started.adoc index d359898a112..b030d52e22d 100644 --- a/spring-boot-project/spring-boot-docs/src/docs/asciidoc/getting-started.adoc +++ b/spring-boot-project/spring-boot-docs/src/docs/asciidoc/getting-started.adoc @@ -483,7 +483,7 @@ If you run `mvn dependency:tree` again, you see that there are now a number of a To finish our application, we need to create a single Java file. By default, Maven compiles sources from `src/main/java`, so you need to create that directory structure and then add a file named `src/main/java/Example.java` to contain the following code: -[source,java,indent=0] +[source,java,pending-extract=true,indent=0] ---- import org.springframework.boot.*; import org.springframework.boot.autoconfigure.*; diff --git a/spring-boot-project/spring-boot-docs/src/docs/asciidoc/howto.adoc b/spring-boot-project/spring-boot-docs/src/docs/asciidoc/howto.adoc index b183f5f3bfb..481c0762e21 100644 --- a/spring-boot-project/spring-boot-docs/src/docs/asciidoc/howto.adoc +++ b/spring-boot-project/spring-boot-docs/src/docs/asciidoc/howto.adoc @@ -239,7 +239,7 @@ Then the Spring Boot banner is not printed on startup, and the application is no Properties defined in external configuration override the values specified with the Java API, with the notable exception of the sources used to create the `ApplicationContext`. Consider the following application: -[source,java,indent=0] +[source,java,pending-extract=true,indent=0] ---- new SpringApplicationBuilder() .bannerMode(Banner.Mode.OFF) @@ -513,7 +513,7 @@ The best way to get that and be sure it has been initialized is to add a `@Bean` 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: -[source,java,indent=0,subs="verbatim,quotes,attributes"] +[source,java,pending-extract=true,indent=0,subs="verbatim,quotes,attributes"] ---- @SpringBootTest(webEnvironment=WebEnvironment.RANDOM_PORT) public class MyWebIntegrationTests { @@ -658,7 +658,7 @@ and instead apply a customizer specific to your choice of server: For Tomcat, we need to add an upgrade protocol: -[source,java,indent=0,subs="verbatim,quotes,attributes"] +[source,java,pending-extract=true,indent=0,subs="verbatim,quotes,attributes"] ---- @Bean public TomcatConnectorCustomizer connectorCustomizer() { @@ -668,7 +668,7 @@ For Tomcat, we need to add an upgrade protocol: For Jetty, we need to add a connection factory to the existing connector: -[source,java,indent=0,subs="verbatim,quotes,attributes"] +[source,java,pending-extract=true,indent=0,subs="verbatim,quotes,attributes"] ---- @Bean public JettyServerCustomizer serverCustomizer() { @@ -687,7 +687,7 @@ For Jetty, we need to add a connection factory to the existing connector: For Netty, we need to add `h2c` as a supported protocol: -[source,java,indent=0,subs="verbatim,quotes,attributes"] +[source,java,pending-extract=true,indent=0,subs="verbatim,quotes,attributes"] ---- @Bean public NettyServerCustomizer serverCustomizer() { @@ -697,7 +697,7 @@ For Netty, we need to add `h2c` as a supported protocol: For Undertow, we need to enable the HTTP2 option: -[source,java,indent=0,subs="verbatim,quotes,attributes"] +[source,java,pending-extract=true,indent=0,subs="verbatim,quotes,attributes"] ---- @Bean public UndertowBuilderCustomizer builderCustomizer() { @@ -721,7 +721,7 @@ You can declare such a component and get access to the server factory relevant t The example below is for Tomcat with the `spring-boot-starter-web` (Servlet stack): -[source,java,indent=0,subs="verbatim,quotes,attributes"] +[source,java,pending-extract=true,indent=0,subs="verbatim,quotes,attributes"] ---- @Component public class MyTomcatWebServerCustomizer @@ -802,7 +802,7 @@ Like any other Spring bean, you can define the order of Servlet filter beans; pl As <>, 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: -[source,java,indent=0,subs="verbatim,quotes,attributes"] +[source,java,pending-extract=true,indent=0,subs="verbatim,quotes,attributes"] ---- @Bean public FilterRegistrationBean registration(MyFilter filter) { @@ -987,7 +987,7 @@ include::{include-howto}/embeddedwebservers/UndertowMultipleListenersConfigurati === 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: -[source,java,indent=0,subs="verbatim,quotes,attributes"] +[source,java,pending-extract=true,indent=0,subs="verbatim,quotes,attributes"] ---- @Bean public ServerEndpointExporter serverEndpointExporter() { @@ -1012,7 +1012,7 @@ This section answers common questions about Spring MVC and Spring Boot. === 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: -[source,java,indent=0,subs="verbatim,quotes,attributes"] +[source,java,pending-extract=true,indent=0,subs="verbatim,quotes,attributes"] ---- @RestController public class MyController { @@ -1046,7 +1046,7 @@ To use the Jackson XML renderer, add the following dependency to your project: 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: -[source,java,indent=0,subs="verbatim,quotes,attributes"] +[source,java,pending-extract=true,indent=0,subs="verbatim,quotes,attributes"] ---- @XmlRootElement public class MyThing { @@ -1251,7 +1251,7 @@ For more detail, see the following sections: 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. -[source,java,indent=0] +[source,java,pending-extract=true,indent=0] ---- @Test @WithMockUser(roles="ADMIN") @@ -1294,7 +1294,7 @@ To use Jersey alongside another web framework, such as Spring MVC, it should be 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,quotes,attributes"] +[source,java,pending-extract=true,indent=0,subs="verbatim,quotes,attributes"] ---- @Component public class JerseyConfig extends ResourceConfig { @@ -1551,7 +1551,7 @@ If you need to externalize some settings, you can bind your `DataSource` to the The following example shows how to define a data source in a bean: -[source,java,indent=0,subs="verbatim,quotes,attributes"] +[source,java,pending-extract=true,indent=0,subs="verbatim,quotes,attributes"] ---- @Bean @ConfigurationProperties(prefix="app.datasource") @@ -1729,7 +1729,7 @@ For more about Spring Data, see the {spring-data}[Spring Data project page]. Spring Boot tries to guess the location of your `@Entity` definitions, based on the `@EnableAutoConfiguration` it finds. To get more control, you can use the `@EntityScan` annotation, as shown in the following example: -[source,java,indent=0,subs="verbatim,quotes,attributes"] +[source,java,pending-extract=true,indent=0,subs="verbatim,quotes,attributes"] ---- @Configuration(proxyBeanMethods = false) @EnableAutoConfiguration @@ -1806,7 +1806,7 @@ If you prefer to use Hibernate 5's default instead, set the following property: Alternatively, you can configure the following bean: -[source,java,indent=0,subs="verbatim,quotes,attributes"] +[source,java,pending-extract=true,indent=0,subs="verbatim,quotes,attributes"] ---- @Bean public PhysicalNamingStrategy physicalNamingStrategy() { @@ -1858,7 +1858,7 @@ Even if the default `EntityManagerFactory` works fine, you need to define a new You can use the `EntityManagerBuilder` provided by Spring Boot to help you to create one. Alternatively, you can use the `LocalContainerEntityManagerFactoryBean` directly from Spring ORM, as shown in the following example: -[source,java,indent=0,subs="verbatim,quotes,attributes"] +[source,java,pending-extract=true,indent=0,subs="verbatim,quotes,attributes"] ---- // add two data sources configured as above @@ -1896,7 +1896,7 @@ 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 example: -[source,java,indent=0,subs="verbatim,quotes,attributes"] +[source,java,pending-extract=true,indent=0,subs="verbatim,quotes,attributes"] ---- @Configuration(proxyBeanMethods = false) @EnableJpaRepositories(basePackageClasses = Customer.class, @@ -2206,7 +2206,7 @@ If your JMS broker does not support transacted sessions, you have to disable the 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: -[source,java,indent=0] +[source,java,pending-extract=true,indent=0] ---- @Bean public DefaultJmsListenerContainerFactory jmsListenerContainerFactory( @@ -2399,7 +2399,7 @@ 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: -[source,java,indent=0,subs="verbatim,quotes,attributes"] +[source,java,pending-extract=true,indent=0,subs="verbatim,quotes,attributes"] ---- @Bean public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception { @@ -2798,7 +2798,7 @@ 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: -[source,java,indent=0,subs="verbatim,quotes,attributes"] +[source,java,pending-extract=true,indent=0,subs="verbatim,quotes,attributes"] ---- @SpringBootApplication public class Application extends SpringBootServletInitializer { @@ -2875,7 +2875,7 @@ See the https://spring.io/guides/gs/convert-jar-to-war/[Getting Started Guide on 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: -[source,java,indent=0,subs="verbatim,quotes,attributes"] +[source,java,pending-extract=true,indent=0,subs="verbatim,quotes,attributes"] ---- @SpringBootApplication public class Application extends SpringBootServletInitializer { @@ -2908,7 +2908,7 @@ 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: -[source,java,indent=0,subs="verbatim,quotes,attributes"] +[source,java,pending-extract=true,indent=0,subs="verbatim,quotes,attributes"] ---- public static void main(String[] args) { SpringApplication.run(Application.class, args); @@ -2919,7 +2919,7 @@ Once the war file is working, you can make it executable by adding a `main` meth ==== 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: -[source,java,indent=0,subs="verbatim,quotes,attributes"] +[source,java,pending-extract=true,indent=0,subs="verbatim,quotes,attributes"] ---- @SpringBootApplication public class Application extends SpringBootServletInitializer { @@ -2969,7 +2969,7 @@ To deploy a Spring Boot application to WebLogic, you must ensure that your servl A typical initializer for WebLogic should resemble the following example: -[source,java,indent=0,subs="verbatim,quotes,attributes"] +[source,java,pending-extract=true,indent=0,subs="verbatim,quotes,attributes"] ---- import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; @@ -3052,7 +3052,7 @@ It integrates with JUnit, allowing you to write a test class that can start up a Testcontainers is especially useful for writing integration tests that talk to a real backend service such as MySQL, MongoDB, Cassandra etc. Testcontainers can be used in a Spring Boot test as follows: -[source,java,indent=0,subs="verbatim,quotes,attributes"] +[source,java,pending-extract=true,indent=0,subs="verbatim,quotes,attributes"] ---- @SpringBootTest @Testcontainers @@ -3069,7 +3069,7 @@ In most cases, you will need to configure the application using details from the This can be done with a static `@DynamicPropertySource` method that allows adding dynamic property values to the Spring Environment. -[source,java,indent=0,subs="verbatim,quotes,attributes"] +[source,java,pending-extract=true,indent=0,subs="verbatim,quotes,attributes"] ---- @SpringBootTest @Testcontainers diff --git a/spring-boot-project/spring-boot-docs/src/docs/asciidoc/production-ready-features.adoc b/spring-boot-project/spring-boot-docs/src/docs/asciidoc/production-ready-features.adoc index 60d68337129..e3fd322882a 100644 --- a/spring-boot-project/spring-boot-docs/src/docs/asciidoc/production-ready-features.adoc +++ b/spring-boot-project/spring-boot-docs/src/docs/asciidoc/production-ready-features.adoc @@ -357,7 +357,7 @@ If you wish to configure custom security for HTTP endpoints, for example, only a A typical Spring Security configuration might look something like the following example: -[source,java,indent=0] +[source,java,pending-extract=true,indent=0] ---- @Bean public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception { @@ -386,7 +386,7 @@ You can do so by changing the configprop:management.endpoints.web.exposure.inclu Additionally, if Spring Security is present, you would need to add custom security configuration that allows unauthenticated access to the endpoints as shown in the following example: -[source,java,indent=0] +[source,java,pending-extract=true,indent=0] ---- @Bean public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception { @@ -776,7 +776,7 @@ You need to provide an implementation of the `health()` method and return a `Hea The `Health` response should include a status and can optionally include additional details to be displayed. The following code shows a sample `HealthIndicator` implementation: -[source,java,indent=0] +[source,java,pending-extract=true,indent=0] ---- import org.springframework.boot.actuate.health.Health; import org.springframework.boot.actuate.health.HealthIndicator; @@ -870,7 +870,7 @@ If you need to register a regular `HealthContributor`, you should wrap it using To provide custom health information from a reactive API, you can register Spring beans that implement the {spring-boot-actuator-module-code}/health/ReactiveHealthIndicator.java[`ReactiveHealthIndicator`] interface. The following code shows a sample `ReactiveHealthIndicator` implementation: -[source,java,indent=0] +[source,java,pending-extract=true,indent=0] ---- @Component public class MyReactiveHealthIndicator implements ReactiveHealthIndicator { @@ -1226,7 +1226,7 @@ To provide custom application information, you can register Spring beans that im The following example contributes an `example` entry with a single value: -[source,java,indent=0] +[source,java,pending-extract=true,indent=0] ---- import java.util.Collections; @@ -1612,7 +1612,7 @@ Spring Boot will also add any auto-configured registries to the global static co You can register any number of `MeterRegistryCustomizer` beans to further configure the registry, such as applying common tags, before any meters are registered with the registry: -[source,java,indent=0] +[source,java,pending-extract=true,indent=0] ---- @Bean MeterRegistryCustomizer metricsCommonTags() { @@ -1622,7 +1622,7 @@ You can register any number of `MeterRegistryCustomizer` beans to further config You can apply customizations to particular registry implementations by being more specific about the generic type: -[source,java,indent=0] +[source,java,pending-extract=true,indent=0] ---- @Bean MeterRegistryCustomizer graphiteMetricsNamingConvention() { @@ -1780,12 +1780,12 @@ Micrometer provides a default `HierarchicalNameMapper` that governs how a dimens TIP: To take control over this behaviour, define your `GraphiteMeterRegistry` and supply your own `HierarchicalNameMapper`. An auto-configured `GraphiteConfig` and `Clock` beans are provided unless you define your own: -[source,java] +[source,java,pending-extract=true,indent=0] ---- -@Bean -public GraphiteMeterRegistry graphiteMeterRegistry(GraphiteConfig config, Clock clock) { - return new GraphiteMeterRegistry(config, clock, MY_HIERARCHICAL_MAPPER); -} + @Bean + public GraphiteMeterRegistry graphiteMeterRegistry(GraphiteConfig config, Clock clock) { + return new GraphiteMeterRegistry(config, clock, MY_HIERARCHICAL_MAPPER); + } ---- @@ -1855,12 +1855,12 @@ Micrometer provides a default `HierarchicalNameMapper` that governs how a dimens TIP: To take control over this behaviour, define your `JmxMeterRegistry` and supply your own `HierarchicalNameMapper`. An auto-configured `JmxConfig` and `Clock` beans are provided unless you define your own: -[source,java] +[source,java,pending-extract=true,indent=0] ---- -@Bean -public JmxMeterRegistry jmxMeterRegistry(JmxConfig config, Clock clock) { - return new JmxMeterRegistry(config, clock, MY_HIERARCHICAL_MAPPER); -} + @Bean + public JmxMeterRegistry jmxMeterRegistry(JmxConfig config, Clock clock) { + return new JmxMeterRegistry(config, clock, MY_HIERARCHICAL_MAPPER); + } ---- @@ -2128,7 +2128,7 @@ Auto-configuration enables the instrumentation of requests handled by Spring MVC When `management.metrics.web.server.request.autotime.enabled` is `true`, this instrumentation occurs for all requests. Alternatively, when set to `false`, you can enable instrumentation by adding `@Timed` to a request-handling method: -[source,java,indent=0] +[source,java,pending-extract=true,indent=0] ---- @RestController @Timed <1> @@ -2217,7 +2217,7 @@ When Micrometer's `micrometer-jersey2` module is on the classpath, auto-configur When `management.metrics.web.server.request.autotime.enabled` is `true`, this instrumentation occurs for all requests. Alternatively, when set to `false`, you can enable instrumentation by adding `@Timed` to a request-handling method: -[source,java,indent=0] +[source,java,pending-extract=true,indent=0] ---- @Component @Path("/api/people") diff --git a/spring-boot-project/spring-boot-docs/src/docs/asciidoc/spring-boot-cli.adoc b/spring-boot-project/spring-boot-docs/src/docs/asciidoc/spring-boot-cli.adoc index 33fbb59ef58..dfd82eda5e5 100644 --- a/spring-boot-project/spring-boot-docs/src/docs/asciidoc/spring-boot-cli.adoc +++ b/spring-boot-project/spring-boot-docs/src/docs/asciidoc/spring-boot-cli.adoc @@ -209,7 +209,7 @@ The preceding declaration picks up `custom-bom-1.0.0.pom` in a Maven repository When you specify multiple BOMs, they are applied in the order in which you declare them, as shown in the following example: -[source,java,indent=0] +[source,java,pending-extract=true,indent=0] ---- @DependencyManagementBom(["com.example.custom-bom:1.0.0", "com.example.another-bom:1.0.0"]) diff --git a/spring-boot-project/spring-boot-docs/src/docs/asciidoc/spring-boot-features.adoc b/spring-boot-project/spring-boot-docs/src/docs/asciidoc/spring-boot-features.adoc index 88721e271f3..86ae9c684fe 100644 --- a/spring-boot-project/spring-boot-docs/src/docs/asciidoc/spring-boot-features.adoc +++ b/spring-boot-project/spring-boot-docs/src/docs/asciidoc/spring-boot-features.adoc @@ -13,7 +13,7 @@ If you have not already done so, you might want to read the "<` from `AcmeProperties`: -[source,java,indent=0] +[source,java,pending-extract=true,indent=0] ---- @ConfigurationProperties("acme") public class AcmeProperties { @@ -1784,7 +1784,7 @@ Spring Boot attempts to validate `@ConfigurationProperties` classes whenever the You can use JSR-303 `javax.validation` constraint annotations directly on your configuration class. To do so, ensure that a compliant JSR-303 implementation is on your classpath and then add constraint annotations to your fields, as shown in the following example: -[source,java,indent=0] +[source,java,pending-extract=true,indent=0] ---- @ConfigurationProperties(prefix="acme") @Validated @@ -1803,7 +1803,7 @@ TIP: You can also trigger validation by annotating the `@Bean` method that creat To ensure that validation is always triggered for nested properties, even when no properties are found, the associated field must be annotated with `@Valid`. The following example builds on the preceding `AcmeProperties` example: -[source,java,indent=0] +[source,java,pending-extract=true,indent=0] ---- @ConfigurationProperties(prefix="acme") @Validated @@ -1881,7 +1881,7 @@ If the value of a property from an application property file is a `SpEL` express 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: -[source,java,indent=0] +[source,java,pending-extract=true,indent=0] ---- @Configuration(proxyBeanMethods = false) @Profile("production") @@ -2522,7 +2522,7 @@ Methods in your controller are mapped to HTTP by using `@RequestMapping` annotat The following code shows a typical `@RestController` that serves JSON data: -[source,java,indent=0] +[source,java,pending-extract=true,indent=0] ---- @RestController @RequestMapping(value="/users") @@ -2591,7 +2591,7 @@ By default, strings are encoded in `UTF-8`. If you need to add or customize converters, you can use Spring Boot's `HttpMessageConverters` class, as shown in the following listing: -[source,java,indent=0] +[source,java,pending-extract=true,indent=0] ---- import org.springframework.boot.autoconfigure.http.HttpMessageConverters; import org.springframework.context.annotation.*; @@ -2623,7 +2623,7 @@ Custom serializers are usually https://github.com/FasterXML/jackson-docs/wiki/Ja 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: -[source,java,indent=0] +[source,java,pending-extract=true,indent=0] ---- import java.io.*; import com.fasterxml.jackson.core.*; @@ -2888,7 +2888,7 @@ To do so, extend `BasicErrorController`, add a public method with a `@RequestMap You can also define a class annotated with `@ControllerAdvice` to customize the JSON document to return for a particular controller and/or exception type, as shown in the following example: -[source,java,indent=0,subs="verbatim,quotes,attributes"] +[source,java,pending-extract=true,indent=0,subs="verbatim,quotes,attributes"] ---- @ControllerAdvice(basePackageClasses = AcmeController.class) public class AcmeControllerAdvice extends ResponseEntityExceptionHandler { @@ -2953,7 +2953,7 @@ To map all `5xx` errors by using a FreeMarker template, your directory structure For more complex mappings, you can also add beans that implement the `ErrorViewResolver` interface, as shown in the following example: -[source,java,indent=0,subs="verbatim,quotes,attributes"] +[source,java,pending-extract=true,indent=0,subs="verbatim,quotes,attributes"] ---- public class MyErrorViewResolver implements ErrorViewResolver { @@ -2978,7 +2978,7 @@ The `ErrorController` then picks up any unhandled exceptions. For applications that do not use Spring MVC, you can use the `ErrorPageRegistrar` interface to directly register `ErrorPages`. This abstraction works directly with the underlying embedded servlet container and works even if you do not have a Spring MVC `DispatcherServlet`. -[source,java,indent=0,subs="verbatim,quotes,attributes"] +[source,java,pending-extract=true,indent=0,subs="verbatim,quotes,attributes"] ---- @Bean public ErrorPageRegistrar errorPageRegistrar(){ @@ -2999,7 +2999,7 @@ This abstraction works directly with the underlying embedded servlet container a NOTE: If you register an `ErrorPage` with a path that ends up being handled by a `Filter` (as is common with some non-Spring web frameworks, like Jersey and Wicket), then the `Filter` has to be explicitly registered as an `ERROR` dispatcher, as shown in the following example: -[source,java,indent=0,subs="verbatim,quotes,attributes"] +[source,java,pending-extract=true,indent=0,subs="verbatim,quotes,attributes"] ---- @Bean public FilterRegistrationBean myFilter() { @@ -3050,7 +3050,7 @@ As of version 4.2, Spring MVC {spring-framework-docs}/web.html#mvc-cors[supports Using {spring-framework-docs}/web.html#mvc-cors-controller[controller method CORS configuration] with {spring-framework-api}/web/bind/annotation/CrossOrigin.html[`@CrossOrigin`] annotations in your Spring Boot application does not require any specific configuration. {spring-framework-docs}/web.html#mvc-cors-global[Global CORS configuration] can be defined by registering a `WebMvcConfigurer` bean with a customized `addCorsMappings(CorsRegistry)` method, as shown in the following example: -[source,java,indent=0] +[source,java,pending-extract=true,indent=0] ---- @Configuration(proxyBeanMethods = false) public class MyConfiguration { @@ -3077,7 +3077,7 @@ Unlike Spring MVC, it does not require the Servlet API, is fully asynchronous an Spring WebFlux comes in two flavors: functional and annotation-based. The annotation-based one is quite close to the Spring MVC model, as shown in the following example: -[source,java,indent=0] +[source,java,pending-extract=true,indent=0] ---- @RestController @RequestMapping("/users") @@ -3103,7 +3103,7 @@ The annotation-based one is quite close to the Spring MVC model, as shown in the "`WebFlux.fn`", the functional variant, separates the routing configuration from the actual handling of the requests, as shown in the following example: -[source,java,indent=0] +[source,java,pending-extract=true,indent=0] ---- @Configuration(proxyBeanMethods = false) public class RoutingConfiguration { @@ -3173,7 +3173,7 @@ For example, `+spring.jackson.*+` configuration keys are applied to the Jackson If you need to add or customize codecs, you can create a custom `CodecCustomizer` component, as shown in the following example: -[source,java,indent=0] +[source,java,pending-extract=true,indent=0] ---- import org.springframework.boot.web.codec.CodecCustomizer; @@ -3258,7 +3258,7 @@ For that, you can add a bean of type `ErrorAttributes`. To change the error handling behavior, you can implement `ErrorWebExceptionHandler` and register a bean definition of that type. Because a `WebExceptionHandler` is quite low-level, Spring Boot also provides a convenient `AbstractErrorWebExceptionHandler` to let you handle errors in a WebFlux functional way, as shown in the following example: -[source,java,indent=0,subs="verbatim,quotes,attributes"] +[source,java,pending-extract=true,indent=0,subs="verbatim,quotes,attributes"] ---- public class CustomErrorWebExceptionHandler extends AbstractErrorWebExceptionHandler { @@ -3350,7 +3350,7 @@ Jersey has some native Spring support, so we also provide auto-configuration sup To get started with Jersey, include the `spring-boot-starter-jersey` as a dependency and then you need one `@Bean` of type `ResourceConfig` in which you register all the endpoints, as shown in the following example: -[source,java,indent=0,subs="verbatim,quotes,attributes"] +[source,java,pending-extract=true,indent=0,subs="verbatim,quotes,attributes"] ---- @Component public class JerseyConfig extends ResourceConfig { @@ -3370,7 +3370,7 @@ For more advanced customizations, you can also register an arbitrary number of b All the registered endpoints should be `@Components` with HTTP resource annotations (`@GET` and others), as shown in the following example: -[source,java,indent=0,subs="verbatim,quotes,attributes"] +[source,java,pending-extract=true,indent=0,subs="verbatim,quotes,attributes"] ---- @Component @Path("/hello") @@ -3493,7 +3493,7 @@ If you need to programmatically configure your embedded servlet container, you c `WebServerFactoryCustomizer` provides access to the `ConfigurableServletWebServerFactory`, which includes numerous customization setter methods. The following example shows programmatically setting the port: -[source,java,indent=0] +[source,java,pending-extract=true,indent=0] ---- import org.springframework.boot.web.server.WebServerFactoryCustomizer; import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory; @@ -3683,7 +3683,7 @@ This is done on purpose since this builder is stateful and you shouldn't create The following code shows a typical example: -[source,java,indent=0] +[source,java,pending-extract=true,indent=0] ---- @Service public class MyService { @@ -3767,7 +3767,7 @@ Spring Boot provides convenience methods that can be used to override access rul For example, you can customize your security configuration by adding something like: -[source,java,indent=0] +[source,java,pending-extract=true,indent=0] ---- include::{include-springbootfeatures}/security/CustomWebFluxSecurityConfiguration.java[tag=*] ---- @@ -3846,7 +3846,7 @@ By default, Spring Security's `OAuth2LoginAuthenticationFilter` only processes U If you want to customize the `redirect-uri` to use a different pattern, you need to provide configuration to process that custom pattern. For example, for servlet applications, you can add your own `SecurityFilterChain` that resembles the following: -[source,java,indent=0] +[source,java,pending-extract=true,indent=0] ---- @Bean public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception { @@ -4170,7 +4170,7 @@ For example, the following section in `application.properties` shows how you can === 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: -[source,java,indent=0] +[source,java,pending-extract=true,indent=0] ---- import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.JdbcTemplate; @@ -4230,7 +4230,7 @@ By default, all packages below your main configuration class (the one annotated Any classes annotated with `@Entity`, `@Embeddable`, or `@MappedSuperclass` are considered. A typical entity class resembles the following example: -[source,java,indent=0] +[source,java,pending-extract=true,indent=0] ---- package com.example.myapp.domain; @@ -4293,7 +4293,7 @@ If you use auto-configuration, repositories are searched from the package contai The following example shows a typical Spring Data repository interface definition: -[source,java,indent=0] +[source,java,pending-extract=true,indent=0] ---- package com.example.myapp.domain; @@ -4438,7 +4438,7 @@ The fluent API offered by jOOQ is initiated through the `org.jooq.DSLContext` in Spring Boot auto-configures a `DSLContext` as a Spring Bean and connects it to your application `DataSource`. To use the `DSLContext`, you can `@Autowire` it, as shown in the following example: -[source,java,indent=0] +[source,java,pending-extract=true,indent=0] ---- @Component public class JooqExample implements CommandLineRunner { @@ -4457,7 +4457,7 @@ TIP: The jOOQ manual tends to use a variable named `create` to hold the `DSLCont You can then use the `DSLContext` to construct your queries, as shown in the following example: -[source,java,indent=0] +[source,java,pending-extract=true,indent=0] ---- public List authorsBornAfter1980() { return this.create.selectFrom(AUTHOR) @@ -4512,7 +4512,7 @@ TIP: The "`How-to`" section includes a < cacheManagerCustomizer() { @@ -5844,7 +5844,7 @@ You can use the configprop:spring.jms.jndi-name[] property if you need to specif ==== Sending a Message Spring's `JmsTemplate` is auto-configured, and you can autowire it directly into your own beans, as shown in the following example: -[source,java,indent=0] +[source,java,pending-extract=true,indent=0] ---- import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jms.core.JmsTemplate; @@ -5885,7 +5885,7 @@ This also includes sending response messages that have been performed on the sam The following component creates a listener endpoint on the `someQueue` destination: -[source,java,indent=0] +[source,java,pending-extract=true,indent=0] ---- @Component public class MyBean { @@ -5904,7 +5904,7 @@ If you need to create more `JmsListenerContainerFactory` instances or if you wan For instance, the following example exposes another factory that uses a specific `MessageConverter`: -[source,java,indent=0] +[source,java,pending-extract=true,indent=0] ---- @Configuration(proxyBeanMethods = false) static class JmsConfiguration { @@ -5924,8 +5924,7 @@ For instance, the following example exposes another factory that uses a specific Then you can use the factory in any `@JmsListener`-annotated method as follows: -[source,java,indent=0] -[subs="verbatim,quotes"] +[source,java,pending-extract=true,indent=0,subs="verbatim,quotes"] ---- @Component public class MyBean { @@ -5989,7 +5988,7 @@ TIP: See https://spring.io/blog/2010/06/14/understanding-amqp-the-protocol-used- ==== Sending a Message Spring's `AmqpTemplate` and `AmqpAdmin` are auto-configured, and you can autowire them directly into your own beans, as shown in the following example: -[source,java,indent=0] +[source,java,pending-extract=true,indent=0] ---- import org.springframework.amqp.core.AmqpAdmin; import org.springframework.amqp.core.AmqpTemplate; @@ -6045,7 +6044,7 @@ If a `MessageConverter` or a `MessageRecoverer` bean is defined, it is automatic The following sample component creates a listener endpoint on the `someQueue` queue: -[source,java,indent=0] +[source,java,pending-extract=true,indent=0] ---- @Component public class MyBean { @@ -6067,7 +6066,7 @@ Those two beans are exposed by the auto-configuration. For instance, the following configuration class exposes another factory that uses a specific `MessageConverter`: -[source,java,indent=0] +[source,java,pending-extract=true,indent=0] ---- @Configuration(proxyBeanMethods = false) static class RabbitConfiguration { @@ -6087,7 +6086,7 @@ For instance, the following configuration class exposes another factory that use Then you can use the factory in any `@RabbitListener`-annotated method, as follows: -[source,java,indent=0] +[source,java,pending-extract=true,indent=0] [subs="verbatim,quotes"] ---- @Component @@ -6140,7 +6139,7 @@ See {spring-boot-autoconfigure-module-code}/kafka/KafkaProperties.java[`KafkaPro ==== Sending a Message Spring's `KafkaTemplate` is auto-configured, and you can autowire it directly in your own beans, as shown in the following example: -[source,java,indent=0] +[source,java,pending-extract=true,indent=0] ---- @Component public class MyBean { @@ -6169,7 +6168,7 @@ If no `KafkaListenerContainerFactory` has been defined, a default one is automat The following component creates a listener endpoint on the `someTopic` topic: -[source,java,indent=0] +[source,java,pending-extract=true,indent=0] ---- @Component public class MyBean { @@ -6291,7 +6290,7 @@ There are several ways to do that: * Provide a system property to map embedded broker addresses into configprop:spring.kafka.bootstrap-servers[] in the test class: -[source,java,indent=0] +[source,java,pending-extract=true,indent=0] ---- static { System.setProperty(EmbeddedKafkaBroker.BROKER_LIST_PROPERTY, "spring.kafka.bootstrap-servers"); @@ -6300,7 +6299,7 @@ There are several ways to do that: * Configure a property name on the `@EmbeddedKafka` annotation: -[source,java,indent=0] +[source,java,pending-extract=true,indent=0] ---- @EmbeddedKafka(topics = "someTopic", bootstrapServersProperty = "spring.kafka.bootstrap-servers") @@ -6326,7 +6325,7 @@ The auto-configured `RestTemplateBuilder` ensures that sensible `HttpMessageConv The following code shows a typical example: -[source,java,indent=0] +[source,java,pending-extract=true,indent=0] ---- @Service public class MyService { @@ -6391,7 +6390,7 @@ Spring Boot is configuring that builder to share HTTP resources, reflect codecs The following code shows a typical example: -[source,java,indent=0] +[source,java,pending-extract=true,indent=0] ---- @Service public class MyService { @@ -6452,7 +6451,7 @@ Target classes with such annotated methods need to be annotated with the `@Valid For instance, the following service triggers the validation of the first argument, making sure its size is between 8 and 10: -[source,java,indent=0] +[source,java,pending-extract=true,indent=0] ---- @Service @Validated @@ -6552,7 +6551,7 @@ For consistency, the `jmsConnectionFactory` bean is also provided by using the b The following example shows how to inject `ConnectionFactory` instances: -[source,java,indent=0,subs="verbatim,quotes,attributes"] +[source,java,pending-extract=true,indent=0,subs="verbatim,quotes,attributes"] ---- // Inject the primary (XA aware) ConnectionFactory @Autowired @@ -6670,7 +6669,7 @@ 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: -[source,java,indent=0] +[source,java,pending-extract=true,indent=0] ---- public class SampleJob extends QuartzJobBean { @@ -6954,7 +6953,7 @@ If you have only Spring WebFlux, we'll detect that and configure a WebFlux-based If both are present, Spring MVC takes precedence. If you want to test a reactive web application in this scenario, you must set the configprop:spring.main.web-application-type[] property: -[source,java,indent=0] +[source,java,pending-extract=true,indent=0] ---- @SpringBootTest(properties = "spring.main.web-application-type=reactive") class MyWebFluxTests { ... } @@ -6998,7 +6997,7 @@ As we < assertThat(number.floatValue()).isCloseTo(0.15f, within(0.01f))); + assertThat(json.write(message)) + .extractingJsonPathNumberValue("@.test.numberValue") + .satisfies((number) -> assertThat(number.floatValue()).isCloseTo(0.15f, within(0.01f))); ---- @@ -7301,7 +7300,7 @@ Mock MVC offers a powerful way to quickly test MVC controllers without needing t TIP: You can also auto-configure `MockMvc` in a non-`@WebMvcTest` (such as `@SpringBootTest`) by annotating it with `@AutoConfigureMockMvc`. The following example uses `MockMvc`: -[source,java,indent=0] +[source,java,pending-extract=true,indent=0] ---- import org.junit.jupiter.api.*; import org.springframework.beans.factory.annotation.*; @@ -7338,7 +7337,7 @@ TIP: If you need to configure elements of the auto-configuration (for example, w If you use HtmlUnit or Selenium, auto-configuration also provides an HtmlUnit `WebClient` bean and/or a Selenium `WebDriver` bean. The following example uses HtmlUnit: -[source,java,indent=0] +[source,java,pending-extract=true,indent=0] ---- import com.gargoylesoftware.htmlunit.*; import org.junit.jupiter.api.*; @@ -7401,7 +7400,7 @@ Often, `@WebFluxTest` is limited to a single controller and used in combination TIP: You can also auto-configure `WebTestClient` in a non-`@WebFluxTest` (such as `@SpringBootTest`) by annotating it with `@AutoConfigureWebTestClient`. The following example shows a class that uses both `@WebFluxTest` and a `WebTestClient`: -[source,java,indent=0] +[source,java,pending-extract=true,indent=0] ---- import org.junit.jupiter.api.Test; @@ -7456,7 +7455,7 @@ TIP: A list of the auto-configuration settings that are enabled by `@DataCassand The following example shows a typical setup for using Cassandra tests in Spring Boot: -[source,java,indent=0] +[source,java,pending-extract=true,indent=0] ---- import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.data.cassandra.DataCassandraTest; @@ -7487,7 +7486,7 @@ By default, data JPA tests are transactional and roll back at the end of each te See the {spring-framework-docs}/testing.html#testcontext-tx-enabling-transactions[relevant section] in the Spring Framework Reference Documentation for more details. If that is not what you want, you can disable transaction management for a test or for the whole class as follows: -[source,java,indent=0] +[source,java,pending-extract=true,indent=0] ---- import org.junit.jupiter.api.Test; import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; @@ -7506,7 +7505,7 @@ If you want to use `TestEntityManager` outside of `@DataJpaTest` instances, you A `JdbcTemplate` is also available if you need that. The following example shows the `@DataJpaTest` annotation in use: -[source,java,indent=0] +[source,java,pending-extract=true,indent=0] ---- import org.junit.jupiter.api.Test; import org.springframework.boot.test.autoconfigure.orm.jpa.*; @@ -7536,7 +7535,7 @@ The following example shows the `@DataJpaTest` annotation in use: In-memory embedded databases generally work well for tests, since they are fast and do not require any installation. If, however, you prefer to run tests against a real database you can use the `@AutoConfigureTestDatabase` annotation, as shown in the following example: -[source,java,indent=0] +[source,java,pending-extract=true,indent=0] ---- @DataJpaTest @AutoConfigureTestDatabase(replace=Replace.NONE) @@ -7562,7 +7561,7 @@ By default, JDBC tests are transactional and roll back at the end of each test. See the {spring-framework-docs}/testing.html#testcontext-tx-enabling-transactions[relevant section] in the Spring Framework Reference Documentation for more details. If that is not what you want, you can disable transaction management for a test or for the whole class, as follows: -[source,java,indent=0] +[source,java,pending-extract=true,indent=0] ---- import org.junit.jupiter.api.Test; import org.springframework.boot.test.autoconfigure.jdbc.JdbcTest; @@ -7613,7 +7612,7 @@ TIP: A list of the auto-configurations that are enabled by `@JooqTest` can be << `@JooqTest` configures a `DSLContext`. The following example shows the `@JooqTest` annotation in use: -[source,java,indent=0] +[source,java,pending-extract=true,indent=0] ---- import org.jooq.DSLContext; import org.junit.jupiter.api.Test; @@ -7644,7 +7643,7 @@ TIP: A list of the auto-configuration settings that are enabled by `@DataMongoTe The following class shows the `@DataMongoTest` annotation in use: -[source,java,indent=0] +[source,java,pending-extract=true,indent=0] ---- import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.data.mongo.DataMongoTest; @@ -7663,7 +7662,7 @@ The following class shows the `@DataMongoTest` annotation in use: In-memory embedded MongoDB generally works well for tests, since it is fast and does not require any developer installation. If, however, you prefer to run tests against a real MongoDB server, you should exclude the embedded MongoDB auto-configuration, as shown in the following example: -[source,java,indent=0] +[source,java,pending-extract=true,indent=0] ---- import org.springframework.boot.autoconfigure.mongo.embedded.EmbeddedMongoAutoConfiguration; import org.springframework.boot.test.autoconfigure.data.mongo.DataMongoTest; @@ -7688,7 +7687,7 @@ TIP: A list of the auto-configuration settings that are enabled by `@DataNeo4jTe The following example shows a typical setup for using Neo4J tests in Spring Boot: -[source,java,indent=0] +[source,java,pending-extract=true,indent=0] ---- import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.data.neo4j.DataNeo4jTest; @@ -7707,7 +7706,7 @@ By default, Data Neo4j tests are transactional and roll back at the end of each See the {spring-framework-docs}/testing.html#testcontext-tx-enabling-transactions[relevant section] in the Spring Framework Reference Documentation for more details. If that is not what you want, you can disable transaction management for a test or for the whole class, as follows: -[source,java,indent=0] +[source,java,pending-extract=true,indent=0] ---- import org.springframework.boot.test.autoconfigure.data.neo4j.DataNeo4jTest; import org.springframework.transaction.annotation.Propagation; @@ -7737,7 +7736,7 @@ TIP: A list of the auto-configuration settings that are enabled by `@DataRedisTe The following example shows the `@DataRedisTest` annotation in use: -[source,java,indent=0] +[source,java,pending-extract=true,indent=0] ---- import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.data.redis.DataRedisTest; @@ -7766,7 +7765,7 @@ TIP: A list of the auto-configuration settings that are enabled by `@DataLdapTes The following example shows the `@DataLdapTest` annotation in use: -[source,java,indent=0] +[source,java,pending-extract=true,indent=0] ---- import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.data.ldap.DataLdapTest; @@ -7785,7 +7784,7 @@ The following example shows the `@DataLdapTest` annotation in use: In-memory embedded LDAP generally works well for tests, since it is fast and does not require any developer installation. If, however, you prefer to run tests against a real LDAP server, you should exclude the embedded LDAP auto-configuration, as shown in the following example: -[source,java,indent=0] +[source,java,pending-extract=true,indent=0] ---- import org.springframework.boot.autoconfigure.ldap.embedded.EmbeddedLdapAutoConfiguration; import org.springframework.boot.test.autoconfigure.data.ldap.DataLdapTest; @@ -7809,7 +7808,7 @@ TIP: A list of the auto-configuration settings that are enabled by `@RestClientT The specific beans that you want to test should be specified by using the `value` or `components` attribute of `@RestClientTest`, as shown in the following example: -[source,java,indent=0] +[source,java,pending-extract=true,indent=0] ---- @RestClientTest(RemoteVehicleDetailsService.class) class ExampleRestClientTest { @@ -7849,7 +7848,7 @@ It can also be used to configure the host, scheme, and port that appears in any `@AutoConfigureRestDocs` customizes the `MockMvc` bean to use Spring REST Docs when testing Servlet-based web applications. You can inject it by using `@Autowired` and use it in your tests as you normally would when using Mock MVC and Spring REST Docs, as shown in the following example: -[source,java,indent=0] +[source,java,pending-extract=true,indent=0] ---- import org.junit.jupiter.api.Test; @@ -7881,7 +7880,7 @@ You can inject it by using `@Autowired` and use it in your tests as you normally If you require more control over Spring REST Docs configuration than offered by the attributes of `@AutoConfigureRestDocs`, you can use a `RestDocsMockMvcConfigurationCustomizer` bean, as shown in the following example: -[source,java,indent=0] +[source,java,pending-extract=true,indent=0] ---- @TestConfiguration static class CustomizationConfiguration @@ -7899,7 +7898,7 @@ If you want to make use of Spring REST Docs support for a parameterized output d The auto-configuration calls `alwaysDo` with this result handler, thereby causing each `MockMvc` call to automatically generate the default snippets. The following example shows a `RestDocumentationResultHandler` being defined: -[source,java,indent=0] +[source,java,pending-extract=true,indent=0] ---- @TestConfiguration(proxyBeanMethods = false) static class ResultHandlerConfiguration { @@ -7963,7 +7962,7 @@ TIP: A list of the auto-configuration settings that are enabled by `@WebServiceC The following example shows the `@WebServiceClientTest` annotation in use: -[source,java,indent=0] +[source,java,pending-extract=true,indent=0] ---- @WebServiceClientTest(ExampleWebServiceClient.class) class WebServiceClientIntegrationTests { @@ -7991,7 +7990,7 @@ The following example shows the `@WebServiceClientTest` annotation in use: Each slice provides one or more `@AutoConfigure...` annotations that namely defines the auto-configurations that should be included as part of a slice. Additional auto-configurations can be added on a test-by-test basis by creating a custom `@AutoConfigure...` annotation or by adding `@ImportAutoConfiguration` to the test as shown in the following example: -[source,java,indent=0] +[source,java,pending-extract=true,indent=0] ---- @JdbcTest @ImportAutoConfiguration(IntegrationAutoConfiguration.class) @@ -8022,7 +8021,7 @@ It then becomes important not to litter the application's main class with config Assume that you are using Spring Batch and you rely on the auto-configuration for it. You could define your `@SpringBootApplication` as follows: -[source,java,indent=0] +[source,java,pending-extract=true,indent=0] ---- @SpringBootApplication @EnableBatchProcessing @@ -8032,7 +8031,7 @@ You could define your `@SpringBootApplication` as follows: Because this class is the source configuration for the test, any slice test actually tries to start Spring Batch, which is definitely not what you want to do. A recommended approach is to move that area-specific configuration to a separate `@Configuration` class at the same level as your application, as shown in the following example: -[source,java,indent=0] +[source,java,pending-extract=true,indent=0] ---- @Configuration(proxyBeanMethods = false) @EnableBatchProcessing @@ -8045,7 +8044,7 @@ The latter approach lets you enable it in one of your tests, if necessary, with Test slices exclude `@Configuration` classes from scanning. For example, for a `@WebMvcTest`, the following configuration will not include the given `WebMvcConfigurer` bean in the application context loaded by the test slice: -[source,java,indent=0] +[source,java,pending-extract=true,indent=0] ---- @Configuration public class WebConfiguration { @@ -8060,7 +8059,7 @@ For example, for a `@WebMvcTest`, the following configuration will not include t The configuration below will, however, cause the custom `WebMvcConfigurer` to be loaded by the test slice. -[source,java,indent=0] +[source,java,pending-extract=true,indent=0] ---- @Component public class TestWebMvcConfigurer implements WebMvcConfigurer { @@ -8072,7 +8071,7 @@ Another source of confusion is classpath scanning. Assume that, while you structured your code in a sensible way, you need to scan an additional package. Your application may resemble the following code: -[source,java,indent=0] +[source,java,pending-extract=true,indent=0] ---- @SpringBootApplication @ComponentScan({ "com.example.app", "org.acme.another" }) @@ -8110,7 +8109,7 @@ A few test utility classes that are generally useful when testing your applicati `ConfigFileApplicationContextInitializer` is an `ApplicationContextInitializer` that you can apply to your tests to load Spring Boot `application.properties` files. You can use it when you do not need the full set of features provided by `@SpringBootTest`, as shown in the following example: -[source,java,indent=0] +[source,java,pending-extract=true,indent=0] ---- @ContextConfiguration(classes = Config.class, initializers = ConfigFileApplicationContextInitializer.class) @@ -8127,7 +8126,7 @@ For `@Value` support, you need to either additionally configure a `PropertySourc `TestPropertyValues` lets you quickly add properties to a `ConfigurableEnvironment` or `ConfigurableApplicationContext`. You can call it with `key=value` strings, as follows: -[source,java,indent=0] +[source,java,pending-extract=true,indent=0] ---- TestPropertyValues.of("org=Spring", "name=Boot").applyTo(env); ---- @@ -8164,7 +8163,7 @@ If you do use Apache's HTTP client, some additional test-friendly features are e `TestRestTemplate` can be instantiated directly in your integration tests, as shown in the following example: -[source,java,indent=0] +[source,java,pending-extract=true,indent=0] ---- public class MyTest { @@ -8237,7 +8236,7 @@ It does, however, auto-configure a `WebServiceTemplateBuilder`, which can be use The following code shows a typical example: -[source,java,indent=0] +[source,java,pending-extract=true,indent=0] ---- @Service public class MyService { @@ -8258,7 +8257,7 @@ The following code shows a typical example: By default, `WebServiceTemplateBuilder` detects a suitable HTTP-based `WebServiceMessageSender` using the available HTTP client libraries on the classpath. You can also customize read and connection timeouts as follows: -[source,java,indent=0] +[source,java,pending-extract=true,indent=0] ---- @Bean public WebServiceTemplate webServiceTemplate(WebServiceTemplateBuilder builder) { @@ -8347,7 +8346,7 @@ 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: -[source,java,indent=0] +[source,java,pending-extract=true,indent=0] ---- @Configuration(proxyBeanMethods = false) // Some conditions @@ -8380,7 +8379,7 @@ The `search` attribute lets you limit the `ApplicationContext` hierarchy that sh When placed on a `@Bean` method, the target type defaults to the return type of the method, as shown in the following example: -[source,java,indent=0] +[source,java,pending-extract=true,indent=0] ---- @Configuration(proxyBeanMethods = false) public class MyAutoConfiguration { @@ -8475,7 +8474,7 @@ 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. -[source,java,indent=0] +[source,java,pending-extract=true,indent=0] ---- @Test void autoConfigTest() { @@ -8549,7 +8548,7 @@ As a rule of thumb, prefix all your keys with a namespace that you own (e.g. `ac Make sure that configuration keys are documented by adding field javadoc for each property, as shown in the following example: -[source,java,indent=0] +[source,java,pending-extract=true,indent=0] ---- @ConfigurationProperties("acme") public class AcmeProperties { diff --git a/spring-boot-project/spring-boot-docs/src/docs/asciidoc/using-spring-boot.adoc b/spring-boot-project/spring-boot-docs/src/docs/asciidoc/using-spring-boot.adoc index e66c218ae49..3e0d42f654b 100644 --- a/spring-boot-project/spring-boot-docs/src/docs/asciidoc/using-spring-boot.adoc +++ b/spring-boot-project/spring-boot-docs/src/docs/asciidoc/using-spring-boot.adoc @@ -209,7 +209,7 @@ The following listing shows a typical layout: The `Application.java` file would declare the `main` method, along with the basic `@SpringBootApplication`, as follows: -[source,java,indent=0] +[source,java,pending-extract=true,indent=0] ---- package com.example.myapplication; @@ -282,7 +282,7 @@ Doing so enables debug logs for a selection of core loggers and logs a condition === Disabling Specific Auto-configuration Classes If you find that specific auto-configuration classes that you do not want are being applied, you can use the exclude attribute of `@SpringBootApplication` to disable them, as shown in the following example: -[source,java,indent=0] +[source,java,pending-extract=true,indent=0] ---- import org.springframework.boot.autoconfigure.*; import org.springframework.boot.autoconfigure.jdbc.*; @@ -313,7 +313,7 @@ All of your application components (`@Component`, `@Service`, `@Repository`, `@C The following example shows a `@Service` Bean that uses constructor injection to obtain a required `RiskAssessor` bean: -[source,java,indent=0] +[source,java,pending-extract=true,indent=0] ---- package com.example.service; @@ -337,7 +337,7 @@ The following example shows a `@Service` Bean that uses constructor injection to If a bean has one constructor, you can omit the `@Autowired`, as shown in the following example: -[source,java,indent=0] +[source,java,pending-extract=true,indent=0] ---- @Service public class DatabaseAccountService implements AccountService { @@ -366,7 +366,7 @@ A single `@SpringBootApplication` annotation can be used to enable those three f * `@ComponentScan`: enable `@Component` scan on the package where the application is located (see <>) * `@Configuration`: allow to register extra beans in the context or import additional configuration classes -[source,java,indent=0] +[source,java,pending-extract=true,indent=0] ---- package com.example.myapplication; @@ -390,7 +390,7 @@ NOTE: `@SpringBootApplication` also provides aliases to customize the attributes None of these features are mandatory and you may choose to replace this single annotation by any of the features that it enables. For instance, you may not want to use component scan or configuration properties scan in your application: -[source,java,indent=0] +[source,java,pending-extract=true,indent=0] ---- package com.example.myapplication; @@ -593,7 +593,7 @@ The way in which you cause the classpath to be updated depends on the IDE that y * In Eclipse, saving a modified file causes the classpath to be updated and triggers a restart. * In IntelliJ IDEA, building the project (`Build +->+ Build Project`) has the same effect. -* If using a build plugin, running `mvn compile` for Maven or `gradle build` for Gradle will trigger a restart. +* If using a build plugin, running `mvn compile` for Maven or `gradle build` for Gradle will trigger a restart. **** NOTE: If you are restarting with Maven or Gradle using the build plugin you must leave the `forking` set to `enabled`. @@ -677,7 +677,7 @@ In most cases, you can set this property in your `application.properties` (doing If you need to _completely_ disable restart support (for example, because it does not work with a specific library), you need to set the configprop:spring.devtools.restart.enabled[] `System` property to `false` before calling `SpringApplication.run(...)`, as shown in the following example: -[source,java,indent=0] +[source,java,pending-extract=true,indent=0] ---- public static void main(String[] args) { System.setProperty("spring.devtools.restart.enabled", "false");