This is an automated email from the ASF dual-hosted git repository.
kwin pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sling-site.git
The following commit(s) were added to refs/heads/master by this push:
new 855974a71 Update documentation on Sling Model constructors and
injectors
855974a71 is described below
commit 855974a71878b7a9f3d605f823d05c8d044ea875
Author: Konrad Windszus <[email protected]>
AuthorDate: Wed Oct 29 17:15:17 2025 +0100
Update documentation on Sling Model constructors and injectors
Clarify constructor injection requirements and adaptable types for Sling
Models. Clarify support for records.
---
.../jbake/content/documentation/bundles/models.md | 28 +++++++++++++++++++++-
1 file changed, 27 insertions(+), 1 deletion(-)
diff --git a/src/main/jbake/content/documentation/bundles/models.md
b/src/main/jbake/content/documentation/bundles/models.md
index d253f6d36..9eaf3132f 100644
--- a/src/main/jbake/content/documentation/bundles/models.md
+++ b/src/main/jbake/content/documentation/bundles/models.md
@@ -88,11 +88,37 @@ Constructor injection is also supported (as of [Sling
Models 1.1.0](https://issu
}
}
-Because the name of a constructor argument parameter cannot be detected via
the Java Reflection API a `@Named` annotation (or a `name` element on injector
specific annotations) is mandatory for injectors that require a name for
resolving the injection.
+Because the name of a constructor argument parameter cannot be detected via
the Java Reflection API a `@Named` annotation (or a `name` element on injector
specific annotations) is mandatory for injectors that require a name for
resolving the injection. This restriction can be bypassed since [Sling Models
1.7.0](https://issues.apache.org/jira/browse/SLING-11917) when the compiler
generates metadata for method names via [javac option
`-parameters`](https://docs.oracle.com/en/java/javase/21 [...]
In order for a constructor to be used for injection *it has to be annotated on
method level with `@Inject`*. In addition using injector-specific annotations
on parameter level is supported.
+You can also reference the adaptable itself via a constructor argument with
the `@Self` annotation.
Constructors may use any visibility modifier (as of [Sling Models
1.5.0](https://issues.apache.org/jira/browse/SLING-8069)).
+In addition to that constructors taking a single argument of the adaptable
type are supported. Those don't need any annotations. So
+
+ ::java
+ @Model(adaptables=Resource.class)
+ public class MyModel {
+ @Inject
+ public MyModel(@Self Resource resource) {
+ // constructor code
+ }
+ }
+
+and
+
+ ::java
+ @Model(adaptables=Resource.class)
+ public class MyModel {
+ public MyModel(Resource resource) {
+ // constructor code
+ }
+ }
+
+are functionally equivalent.
+
+The implicit constructors of [record classes] are supported since [Sling
Models 1.7.0](https://issues.apache.org/jira/browse/SLING-12359) as well.
+
## @Model and Adaptable Types
When defining a Sling Model class, the `adaptables` parameter to the `@Model`
annotation is mostly determined by the injectors being used. The provided class
must satisfy the needs of all injectors (for the details see [the table
below](#available-injectors-1)). For example if the model class only uses the
`ValueMap` injector, the adaptables parameter can be a `Resource`, a
`SlingJakartaHttpServletRequest`, a `SlingHttpServletRequest` or both. But if
the `Request Attribute` injector is u [...]