diff --git a/spring-boot-project/spring-boot-docs/src/docs/asciidoc/native-image/advanced-topics.adoc b/spring-boot-project/spring-boot-docs/src/docs/asciidoc/native-image/advanced-topics.adoc index a7e8f11169d7..5fa7ebbfa910 100644 --- a/spring-boot-project/spring-boot-docs/src/docs/asciidoc/native-image/advanced-topics.adoc +++ b/spring-boot-project/spring-boot-docs/src/docs/asciidoc/native-image/advanced-topics.adoc @@ -5,20 +5,36 @@ [[native-image.advanced.nested-configuration-properties]] === Nested Configuration Properties + Reflection hints are automatically created for configuration properties by the Spring ahead-of-time engine. Nested configuration properties which are no inner classes, however, *must* be annotated with `@NestedConfigurationProperty`, otherwise they won't be detected and will not be bindable. include::code:MyProperties[] +where `Nested` is: + +include::code:Nested[] + The example above produces configuration properties for `my.properties.name` and `my.properties.nested.number`. Without the `@NestedConfigurationProperty` annotation on the `nested` field, the `my.properties.nested.number` property would not be bindable in a native image. -NOTE: Please use public getters and setters, otherwise the properties will not be bindable. +When using constructor binding, you have to annotate the field with `@NestedConfigurationProperty`: + +include::code:MyPropertiesCtor[] +When using records, you have to annotate the parameter with `@NestedConfigurationProperty`: +include::code:MyPropertiesRecord[] + +When using Kotlin, you need to annotate the parameter of a data class with `@NestedConfigurationProperty`: + +include::code:MyPropertiesKotlin[] + +NOTE: Please use public getters and setters in all cases, otherwise the properties will not be bindable. [[native-image.advanced.converting-executable-jars]] === Converting a Spring Boot Executable Jar + It is possible to convert a Spring Boot <> into a native image as long at the jar contains the AOT generated assets. This can be useful for a number of reasons, including: diff --git a/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/nativeimage/advanced/nestedconfigurationproperties/MyProperties.java b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/nativeimage/advanced/nestedconfigurationproperties/MyProperties.java index 0f73ef5dd68c..d16229208c49 100644 --- a/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/nativeimage/advanced/nestedconfigurationproperties/MyProperties.java +++ b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/nativeimage/advanced/nestedconfigurationproperties/MyProperties.java @@ -42,19 +42,3 @@ public Nested getNested() { // @fold:off } - -class Nested { - - private int number; - - // @fold:on // getters / setters... - public int getNumber() { - return this.number; - } - - public void setNumber(int number) { - this.number = number; - } - // @fold:off - -} diff --git a/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/nativeimage/advanced/nestedconfigurationproperties/MyPropertiesCtor.java b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/nativeimage/advanced/nestedconfigurationproperties/MyPropertiesCtor.java new file mode 100644 index 000000000000..779bf7708be2 --- /dev/null +++ b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/nativeimage/advanced/nestedconfigurationproperties/MyPropertiesCtor.java @@ -0,0 +1,45 @@ +/* + * Copyright 2012-2022 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.nativeimage.advanced.nestedconfigurationproperties; + +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.boot.context.properties.NestedConfigurationProperty; + +@ConfigurationProperties(prefix = "my.properties") +public class MyPropertiesCtor { + + private final String name; + + @NestedConfigurationProperty + private final Nested nested; + + public MyPropertiesCtor(String name, Nested nested) { + this.name = name; + this.nested = nested; + } + + // @fold:on // getters / setters... + public String getName() { + return this.name; + } + + public Nested getNested() { + return this.nested; + } + // @fold:off + +} diff --git a/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/nativeimage/advanced/nestedconfigurationproperties/MyPropertiesRecord.java b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/nativeimage/advanced/nestedconfigurationproperties/MyPropertiesRecord.java new file mode 100644 index 000000000000..3065a932ffa1 --- /dev/null +++ b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/nativeimage/advanced/nestedconfigurationproperties/MyPropertiesRecord.java @@ -0,0 +1,25 @@ +/* + * Copyright 2012-2022 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.nativeimage.advanced.nestedconfigurationproperties; + +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.boot.context.properties.NestedConfigurationProperty; + +@ConfigurationProperties(prefix = "my.properties") +public record MyPropertiesRecord(String name, @NestedConfigurationProperty Nested nested) { + +} diff --git a/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/nativeimage/advanced/nestedconfigurationproperties/Nested.java b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/nativeimage/advanced/nestedconfigurationproperties/Nested.java new file mode 100644 index 000000000000..67e2c05b2ac8 --- /dev/null +++ b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/nativeimage/advanced/nestedconfigurationproperties/Nested.java @@ -0,0 +1,33 @@ +/* + * Copyright 2012-2022 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.nativeimage.advanced.nestedconfigurationproperties; + +public class Nested { + + private int number; + + // @fold:on // getters / setters... + public int getNumber() { + return this.number; + } + + public void setNumber(int number) { + this.number = number; + } + // @fold:off + +} diff --git a/spring-boot-project/spring-boot-docs/src/main/kotlin/org/springframework/boot/docs/nativeimage/advanced/nestedconfigurationproperties/MyPropertiesKotlin.kt b/spring-boot-project/spring-boot-docs/src/main/kotlin/org/springframework/boot/docs/nativeimage/advanced/nestedconfigurationproperties/MyPropertiesKotlin.kt new file mode 100644 index 000000000000..d00a5bd34269 --- /dev/null +++ b/spring-boot-project/spring-boot-docs/src/main/kotlin/org/springframework/boot/docs/nativeimage/advanced/nestedconfigurationproperties/MyPropertiesKotlin.kt @@ -0,0 +1,26 @@ +/* + * Copyright 2012-2022 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.nativeimage.advanced.nestedconfigurationproperties + +import org.springframework.boot.context.properties.ConfigurationProperties +import org.springframework.boot.context.properties.NestedConfigurationProperty + +@ConfigurationProperties(prefix = "my.properties") +data class MyPropertiesKotlin( + val name: String, + @NestedConfigurationProperty val nested: Nested +)