From 0e326d6b0f35b3ab311d7e4eb14c44049f088897 Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Wed, 3 Feb 2021 17:53:29 -0800 Subject: [PATCH] Extract some code samples from docs See gh-6313 --- .../docs/asciidoc/spring-boot-features.adoc | 248 ++---------------- .../constructorbinding/AcmeProperties.java | 84 ++++++ .../nonnull/AcmeProperties.java | 84 ++++++ .../javabeanbinding/AcmeProperties.java | 91 +++++++ .../valueinjection/MyBean.java | 32 +++ .../ApplicationArgumentsExample.java | 38 +++ .../CommandLineRunnerExample.java | 32 +++ .../availability/LocalCacheVerifier.java | 48 ++++ .../availability/ReadinessStateExporter.java | 41 +++ .../main/custom/MyApplication.java | 34 +++ .../main/run/MyApplication.java | 31 +++ .../startup/MyApplication.java | 34 +++ 12 files changed, 571 insertions(+), 226 deletions(-) create mode 100644 spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/externalizedconfiguration/constructorbinding/AcmeProperties.java create mode 100644 spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/externalizedconfiguration/constructorbinding/nonnull/AcmeProperties.java create mode 100644 spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/externalizedconfiguration/javabeanbinding/AcmeProperties.java create mode 100644 spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/externalizedconfiguration/valueinjection/MyBean.java create mode 100644 spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/springapplication/ApplicationArgumentsExample.java create mode 100644 spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/springapplication/CommandLineRunnerExample.java create mode 100644 spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/springapplication/availability/LocalCacheVerifier.java create mode 100644 spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/springapplication/availability/ReadinessStateExporter.java create mode 100644 spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/springapplication/main/custom/MyApplication.java create mode 100644 spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/springapplication/main/run/MyApplication.java create mode 100644 spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/springapplication/startup/MyApplication.java 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 5343e4a922..6123756b13 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,11 +13,9 @@ If you have not already done so, you might want to read the "< event) { - switch (event.getState()) { - case ACCEPTING_TRAFFIC: - // create file /tmp/healthy - break; - case REFUSING_TRAFFIC: - // remove file /tmp/healthy - break; - } - } - - } +include::{include-springbootfeatures}/springapplication/availability/ReadinessStateExporter.java[tag=*] ---- We can also update the state of the application, when the application breaks and cannot recover: -[source,java,pending-extract=true,indent=0] +[source,java,indent=0] ---- - @Component - public class LocalCacheVerifier { - - private final ApplicationEventPublisher eventPublisher; - - public LocalCacheVerifier(ApplicationEventPublisher eventPublisher) { - this.eventPublisher = eventPublisher; - } - - public void checkLocalCache() { - try { - //... - } - catch (CacheCompletelyBrokenException ex) { - AvailabilityChangeEvent.publish(this.eventPublisher, ex, LivenessState.BROKEN); - } - } - - } +include::{include-springbootfeatures}/springapplication/availability/LocalCacheVerifier.java[tag=*] ---- Spring Boot provides <>. @@ -380,23 +341,9 @@ TIP: It is often desirable to call `setWebApplicationType(WebApplicationType.NON 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: -[source,java,pending-extract=true,indent=0] +[source,java,indent=0] ---- - import org.springframework.boot.*; - import org.springframework.beans.factory.annotation.*; - import org.springframework.stereotype.*; - - @Component - public class MyBean { - - @Autowired - public MyBean(ApplicationArguments args) { - boolean debug = args.containsOption("debug"); - List files = args.getNonOptionArgs(); - // if run with "--debug logfile.txt" debug=true, files=["logfile.txt"] - } - - } +include::{include-springbootfeatures}/springapplication/ApplicationArgumentsExample.java[tag=*] ---- TIP: Spring Boot also registers a `CommandLinePropertySource` with the Spring `Environment`. @@ -415,19 +362,9 @@ 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: -[source,java,pending-extract=true,indent=0] +[source,java,indent=0] ---- - import org.springframework.boot.*; - import org.springframework.stereotype.*; - - @Component - public class MyBean implements CommandLineRunner { - - public void run(String... args) { - // Do something... - } - - } +include::{include-springbootfeatures}/springapplication/CommandLineRunnerExample.java[tag=*] ---- 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. @@ -473,13 +410,9 @@ This data can be collected for profiling purposes, or just to have a better unde You can choose an `ApplicationStartup` implementation when setting up the `SpringApplication` instance. For example, to use the `BufferingApplicationStartup`, you could write: -[source,java,pending-extract=true,indent=0] +[source,java,indent=0] ---- - public static void main(String[] args) { - SpringApplication app = new SpringApplication(MySpringConfiguration.class); - app.setApplicationStartup(new BufferingApplicationStartup(2048)); - app.run(args); - } +include::{include-springbootfeatures}/springapplication/startup/MyApplication.java[tag=*] ---- The first available implementation, `FlightRecorderApplicationStartup` is provided by Spring Framework. @@ -532,20 +465,9 @@ Config data files are considered in the following order: To provide a concrete example, suppose you develop a `@Component` that uses a `name` property, as shown in the following example: -[source,java,pending-extract=true,indent=0] +[source,java,indent=0] ---- - import org.springframework.stereotype.*; - import org.springframework.beans.factory.annotation.*; - - @Component - public class MyBean { - - @Value("${name}") - private String name; - - // ... - - } +include::{include-springbootfeatures}/externalizedconfiguration/valueinjection/MyBean.java[tag=*] ---- On your application classpath (for example, inside your jar) you can have an `application.properties` file that provides a sensible default property value for `name`. @@ -1115,58 +1037,9 @@ TIP: See also the < roles = new ArrayList<>(Collections.singleton("USER")); - - public String getUsername() { ... } - - public void setUsername(String username) { ... } - - public String getPassword() { ... } - - public void setPassword(String password) { ... } - - public List getRoles() { ... } - - public void setRoles(List roles) { ... } - - } - } +include::{include-springbootfeatures}/externalizedconfiguration/javabeanbinding/AcmeProperties.java[tag=*] ---- The preceding POJO defines the following properties: @@ -1205,63 +1078,9 @@ Finally, only standard Java Bean properties are considered and binding on static ==== Constructor binding The example in the previous section can be rewritten in an immutable fashion as shown in the following example: -[source,java,pending-extract=true,indent=0] +[source,java,indent=0] ---- - package com.example; - - import java.net.InetAddress; - import java.util.List; - - import org.springframework.boot.context.properties.ConfigurationProperties; - import org.springframework.boot.context.properties.ConstructorBinding; - import org.springframework.boot.context.properties.bind.DefaultValue; - - @ConstructorBinding - @ConfigurationProperties("acme") - public class AcmeProperties { - - private final boolean enabled; - - private final InetAddress remoteAddress; - - private final Security security; - - public AcmeProperties(boolean enabled, InetAddress remoteAddress, Security security) { - this.enabled = enabled; - this.remoteAddress = remoteAddress; - this.security = security; - } - - public boolean isEnabled() { ... } - - public InetAddress getRemoteAddress() { ... } - - public Security getSecurity() { ... } - - public static class Security { - - private final String username; - - private final String password; - - private final List roles; - - public Security(String username, String password, - @DefaultValue("USER") List roles) { - this.username = username; - this.password = password; - this.roles = roles; - } - - public String getUsername() { ... } - - public String getPassword() { ... } - - public List getRoles() { ... } - - } - - } +include::{include-springbootfeatures}/externalizedconfiguration/constructorbinding/AcmeProperties.java[tag=*] ---- In this setup, the `@ConstructorBinding` annotation is used to indicate that constructor binding should be used. @@ -1273,32 +1092,9 @@ Default values can be specified using `@DefaultValue` and the same conversion se By default, if no properties are bound to `Security`, the `AcmeProperties` instance will contain a `null` value for `security`. If you wish you return a non-null instance of `Security` even when no properties are bound to it, you can use an empty `@DefaultValue` annotation to do so: -[source,java,pending-extract=true,indent=0] +[source,java,indent=0] ---- - package com.example; - import java.net.InetAddress; - import java.util.List; - - import org.springframework.boot.context.properties.ConfigurationProperties; - import org.springframework.boot.context.properties.ConstructorBinding; - import org.springframework.boot.context.properties.bind.DefaultValue; - - @ConstructorBinding - @ConfigurationProperties("acme") - public class AcmeProperties { - - private final boolean enabled; - - private final InetAddress remoteAddress; - - private final Security security; - - public AcmeProperties(boolean enabled, InetAddress remoteAddress, @DefaultValue Security security) { - this.enabled = enabled; - this.remoteAddress = remoteAddress; - this.security = security; - } - } +include::{include-springbootfeatures}/externalizedconfiguration/constructorbinding/nonnull/AcmeProperties.java[tag=*] ---- diff --git a/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/externalizedconfiguration/constructorbinding/AcmeProperties.java b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/externalizedconfiguration/constructorbinding/AcmeProperties.java new file mode 100644 index 0000000000..f5794c3653 --- /dev/null +++ b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/externalizedconfiguration/constructorbinding/AcmeProperties.java @@ -0,0 +1,84 @@ +/* + * Copyright 2012-2021 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.docs.springbootfeatures.externalizedconfiguration.constructorbinding; + +//tag::code[] +import java.net.InetAddress; +import java.util.List; + +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.boot.context.properties.ConstructorBinding; +import org.springframework.boot.context.properties.bind.DefaultValue; + +@ConstructorBinding +@ConfigurationProperties("acme") +public class AcmeProperties { + + private final boolean enabled; + + private final InetAddress remoteAddress; + + private final Security security; + + public AcmeProperties(boolean enabled, InetAddress remoteAddress, Security security) { + this.enabled = enabled; + this.remoteAddress = remoteAddress; + this.security = security; + } + + public boolean isEnabled() { + return this.enabled; + } + + public InetAddress getRemoteAddress() { + return this.remoteAddress; + } + + public Security getSecurity() { + return this.security; + } + + public static class Security { + + private final String username; + + private final String password; + + private final List roles; + + public Security(String username, String password, @DefaultValue("USER") List roles) { + this.username = username; + this.password = password; + this.roles = roles; + } + + public String getUsername() { + return this.username; + } + + public String getPassword() { + return this.password; + } + + public List getRoles() { + return this.roles; + } + + } + +} +// end::code[] diff --git a/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/externalizedconfiguration/constructorbinding/nonnull/AcmeProperties.java b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/externalizedconfiguration/constructorbinding/nonnull/AcmeProperties.java new file mode 100644 index 0000000000..19aaf5ff87 --- /dev/null +++ b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/externalizedconfiguration/constructorbinding/nonnull/AcmeProperties.java @@ -0,0 +1,84 @@ +/* + * Copyright 2012-2021 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.docs.springbootfeatures.externalizedconfiguration.constructorbinding.nonnull; + +import java.net.InetAddress; +import java.util.List; + +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.boot.context.properties.ConstructorBinding; +import org.springframework.boot.context.properties.bind.DefaultValue; + +@ConstructorBinding +@ConfigurationProperties("acme") +public class AcmeProperties { + + private final boolean enabled; + + private final InetAddress remoteAddress; + + private final Security security; + + // tag::code[] + public AcmeProperties(boolean enabled, InetAddress remoteAddress, @DefaultValue Security security) { + this.enabled = enabled; + this.remoteAddress = remoteAddress; + this.security = security; + } + // end::code[] + + public boolean isEnabled() { + return this.enabled; + } + + public InetAddress getRemoteAddress() { + return this.remoteAddress; + } + + public Security getSecurity() { + return this.security; + } + + public static class Security { + + private final String username; + + private final String password; + + private final List roles; + + public Security(String username, String password, @DefaultValue("USER") List roles) { + this.username = username; + this.password = password; + this.roles = roles; + } + + public String getUsername() { + return this.username; + } + + public String getPassword() { + return this.password; + } + + public List getRoles() { + return this.roles; + } + + } + +} diff --git a/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/externalizedconfiguration/javabeanbinding/AcmeProperties.java b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/externalizedconfiguration/javabeanbinding/AcmeProperties.java new file mode 100644 index 0000000000..a994f5de38 --- /dev/null +++ b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/externalizedconfiguration/javabeanbinding/AcmeProperties.java @@ -0,0 +1,91 @@ +/* + * Copyright 2012-2021 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.docs.springbootfeatures.externalizedconfiguration.javabeanbinding; + +//tag::code[] +import java.net.InetAddress; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +import org.springframework.boot.context.properties.ConfigurationProperties; + +@ConfigurationProperties("acme") +public class AcmeProperties { + + private boolean enabled; + + private InetAddress remoteAddress; + + private final Security security = new Security(); + + public boolean isEnabled() { + return this.enabled; + } + + public void setEnabled(boolean enabled) { + this.enabled = enabled; + } + + public InetAddress getRemoteAddress() { + return this.remoteAddress; + } + + public void setRemoteAddress(InetAddress remoteAddress) { + this.remoteAddress = remoteAddress; + } + + public Security getSecurity() { + return this.security; + } + + public static class Security { + + private String username; + + private String password; + + private List roles = new ArrayList<>(Collections.singleton("USER")); + + public String getUsername() { + return this.username; + } + + public void setUsername(String username) { + this.username = username; + } + + public String getPassword() { + return this.password; + } + + public void setPassword(String password) { + this.password = password; + } + + public List getRoles() { + return this.roles; + } + + public void setRoles(List roles) { + this.roles = roles; + } + + } + +} +// end::code[] diff --git a/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/externalizedconfiguration/valueinjection/MyBean.java b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/externalizedconfiguration/valueinjection/MyBean.java new file mode 100644 index 0000000000..f2a388ca71 --- /dev/null +++ b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/externalizedconfiguration/valueinjection/MyBean.java @@ -0,0 +1,32 @@ +/* + * Copyright 2012-2021 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.docs.springbootfeatures.externalizedconfiguration.valueinjection; + +// tag::code[] +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Component; + +@Component +public class MyBean { + + @Value("${name}") + private String name; + + // ... + +} +// end::code[] diff --git a/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/springapplication/ApplicationArgumentsExample.java b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/springapplication/ApplicationArgumentsExample.java new file mode 100644 index 0000000000..55d895f826 --- /dev/null +++ b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/springapplication/ApplicationArgumentsExample.java @@ -0,0 +1,38 @@ +/* + * Copyright 2012-2021 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.docs.springbootfeatures.springapplication; + +//tag::code[] +import java.util.List; + +import org.springframework.boot.ApplicationArguments; +import org.springframework.stereotype.Component; + +@Component +public class ApplicationArgumentsExample { + + public ApplicationArgumentsExample(ApplicationArguments args) { + boolean debug = args.containsOption("debug"); + List files = args.getNonOptionArgs(); + if (debug) { + System.out.println(files); + } + // if run with "--debug logfile.txt" prints ["logfile.txt"] + } + +} +// end::code[] diff --git a/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/springapplication/CommandLineRunnerExample.java b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/springapplication/CommandLineRunnerExample.java new file mode 100644 index 0000000000..e082045d67 --- /dev/null +++ b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/springapplication/CommandLineRunnerExample.java @@ -0,0 +1,32 @@ +/* + * Copyright 2012-2021 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.docs.springbootfeatures.springapplication; + +//tag::code[] +import org.springframework.boot.CommandLineRunner; +import org.springframework.stereotype.Component; + +@Component +public class CommandLineRunnerExample implements CommandLineRunner { + + @Override + public void run(String... args) { + // Do something... + } + +} +// end::code[] diff --git a/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/springapplication/availability/LocalCacheVerifier.java b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/springapplication/availability/LocalCacheVerifier.java new file mode 100644 index 0000000000..1fd7fbb406 --- /dev/null +++ b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/springapplication/availability/LocalCacheVerifier.java @@ -0,0 +1,48 @@ +/* + * Copyright 2012-2021 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.docs.springbootfeatures.springapplication.availability; + +//tag::code[] +import org.springframework.boot.availability.AvailabilityChangeEvent; +import org.springframework.boot.availability.LivenessState; +import org.springframework.context.ApplicationEventPublisher; +import org.springframework.stereotype.Component; + +@Component +public class LocalCacheVerifier { + + private final ApplicationEventPublisher eventPublisher; + + public LocalCacheVerifier(ApplicationEventPublisher eventPublisher) { + this.eventPublisher = eventPublisher; + } + + public void checkLocalCache() { + try { + // ... + } + catch (CacheCompletelyBrokenException ex) { + AvailabilityChangeEvent.publish(this.eventPublisher, ex, LivenessState.BROKEN); + } + } + +} +// end::code[] + +class CacheCompletelyBrokenException extends RuntimeException { + +} diff --git a/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/springapplication/availability/ReadinessStateExporter.java b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/springapplication/availability/ReadinessStateExporter.java new file mode 100644 index 0000000000..4b82ecf8d3 --- /dev/null +++ b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/springapplication/availability/ReadinessStateExporter.java @@ -0,0 +1,41 @@ +/* + * Copyright 2012-2021 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.docs.springbootfeatures.springapplication.availability; + +// tag::code[] +import org.springframework.boot.availability.AvailabilityChangeEvent; +import org.springframework.boot.availability.ReadinessState; +import org.springframework.context.event.EventListener; +import org.springframework.stereotype.Component; + +@Component +public class ReadinessStateExporter { + + @EventListener + public void onStateChange(AvailabilityChangeEvent event) { + switch (event.getState()) { + case ACCEPTING_TRAFFIC: + // create file /tmp/healthy + break; + case REFUSING_TRAFFIC: + // remove file /tmp/healthy + break; + } + } + +} +// end::code[] diff --git a/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/springapplication/main/custom/MyApplication.java b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/springapplication/main/custom/MyApplication.java new file mode 100644 index 0000000000..40f8f32e3f --- /dev/null +++ b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/springapplication/main/custom/MyApplication.java @@ -0,0 +1,34 @@ +/* + * Copyright 2012-2021 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.docs.springbootfeatures.springapplication.main.custom; + +import org.springframework.boot.Banner; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class MyApplication { + + // tag::code[] + public static void main(String[] args) { + SpringApplication application = new SpringApplication(MyApplication.class); + application.setBannerMode(Banner.Mode.OFF); + application.run(args); + } + // end::code[] + +} diff --git a/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/springapplication/main/run/MyApplication.java b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/springapplication/main/run/MyApplication.java new file mode 100644 index 0000000000..07d9b0ceff --- /dev/null +++ b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/springapplication/main/run/MyApplication.java @@ -0,0 +1,31 @@ +/* + * Copyright 2012-2021 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.docs.springbootfeatures.springapplication.main.run; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class MyApplication { + + // tag::code[] + public static void main(String[] args) { + SpringApplication.run(MyApplication.class, args); + } + // end::code[] + +} diff --git a/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/springapplication/startup/MyApplication.java b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/springapplication/startup/MyApplication.java new file mode 100644 index 0000000000..5df88e11ad --- /dev/null +++ b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/springapplication/startup/MyApplication.java @@ -0,0 +1,34 @@ +/* + * Copyright 2012-2021 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.docs.springbootfeatures.springapplication.startup; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.context.metrics.buffering.BufferingApplicationStartup; + +@SpringBootApplication +public class MyApplication { + + // tag::code[] + public static void main(String[] args) { + SpringApplication application = new SpringApplication(MyApplication.class); + application.setApplicationStartup(new BufferingApplicationStartup(2048)); + application.run(args); + } + // end::code[] + +}