Skip to content

Commit 8bcf518

Browse files
committed
Merge branch '2.3.x'
Closes gh-22891
2 parents 44e8077 + 92445fc commit 8bcf518

File tree

1 file changed

+71
-32
lines changed

1 file changed

+71
-32
lines changed

spring-boot-project/spring-boot-docs/src/docs/asciidoc/appendix-configuration-metadata.adoc

Lines changed: 71 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -695,6 +695,11 @@ The following metadata snippet corresponds to the standard `spring.profiles.acti
695695
== Generating Your Own Metadata by Using the Annotation Processor
696696
You can easily generate your own configuration metadata file from items annotated with `@ConfigurationProperties` by using the `spring-boot-configuration-processor` jar.
697697
The jar includes a Java annotation processor which is invoked as your project is compiled.
698+
699+
700+
701+
[[configuration-metadata-annotation-processor-setup]]
702+
=== Configuring the Annotation Processor
698703
To use the processor, include a dependency on `spring-boot-configuration-processor`.
699704

700705
With Maven the dependency should be declared as optional, as shown in the following example:
@@ -759,20 +764,73 @@ If you are using an `additional-spring-configuration-metadata.json` file, the `c
759764

760765
This dependency ensures that the additional metadata is available when the annotation processor runs during compilation.
761766

762-
The processor picks up both classes and methods that are annotated with `@ConfigurationProperties`.
763-
The Javadoc for field values within configuration classes is used to populate the `description` attribute.
767+
[NOTE]
768+
====
769+
If you are using AspectJ in your project, you need to make sure that the annotation processor runs only once.
770+
There are several ways to do this.
771+
With Maven, you can configure the `maven-apt-plugin` explicitly and add the dependency to the annotation processor only there.
772+
You could also let the AspectJ plugin run all the processing and disable annotation processing in the `maven-compiler-plugin` configuration, as follows:
764773
765-
NOTE: You should only use plain text with `@ConfigurationProperties` field Javadoc, since they are not processed before being added to the JSON.
774+
[source,xml,indent=0,subs="verbatim,quotes,attributes"]
775+
----
776+
<plugin>
777+
<groupId>org.apache.maven.plugins</groupId>
778+
<artifactId>maven-compiler-plugin</artifactId>
779+
<configuration>
780+
<proc>none</proc>
781+
</configuration>
782+
</plugin>
783+
----
784+
====
766785

767-
If the class has a single constructor with at least one parameters, one property is created per constructor parameter.
768-
Otherwise, properties are discovered through the presence of standard getters and setters with special handling for collection types (that is detected even if only a getter is present).
769786

787+
788+
[[configuration-metadata-annotation-processor-metadata-generation]]
789+
=== Automatic Metadata Generation
790+
The processor picks up both classes and methods that are annotated with `@ConfigurationProperties`.
791+
792+
If the class is also annotated with `@ConstructorBinding`, a single constructor is expected and one property is created per constructor parameter.
793+
Otherwise, properties are discovered through the presence of standard getters and setters with special handling for collection and map types (that is detected even if only a getter is present).
770794
The annotation processor also supports the use of the `@Data`, `@Getter`, and `@Setter` lombok annotations.
771795

772-
The annotation processor cannot auto-detect default values for ``Enum``s and ``Collections``s.
773-
In the cases where a `Collection` or `Enum` property has a non-empty default value, <<configuration-metadata-additional-metadata,manual metadata>> should be provided.
796+
Consider the following example:
797+
798+
[source,java,indent=0,subs="verbatim,attributes"]
799+
----
800+
@ConfigurationProperties(prefix="server")
801+
public class ServerProperties {
802+
803+
/**
804+
* Name of the server.
805+
*/
806+
private String name;
807+
808+
/**
809+
* IP address to listen to.
810+
*/
811+
private String ip = "127.0.0.1";
812+
813+
/**
814+
* Port to listener to.
815+
*/
816+
private int port = 9797;
817+
818+
// ... getter and setters
819+
820+
}
821+
----
822+
823+
This exposes three properties where `server.name` has no default and `server.ip` and `server.port` defaults to `"127.0.0.1"` and `9797` respectively.
824+
The Javadoc on fields is used to populate the `description` attribute. For instance, the description of `server.ip` is "IP address to listen to.".
774825

775-
Consider the following class:
826+
NOTE: You should only use plain text with `@ConfigurationProperties` field Javadoc, since they are not processed before being added to the JSON.
827+
828+
The annotation processor applies a number of heuristics to extract the default value from the source model.
829+
Default values have to be provided statically. In particular, do not refer to a constant defined in another class.
830+
Also, the annotation processor cannot auto-detect default values for ``Enum``s and ``Collections``s.
831+
832+
For cases where the default value could not be detected, <<configuration-metadata-additional-metadata,manual metadata>> should be provided.
833+
Consider the following example:
776834

777835
[source,java,indent=0,subs="verbatim,quotes,attributes"]
778836
----
@@ -811,33 +869,14 @@ In order to document default values for properties in the class above, you could
811869
]}
812870
----
813871

814-
Only the `name` of the property is required to document additional fields with manual metadata.
815-
816-
[NOTE]
817-
====
818-
If you are using AspectJ in your project, you need to make sure that the annotation processor runs only once.
819-
There are several ways to do this.
820-
With Maven, you can configure the `maven-apt-plugin` explicitly and add the dependency to the annotation processor only there.
821-
You could also let the AspectJ plugin run all the processing and disable annotation processing in the `maven-compiler-plugin` configuration, as follows:
822-
823-
[source,xml,indent=0,subs="verbatim,quotes,attributes"]
824-
----
825-
<plugin>
826-
<groupId>org.apache.maven.plugins</groupId>
827-
<artifactId>maven-compiler-plugin</artifactId>
828-
<configuration>
829-
<proc>none</proc>
830-
</configuration>
831-
</plugin>
832-
----
833-
====
834-
872+
NOTE: Only the `name` of the property is required to document additional metadata for existing properties.
835873

836874

837-
[[configuration-metadata-nested-properties]]
838-
=== Nested Properties
875+
[[configuration-metadata-annotation-processor-metadata-generation-nested]]
876+
==== Nested Properties
839877
The annotation processor automatically considers inner classes as nested properties.
840-
Consider the following class:
878+
Rather than documenting the `ip` and `port` at the root of the namespace, we could create a sub-namespace for it.
879+
Consider the updated example:
841880

842881
[source,java,indent=0,subs="verbatim,quotes,attributes"]
843882
----

0 commit comments

Comments
 (0)