Table of Contents |
---|
Info |
---|
This functionality has been renamed Sling Models and is documented at http://sling.apache.org/documentation/bundles/models.html |
Many Sling projects want to be able to create model objects - POJOs which are automatically mapped from Sling objects, typically resources, but also request objects. Sometimes these POJOs need OSGi services as well.
...
Code Block | ||
---|---|---|
| ||
${sling:adaptTo(resource, 'org.apache.sling.yamf.it.models.MyModel')} |
As with other AdapterFactories, if the adaptation can't be made for any reason, adaptTo() returns null.
Other Options
If the field or method name doesn't exactly match the property name, @Named can be used:
...
@PostConstruct methods in a super class will be invoked first.
Using projection, you can inject If the injection should be based on a "child" object JavaBean property of the adaptable. , you can indicate this using the @Via annotation:
Code Block | ||
---|---|---|
| ||
@Model(adaptables=SlingHttpServletRequest.class) public interface MyModel { // will return request.getResource().adaptTo(ValueMap.class).get("propertyName", String.class) @Inject @Projection@Via("resource") String getPropertyName(); } |
If there is ambiguity where a given injection could be handled by more than one injector, the @Source annotation can be used to define which injector is responsible:
Code Block | ||
---|---|---|
| ||
@Model(adaptables=SlingHttpServletRequest.class)
public interface MyModel {
// Ensure that "resource" is retrived from the bindings, not a request attribute
@Inject @Source("script-bindings")
Resource getResource();
} |
If the injected object does not match the desired type and the object implements the Adaptable interface, YAMF will try to adapt it. This provides the ability to create rich object graphs. For example:
Code Block | ||
---|---|---|
| ||
@Model(adaptables=Resource.class) public interface MyModel { @Inject ImageModel getImage(); } @Model(adaptables=Resource.class) public interface ImageModel { @Inject String getPath(); } |
When a resource is adapted to MyModel, a child resource named image is automatically adapted to an instance of ImageModel.
Annotation Reference
- @Model - declares a model class or interface
- @Inject - marks a field or method as injectable
- @Named - declare a name for the injection (otherwise, defaults based on field or method name).
- @Optional - marks a field or method injection as optional
- @Source - explictly tie an injected field or method to a particular injector (by name). Can also be on other annotations.
- @Filter - an OSGi service filter
- @PostConstruct - methods to call upon model option creation (only for model classes)
- @Projection @Via - project use a JavaBean property of the adaptable as the source of the adaptable.injection
- @Default - set default values for a field or method
Available Injectors
Title | Name (for @Source) | Description | Applicable To (including using @Via) | Notes |
---|---|---|---|---|
Value Map |
...
valuemap | Gets a property from a Value Map | Any object which is or can be adapted to a ValueMap | ||
OSGI Services | osgi-services | Lookup services based on class name |
...
Any object | Effectively ignores name. | |
Script Bindings |
...
script-bindings |
...
Lookup objects in the script bindings object | A ServletRequest object which has the Sling Bindings attribute defined | ||
Child Resources |
...
child-resources |
...
Gets a child resource by name | Resource objects | ||
Request Attributes |
...
request- |
...
attributes | Get a request attribute | ServletRequest objects |