|
1 | 1 | /*
|
2 |
| - * Copyright 2012-2022 the original author or authors. |
| 2 | + * Copyright 2012-2024 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
25 | 25 | import org.springframework.boot.context.properties.bind.Nested;
|
26 | 26 |
|
27 | 27 | /**
|
28 |
| - * Indicates that a field in a {@link ConfigurationProperties @ConfigurationProperties} |
| 28 | + * Indicates that a property in a {@link ConfigurationProperties @ConfigurationProperties} |
29 | 29 | * object should be treated as if it were a nested type. This annotation has no bearing on
|
30 | 30 | * the actual binding processes, but it is used by the
|
31 |
| - * {@code spring-boot-configuration-processor} as a hint that a field is not bound as a |
32 |
| - * single value. When this is specified, a nested group is created for the field and its |
33 |
| - * type is harvested. |
| 31 | + * {@code spring-boot-configuration-processor} as a hint that a property is not bound as a |
| 32 | + * single value. When this is specified, a nested group is created for the property and |
| 33 | + * its type is harvested. |
| 34 | + * <p> |
| 35 | + * In the example below, {@code Host} is flagged as a nested property using its field and |
| 36 | + * an {@code example.server.host} nested group is created with any property that |
| 37 | + * {@code Host} defines:<pre><code class="java"> |
| 38 | + * @ConfigurationProperties("example.server") |
| 39 | + * class ServerProperties { |
| 40 | + * |
| 41 | + * @NestedConfigurationProperty |
| 42 | + * private final Host host = new Host(); |
| 43 | + * |
| 44 | + * public Host getHost() { ... } |
| 45 | + * |
| 46 | + * // Other properties, getter, setter. |
| 47 | + * |
| 48 | + * }</code></pre> |
| 49 | + * <p> |
| 50 | + * The annotation can also be specified on a getter method. If you use records, you can |
| 51 | + * annotate the record component. |
34 | 52 | * <p>
|
35 | 53 | * This has no effect on collections and maps as these types are automatically identified.
|
| 54 | + * Also, the annotation is not necessary if the target type is an inner class of the |
| 55 | + * {@link ConfigurationProperties @ConfigurationProperties} object. In the example below, |
| 56 | + * {@code Host} is detected as a nested type as it is defined as an inner class: |
| 57 | + * <pre><code class="java"> |
| 58 | + * @ConfigurationProperties("example.server") |
| 59 | + * class ServerProperties { |
| 60 | + * |
| 61 | + * private final Host host = new Host(); |
| 62 | + * |
| 63 | + * public Host getHost() { ... } |
| 64 | + * |
| 65 | + * // Other properties, getter, setter. |
| 66 | + * |
| 67 | + * public static class Host { |
| 68 | + * |
| 69 | + * // properties, getter, setter. |
| 70 | + * |
| 71 | + * } |
| 72 | + * |
| 73 | + * }</code></pre> |
36 | 74 | *
|
37 | 75 | * @author Stephane Nicoll
|
38 | 76 | * @author Phillip Webb
|
39 | 77 | * @since 1.2.0
|
40 | 78 | */
|
41 |
| -@Target({ ElementType.FIELD, ElementType.RECORD_COMPONENT }) |
| 79 | +@Target({ ElementType.FIELD, ElementType.RECORD_COMPONENT, ElementType.METHOD }) |
42 | 80 | @Retention(RetentionPolicy.RUNTIME)
|
43 | 81 | @Documented
|
44 | 82 | @Nested
|
|
0 commit comments