Skip to content

Commit 0e326d6

Browse files
committed
Extract some code samples from docs
See gh-6313
1 parent e0392c4 commit 0e326d6

File tree

12 files changed

+571
-226
lines changed

12 files changed

+571
-226
lines changed

spring-boot-project/spring-boot-docs/src/docs/asciidoc/spring-boot-features.adoc

Lines changed: 22 additions & 226 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,9 @@ If you have not already done so, you might want to read the "<<getting-started.a
1313
The `SpringApplication` class provides a convenient way to bootstrap a Spring application that is started from a `main()` method.
1414
In many situations, you can delegate to the static `SpringApplication.run` method, as shown in the following example:
1515

16-
[source,java,pending-extract=true,indent=0]
16+
[source,java,indent=0]
1717
----
18-
public static void main(String[] args) {
19-
SpringApplication.run(MySpringConfiguration.class, args);
20-
}
18+
include::{include-springbootfeatures}/springapplication/main/run/MyApplication.java[tag=*]
2119
----
2220

2321
When your application starts, you should see something similar to the following output:
@@ -176,13 +174,9 @@ This will initialize the `application.*` banner variables before building the cl
176174
If the `SpringApplication` defaults are not to your taste, you can instead create a local instance and customize it.
177175
For example, to turn off the banner, you could write:
178176

179-
[source,java,pending-extract=true,indent=0]
177+
[source,java,indent=0]
180178
----
181-
public static void main(String[] args) {
182-
SpringApplication app = new SpringApplication(MySpringConfiguration.class);
183-
app.setBannerMode(Banner.Mode.OFF);
184-
app.run(args);
185-
}
179+
include::{include-springbootfeatures}/springapplication/main/custom/MyApplication.java[tag=*]
186180
----
187181

188182
NOTE: The constructor arguments passed to `SpringApplication` are configuration sources for Spring beans.
@@ -255,49 +249,16 @@ More often, applications will want to listen to state updates or update the stat
255249

256250
For example, we can export the "Readiness" state of the application to a file so that a Kubernetes "exec Probe" can look at this file:
257251

258-
[source,java,pending-extract=true,indent=0]
252+
[source,java,indent=0]
259253
----
260-
@Component
261-
public class ReadinessStateExporter {
262-
263-
@EventListener
264-
public void onStateChange(AvailabilityChangeEvent<ReadinessState> event) {
265-
switch (event.getState()) {
266-
case ACCEPTING_TRAFFIC:
267-
// create file /tmp/healthy
268-
break;
269-
case REFUSING_TRAFFIC:
270-
// remove file /tmp/healthy
271-
break;
272-
}
273-
}
274-
275-
}
254+
include::{include-springbootfeatures}/springapplication/availability/ReadinessStateExporter.java[tag=*]
276255
----
277256

278257
We can also update the state of the application, when the application breaks and cannot recover:
279258

280-
[source,java,pending-extract=true,indent=0]
259+
[source,java,indent=0]
281260
----
282-
@Component
283-
public class LocalCacheVerifier {
284-
285-
private final ApplicationEventPublisher eventPublisher;
286-
287-
public LocalCacheVerifier(ApplicationEventPublisher eventPublisher) {
288-
this.eventPublisher = eventPublisher;
289-
}
290-
291-
public void checkLocalCache() {
292-
try {
293-
//...
294-
}
295-
catch (CacheCompletelyBrokenException ex) {
296-
AvailabilityChangeEvent.publish(this.eventPublisher, ex, LivenessState.BROKEN);
297-
}
298-
}
299-
300-
}
261+
include::{include-springbootfeatures}/springapplication/availability/LocalCacheVerifier.java[tag=*]
301262
----
302263

