what’s new in sling models 1 · apache sling & friends tech meetup berlin, 22-24 september...
TRANSCRIPT
APACHE SLING & FRIENDS TECH MEETUPBERLIN, 22-24 SEPTEMBER 2014
What’s new in Sling Models 1.1Stefan Seifert, pro!vision GmbH
About the Speaker
adaptTo() 2014 2
CQ5/AEM6 Developer Apache Sling Committer CTO of pro!vision GmbH
http://www.pro-vision.de
Overview
adaptTo() 2014 3
Short Recap on Sling Models 1.0.x Injector-specific Annotations Constructor Injection New Injectors Alternate Adapter Interfaces
Sling Models Timeline
adaptTo() 2014 4
March 2014 – Release 1.0.2 Included in AEM 6.0
June 2014 – Release 1.0.6 Custom Injector Annotations
September 2014 – Release 1.1.0
Short Recap on Sling Models 1.0.x
Resource Data in POJO
adaptTo() 2014 6
Resource
Propertiesname = T-Shirtprice = 19.50
available = true
@Model(adaptables = Resource.class)
public class Product {
@Inject
private String name;
@Inject
private double price;
@Inject
private boolean available;
}
adapt
Product item = resource.adaptTo(Product.class);
Alternative: Interface instead of POJO
adaptTo() 2014 7
Resource
Propertiesname = T-Shirtprice = 19.50
available = true
@Model(adaptables = Resource.class)
public interface Product {
@Inject
public String getName();
@Inject
public double getPrice();
@Inject
public boolean isAvailable();
}
adapt
Product item = resource.adaptTo(Product.class);
What can be injected (1.0.x)
adaptTo() 2014 8
Script Variables (Sling Bindings) Value Map (Resource properties) Child Resources Request Attribute OSGi Service References
Design Goals
adaptTo() 2014 9
Integrates nicely with adaptTo but does not require a custom AdapterFactory
POJOs and Interfaces with Annotations Name-based and class-based injections Pluggable Architecture
Learn more about the basics
adaptTo() 2014 10
Presentation by Justin Edelsonhttp://slideshare.net/justinedelson/sling-models-overview
Injector-specific Annotations
Custom Injector Annotations (since 1.0.6)
adaptTo() 2014 12
@Inject queries all Injector implementations “First Match” wins
Injector-specific annotationsspecify the source explicitly
One custom annotation instead of a couple of generic ones
Example: Value Map
adaptTo() 2014 13
Generic Annotation Injector-specific Annotation
@Inject
String name;
@Inject
@Named("jcr:title")
@Optional
String title;
@ValueMapValue
String name;
@ValueMapValue(name="jcr:title",
optional=true)
String title;
Example: OSGi Service
adaptTo() 2014 14
Generic Annotation Injector-specific Annotation
@Inject
SlingSettingsService settings;
@Inject
@Filter("(paths=/bin/something)")
List<Servlet> servlets;
@OSGiService
SlingSettingsService settings;
@OSGiService(
filter="(paths=/bin/something)")
List<Servlet> servlets;
Constructor Injection
Constructor Injection (since 1.1.0)
adaptTo() 2014 16
Adds support for Constructor Injection in POJOs
Supports same features and annotations as field injection
Constructor Injection Example
adaptTo() 2014 17
@Model(adaptables = Resource.class)
public class MyController {
@Inject
public MyController(
@Named("name") String name,
@ValueMapValue(name = "jcr:title", optional = true) String title,
@OSGiService SlingSettingsService settings) {
// initialization logic
}
}
Constructor Injection Hints
adaptTo() 2014 18
@Inject is not placed on constructor arguments, but on constructor itself
Specifying a name for name-based injections is mandatory JDK does not support reading constructor
parameter names via reflection
New Injectors
New Injectors in 1.1.0
adaptTo() 2014 20
Self Injector Sling Object Injector Resource Path Injector
Self Injector
adaptTo() 2014 21
Injects the adaptable object itself Class-based injection
Or an object that can be adapted from it If the @Self annotation is present
Self Injector Example
adaptTo() 2014 22
@Model(adaptables = Resource.class)
public class MyController {
// the adaptable itself
@Self
private Resource resource;
// an object that can be adapted from the Resource
@Self
private Product product;
// an object that can be adapted from the Resource
@Self
private ValueMap properties;
}
Sling Object Injector
adaptTo() 2014 23
Injects commonly used sling objects Class-based injection
Supports Request, Response, Resource Resolver, Current Resource and Sling Script Helper
Sling Object Injector Example
adaptTo() 2014 24
@Model(adaptables = SlingHttpServletRequest.class)
public class MyController {
@SlingObject
private Resource resource;
@SlingObject
private ResourceResolver resolver;
@SlingObject
private SlingHttpServletRequest request;
}
Sling Object Injector Hints
adaptTo() 2014 25
For AEM Objects use AEM Object Injector e.g. Page, Page Manager, WCM Mode, Design Not part of Sling Models
Implementations http://wcm.io/sling/models/ or http://adobe-consulting-services.github.io/acs-aem-commons/
Resource Path Injector
adaptTo() 2014 26
Injects a resource by its path@Model(adaptables = Resource.class)
public class MyController {
// injects resource from given path
@ResourcePath(path="/path/to/resource")
private Resource resource;
// injects resource from path that's stored in the "resourcePath" property
@ResourcePath(name="resourcePath")
private Resource otherResource;
}
Alternate Adapter Interfaces
Alternate Adapter Interfaces (since 1.1.0)
adaptTo() 2014 28
Model implementations can be adapted to Interfaces they implement Like to OSGi SCR @Service annotation
If multiple models implement the same interface “ImplementationPicker” services can choose the right one
Alternate Adapter Interface Example
adaptTo() 2014 29
@Model(adaptables = SlingHttpServletRequest.class,
adapters = ProductManager.class)
public class ProductManagerImpl implements ProductManager {
@AemObject
private ResourceResolver resolver;
@Override
public List<Product> listProducts() {
// business logic
}
}
Summing up
adaptTo() 2014 30
All this new Sling Models Features allow a new architecture style for Sling Components and supporting business classes
More in the talk“Application architecture with Sling Models”
Resources
adaptTo() 2014 31
Sling Models Documentationhttp://sling.apache.org/documentation/bundles/models.html
Sling Models 1.0.x Introduction by Justin Edelsonhttp://slideshare.net/justinedelson/sling-models-overview
Sling Models Content Packageshttps://github.com/Adobe-Consulting-Services/com.adobe.acs.bundles.sling-models/releases(Drop-in replacement in existing AEM6 instances, runs in CQ56 as well)
AEM Object Injector Implementationshttp://wcm.io/sling/models/http://adobe-consulting-services.github.io/acs-aem-commons/features/aem-sling-models-injectors.html
Application architecture with Sling Modelshttp://adapt.to/2014/en/schedule/sling-application-architecture-with-sling-models.html