Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

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
languagexml
${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
languagejava
@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
languagejava
@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
languagejava
@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

TitleName (for @Source)DescriptionApplicable To (including using @Via)Notes
Value Map

...

valuemapGets a property from a Value MapAny object which is or can be adapted to a ValueMap 
OSGI Servicesosgi-servicesLookup services based on class name

...

Any objectEffectively ignores name.
Script Bindings

...

script-bindings

...

Lookup objects in the script bindings objectA ServletRequest object which has the Sling Bindings attribute defined 
Child Resources

...

child-resources

...

Gets a child resource by nameResource objects 
Request Attributes

...

request-

...

attributesGet a request attributeServletRequest objects