303264
Spring Boot provides <<production-ready-features.adoc#production-ready-kubernetes-probes,Kubernetes HTTP probes for "Liveness" and "Readiness" with Actuator Health Endpoints>>.
@@ -380,23 +341,9 @@ TIP: It is often desirable to call `setWebApplicationType(WebApplicationType.NON
380341
If you need to access the application arguments that were passed to `SpringApplication.run(...)`, you can inject a `org.springframework.boot.ApplicationArguments` bean.
381342
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:
382343

383-
[source,java,pending-extract=true,indent=0]
344+
[source,java,indent=0]
384345
----
385-
import org.springframework.boot.*;
386-
import org.springframework.beans.factory.annotation.*;
387-
import org.springframework.stereotype.*;
388-
389-
@Component
390-
public class MyBean {
391-
392-
@Autowired
393-
public MyBean(ApplicationArguments args) {
394-
boolean debug = args.containsOption("debug");
395-
List<String> files = args.getNonOptionArgs();
396-
// if run with "--debug logfile.txt" debug=true, files=["logfile.txt"]
397-
}
398-
399-
}
346+
include::{include-springbootfeatures}/springapplication/ApplicationArgumentsExample.java[tag=*]
400347
----
401348

402349
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
415362
The `CommandLineRunner` interfaces provides access to application arguments as a string array, whereas the `ApplicationRunner` uses the `ApplicationArguments` interface discussed earlier.
416363
The following example shows a `CommandLineRunner` with a `run` method:
417364

418-
[source,java,pending-extract=true,indent=0]
365+
[source,java,indent=0]
419366
----
420-
import org.springframework.boot.*;
421-
import org.springframework.stereotype.*;
422-
423-
@Component
424-
public class MyBean implements CommandLineRunner {
425-
426-
public void run(String... args) {
427-
// Do something...
428-
}
429-
430-
}
367+
include::{include-springbootfeatures}/springapplication/CommandLineRunnerExample.java[tag=*]
431368
----
432369

433370
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
473410
You can choose an `ApplicationStartup` implementation when setting up the `SpringApplication` instance.
474411
For example, to use the `BufferingApplicationStartup`, you could write:
475412

476-
[source,java,pending-extract=true,indent=0]
413+
[source,java,indent=0]
477414
----
478-
public static void main(String[] args) {
479-
SpringApplication app = new SpringApplication(MySpringConfiguration.class);
480-
app.setApplicationStartup(new BufferingApplicationStartup(2048));
481-
app.run(args);
482-
}
415+
include::{include-springbootfeatures}/springapplication/startup/MyApplication.java[tag=*]
483416
----
484417

485418
The first available implementation, `FlightRecorderApplicationStartup` is provided by Spring Framework.
@@ -532,20 +465,9 @@ Config data files are considered in the following order:
532465

533466
To provide a concrete example, suppose you develop a `@Component` that uses a `name` property, as shown in the following example:
534467

535-
[source,java,pending-extract=true,indent=0]
468+
[source,java,indent=0]
536469
----
537-
import org.springframework.stereotype.*;
538-
import org.springframework.beans.factory.annotation.*;
539-
540-
@Component
541-
public class MyBean {
542-
543-
@Value("${name}")
544-
private String name;
545-
546-
// ...
547-
548-
}
470+
include::{include-springbootfeatures}/externalizedconfiguration/valueinjection/MyBean.java[tag=*]
549471
----
550472

551473
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 <<boot-features-external-config-vs-value,differences between `
11151037
==== JavaBean properties binding
11161038
It is possible to bind a bean declaring standard JavaBean properties as shown in the following example:
11171039

1118-
[source,java,pending-extract=true,indent=0]
1040+
[source,java,indent=0]
11191041
----
1120-
package com.example;
1121-
1122-
import java.net.InetAddress;
1123-
import java.util.ArrayList;
1124-
import java.util.Collections;
1125-
import java.util.List;
1126-
1127-
import org.springframework.boot.context.properties.ConfigurationProperties;
1128-
1129-
@ConfigurationProperties("acme")
1130-
public class AcmeProperties {
1131-
1132-
private boolean enabled;
1133-
1134-
private InetAddress remoteAddress;
1135-
1136-
private final Security security = new Security();
1137-
1138-
public boolean isEnabled() { ... }
1139-
1140-
public void setEnabled(boolean enabled) { ... }
1141-
1142-
public InetAddress getRemoteAddress() { ... }
1143-
1144-
public void setRemoteAddress(InetAddress remoteAddress) { ... }
1145-
1146-
public Security getSecurity() { ... }
1147-
1148-
public static class Security {
1149-
1150-
private String username;
1151-
1152-
private String password;
1153-
1154-
private List<String> roles = new ArrayList<>(Collections.singleton("USER"));
1155-
1156-
public String getUsername() { ... }
1157-
1158-
public void setUsername(String username) { ... }
1159-
1160-
public String getPassword() { ... }
1161-
1162-
public void setPassword(String password) { ... }
1163-
1164-
public List<String> getRoles() { ... }
1165-
1166-
public void setRoles(List<String> roles) { ... }
1167-
1168-
}
1169-
}
1042+
include::{include-springbootfeatures}/externalizedconfiguration/javabeanbinding/AcmeProperties.java[tag=*]
11701043
----
11711044

