Skip to content

Commit be33827

Browse files
committed
Simplify property validation sample and document need for static bean method
Closes gh-6627
1 parent 43e020e commit be33827

File tree

2 files changed

+23
-25
lines changed

2 files changed

+23
-25
lines changed

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1070,9 +1070,13 @@ as `@Valid` to trigger its validation. For example, building upon the above
10701070
----
10711071

10721072
You can also add a custom Spring `Validator` by creating a bean definition called
1073-
`configurationPropertiesValidator`. There is a
1074-
{github-code}/spring-boot-samples/spring-boot-sample-property-validation[Validation sample]
1075-
so you can see how to set things up.
1073+
`configurationPropertiesValidator`. The `@Bean` method should be declared `static`. The
1074+
configuration properties validator is created very early in the application's lifecycle
1075+
and declaring the `@Bean` method as static allows the bean to be created without having to
1076+
instantiate the `@Configuration` class. This avoids any problems that may be caused by
1077+
early instantiation. There is a
1078+
{github-code}/spring-boot-samples/spring-boot-sample-property-validation[property
1079+
validation sample] so you can see how to set things up.
10761080

10771081
TIP: The `spring-boot-actuator` module includes an endpoint that exposes all
10781082
`@ConfigurationProperties` beans. Simply point your web browser to `/configprops`
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2015 the original author or authors.
2+
* Copyright 2012-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,42 +16,36 @@
1616

1717
package sample.propertyvalidation;
1818

19-
import org.springframework.beans.factory.annotation.Autowired;
2019
import org.springframework.boot.CommandLineRunner;
2120
import org.springframework.boot.autoconfigure.SpringBootApplication;
2221
import org.springframework.boot.builder.SpringApplicationBuilder;
2322
import org.springframework.context.annotation.Bean;
24-
import org.springframework.context.annotation.Profile;
25-
import org.springframework.stereotype.Service;
2623
import org.springframework.validation.Validator;
2724

2825
@SpringBootApplication
29-
public class SamplePropertyValidationApplication {
26+
public class SamplePropertyValidationApplication implements CommandLineRunner {
27+
28+
private final SampleProperties properties;
29+
30+
public SamplePropertyValidationApplication(SampleProperties properties) {
31+
this.properties = properties;
32+
}
3033

3134
@Bean
32-
public Validator configurationPropertiesValidator() {
35+
public static Validator configurationPropertiesValidator() {
3336
return new SamplePropertiesValidator();
3437
}
3538

36-
@Service
37-
@Profile("app")
38-
static class Startup implements CommandLineRunner {
39-
40-
@Autowired
41-
private SampleProperties properties;
42-
43-
@Override
44-
public void run(String... args) {
45-
System.out.println("=========================================");
46-
System.out.println("Sample host: " + this.properties.getHost());
47-
System.out.println("Sample port: " + this.properties.getPort());
48-
System.out.println("=========================================");
49-
}
39+
@Override
40+
public void run(String... args) {
41+
System.out.println("=========================================");
42+
System.out.println("Sample host: " + this.properties.getHost());
43+
System.out.println("Sample port: " + this.properties.getPort());
44+
System.out.println("=========================================");
5045
}
5146

5247
public static void main(String[] args) throws Exception {
53-
new SpringApplicationBuilder(SamplePropertyValidationApplication.class)
54-
.profiles("app").run(args);
48+
new SpringApplicationBuilder(SamplePropertyValidationApplication.class).run(args);
5549
}
5650

5751
}

0 commit comments

Comments
 (0)