...
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:
...
Code Block |
|
|
@Model(adaptables=SlingHttpServletRequest.class)
public interface MyModel {
@Inject @Projection("resource")
String getPropertyName();
} |
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 - project a JavaBean property as the adaptable.
- @Default - set default values for a field or method
...