11721045
The preceding POJO defines the following properties:
@@ -1205,63 +1078,9 @@ Finally, only standard Java Bean properties are considered and binding on static
12051078
==== Constructor binding
12061079
The example in the previous section can be rewritten in an immutable fashion as shown in the following example:
12071080

1208-
[source,java,pending-extract=true,indent=0]
1081+
[source,java,indent=0]
12091082
----
1210-
package com.example;
1211-
1212-
import java.net.InetAddress;
1213-
import java.util.List;
1214-
1215-
import org.springframework.boot.context.properties.ConfigurationProperties;
1216-
import org.springframework.boot.context.properties.ConstructorBinding;
1217-
import org.springframework.boot.context.properties.bind.DefaultValue;
1218-
1219-
@ConstructorBinding
1220-
@ConfigurationProperties("acme")
1221-
public class AcmeProperties {
1222-
1223-
private final boolean enabled;
1224-
1225-
private final InetAddress remoteAddress;
1226-
1227-
private final Security security;
1228-
1229-
public AcmeProperties(boolean enabled, InetAddress remoteAddress, Security security) {
1230-
this.enabled = enabled;
1231-
this.remoteAddress = remoteAddress;
1232-
this.security = security;
1233-
}
1234-
1235-
public boolean isEnabled() { ... }
1236-
1237-
public InetAddress getRemoteAddress() { ... }
1238-
1239-
public Security getSecurity() { ... }
1240-
1241-
public static class Security {
1242-
1243-
private final String username;
1244-
1245-
private final String password;
1246-
1247-
private final List<String> roles;
1248-
1249-
public Security(String username, String password,
1250-
@DefaultValue("USER") List<String> roles) {
1251-
this.username = username;
1252-
this.password = password;
1253-
this.roles = roles;
1254-
}
1255-
1256-
public String getUsername() { ... }
1257-
1258-
public String getPassword() { ... }
1259-
1260-
public List<String> getRoles() { ... }
1261-
1262-
}
1263-
1264-
}
1083+
include::{include-springbootfeatures}/externalizedconfiguration/constructorbinding/AcmeProperties.java[tag=*]
12651084
----
12661085

12671086
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
12731092
By default, if no properties are bound to `Security`, the `AcmeProperties` instance will contain a `null` value for `security`.
12741093
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:
12751094

1276-
[source,java,pending-extract=true,indent=0]
1095+
[source,java,indent=0]
12771096
----
1278-
package com.example;
1279-
import java.net.InetAddress;
1280-
import java.util.List;
1281-
1282-
import org.springframework.boot.context.properties.ConfigurationProperties;
1283-
import org.springframework.boot.context.properties.ConstructorBinding;
1284-
import org.springframework.boot.context.properties.bind.DefaultValue;
1285-
1286-
@ConstructorBinding
1287-
@ConfigurationProperties("acme")
1288-
public class AcmeProperties {
1289-
1290-
private final boolean enabled;
1291-
1292-
private final InetAddress remoteAddress;
1293-
1294-
private final Security security;
1295-
1296-
public AcmeProperties(boolean enabled, InetAddress remoteAddress, @DefaultValue Security security) {
1297-
this.enabled = enabled;
1298-
this.remoteAddress = remoteAddress;
1299-
this.security = security;
1300-
}
1301-
}
1097+
include::{include-springbootfeatures}/externalizedconfiguration/constructorbinding/nonnull/AcmeProperties.java[tag=*]
13021098
----
13031099

13041100

0 commit comments

Comments
 (0)