What’s new in Sling Models 1 · APACHE SLING & FRIENDS TECH MEETUP BERLIN, 22-24 SEPTEMBER 2014...

31
APACHE SLING & FRIENDS TECH MEETUP BERLIN, 22-24 SEPTEMBER 2014 What’s new in Sling Models 1.1 Stefan Seifert, pro!vision GmbH

Transcript of What’s new in Sling Models 1 · APACHE SLING & FRIENDS TECH MEETUP BERLIN, 22-24 SEPTEMBER 2014...

Page 1: What’s new in Sling Models 1 · APACHE SLING & FRIENDS TECH MEETUP BERLIN, 22-24 SEPTEMBER 2014 What’s new in Sling Models 1.1 Stefan Seifert, pro!vision GmbH

APACHE SLING & FRIENDS TECH MEETUPBERLIN, 22-24 SEPTEMBER 2014

What’s new in Sling Models 1.1Stefan Seifert, pro!vision GmbH

Page 2: What’s new in Sling Models 1 · APACHE SLING & FRIENDS TECH MEETUP BERLIN, 22-24 SEPTEMBER 2014 What’s new in Sling Models 1.1 Stefan 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

Page 3: What’s new in Sling Models 1 · APACHE SLING & FRIENDS TECH MEETUP BERLIN, 22-24 SEPTEMBER 2014 What’s new in Sling Models 1.1 Stefan Seifert, pro!vision GmbH

Overview

adaptTo() 2014 3

Short Recap on Sling Models 1.0.x Injector-specific Annotations Constructor Injection New Injectors Alternate Adapter Interfaces

Page 4: What’s new in Sling Models 1 · APACHE SLING & FRIENDS TECH MEETUP BERLIN, 22-24 SEPTEMBER 2014 What’s new in Sling Models 1.1 Stefan Seifert, pro!vision GmbH

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

Page 5: What’s new in Sling Models 1 · APACHE SLING & FRIENDS TECH MEETUP BERLIN, 22-24 SEPTEMBER 2014 What’s new in Sling Models 1.1 Stefan Seifert, pro!vision GmbH

Short Recap on Sling Models 1.0.x

Page 6: What’s new in Sling Models 1 · APACHE SLING & FRIENDS TECH MEETUP BERLIN, 22-24 SEPTEMBER 2014 What’s new in Sling Models 1.1 Stefan Seifert, pro!vision GmbH

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);

Page 7: What’s new in Sling Models 1 · APACHE SLING & FRIENDS TECH MEETUP BERLIN, 22-24 SEPTEMBER 2014 What’s new in Sling Models 1.1 Stefan Seifert, pro!vision GmbH

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);

Page 8: What’s new in Sling Models 1 · APACHE SLING & FRIENDS TECH MEETUP BERLIN, 22-24 SEPTEMBER 2014 What’s new in Sling Models 1.1 Stefan Seifert, pro!vision GmbH

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

Page 9: What’s new in Sling Models 1 · APACHE SLING & FRIENDS TECH MEETUP BERLIN, 22-24 SEPTEMBER 2014 What’s new in Sling Models 1.1 Stefan Seifert, pro!vision GmbH

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

Page 10: What’s new in Sling Models 1 · APACHE SLING & FRIENDS TECH MEETUP BERLIN, 22-24 SEPTEMBER 2014 What’s new in Sling Models 1.1 Stefan Seifert, pro!vision GmbH

Learn more about the basics

adaptTo() 2014 10

Presentation by Justin Edelsonhttp://slideshare.net/justinedelson/sling-models-overview

Page 11: What’s new in Sling Models 1 · APACHE SLING & FRIENDS TECH MEETUP BERLIN, 22-24 SEPTEMBER 2014 What’s new in Sling Models 1.1 Stefan Seifert, pro!vision GmbH

Injector-specific Annotations

Page 12: What’s new in Sling Models 1 · APACHE SLING & FRIENDS TECH MEETUP BERLIN, 22-24 SEPTEMBER 2014 What’s new in Sling Models 1.1 Stefan Seifert, pro!vision GmbH

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

Page 13: What’s new in Sling Models 1 · APACHE SLING & FRIENDS TECH MEETUP BERLIN, 22-24 SEPTEMBER 2014 What’s new in Sling Models 1.1 Stefan Seifert, pro!vision GmbH

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;

Page 14: What’s new in Sling Models 1 · APACHE SLING & FRIENDS TECH MEETUP BERLIN, 22-24 SEPTEMBER 2014 What’s new in Sling Models 1.1 Stefan Seifert, pro!vision GmbH

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;

Page 15: What’s new in Sling Models 1 · APACHE SLING & FRIENDS TECH MEETUP BERLIN, 22-24 SEPTEMBER 2014 What’s new in Sling Models 1.1 Stefan Seifert, pro!vision GmbH

Constructor Injection

Page 16: What’s new in Sling Models 1 · APACHE SLING & FRIENDS TECH MEETUP BERLIN, 22-24 SEPTEMBER 2014 What’s new in Sling Models 1.1 Stefan Seifert, pro!vision GmbH

Constructor Injection (since 1.1.0)

adaptTo() 2014 16

Adds support for Constructor Injection in POJOs

Supports same features and annotations as field injection

Page 17: What’s new in Sling Models 1 · APACHE SLING & FRIENDS TECH MEETUP BERLIN, 22-24 SEPTEMBER 2014 What’s new in Sling Models 1.1 Stefan Seifert, pro!vision GmbH

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

}

}

Page 18: What’s new in Sling Models 1 · APACHE SLING & FRIENDS TECH MEETUP BERLIN, 22-24 SEPTEMBER 2014 What’s new in Sling Models 1.1 Stefan Seifert, pro!vision GmbH

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

Page 19: What’s new in Sling Models 1 · APACHE SLING & FRIENDS TECH MEETUP BERLIN, 22-24 SEPTEMBER 2014 What’s new in Sling Models 1.1 Stefan Seifert, pro!vision GmbH

New Injectors

Page 20: What’s new in Sling Models 1 · APACHE SLING & FRIENDS TECH MEETUP BERLIN, 22-24 SEPTEMBER 2014 What’s new in Sling Models 1.1 Stefan Seifert, pro!vision GmbH

New Injectors in 1.1.0

adaptTo() 2014 20

Self Injector Sling Object Injector Resource Path Injector

Page 21: What’s new in Sling Models 1 · APACHE SLING & FRIENDS TECH MEETUP BERLIN, 22-24 SEPTEMBER 2014 What’s new in Sling Models 1.1 Stefan Seifert, pro!vision GmbH

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

Page 22: What’s new in Sling Models 1 · APACHE SLING & FRIENDS TECH MEETUP BERLIN, 22-24 SEPTEMBER 2014 What’s new in Sling Models 1.1 Stefan Seifert, pro!vision GmbH

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;

}

Page 23: What’s new in Sling Models 1 · APACHE SLING & FRIENDS TECH MEETUP BERLIN, 22-24 SEPTEMBER 2014 What’s new in Sling Models 1.1 Stefan Seifert, pro!vision GmbH

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

Page 24: What’s new in Sling Models 1 · APACHE SLING & FRIENDS TECH MEETUP BERLIN, 22-24 SEPTEMBER 2014 What’s new in Sling Models 1.1 Stefan Seifert, pro!vision GmbH

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;

}

Page 25: What’s new in Sling Models 1 · APACHE SLING & FRIENDS TECH MEETUP BERLIN, 22-24 SEPTEMBER 2014 What’s new in Sling Models 1.1 Stefan Seifert, pro!vision GmbH

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/

Page 26: What’s new in Sling Models 1 · APACHE SLING & FRIENDS TECH MEETUP BERLIN, 22-24 SEPTEMBER 2014 What’s new in Sling Models 1.1 Stefan Seifert, pro!vision GmbH

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;

}

Page 27: What’s new in Sling Models 1 · APACHE SLING & FRIENDS TECH MEETUP BERLIN, 22-24 SEPTEMBER 2014 What’s new in Sling Models 1.1 Stefan Seifert, pro!vision GmbH

Alternate Adapter Interfaces

Page 28: What’s new in Sling Models 1 · APACHE SLING & FRIENDS TECH MEETUP BERLIN, 22-24 SEPTEMBER 2014 What’s new in Sling Models 1.1 Stefan Seifert, pro!vision GmbH

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

Page 29: What’s new in Sling Models 1 · APACHE SLING & FRIENDS TECH MEETUP BERLIN, 22-24 SEPTEMBER 2014 What’s new in Sling Models 1.1 Stefan Seifert, pro!vision GmbH

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

}

}

Page 30: What’s new in Sling Models 1 · APACHE SLING & FRIENDS TECH MEETUP BERLIN, 22-24 SEPTEMBER 2014 What’s new in Sling Models 1.1 Stefan Seifert, pro!vision GmbH

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”

Page 31: What’s new in Sling Models 1 · APACHE SLING & FRIENDS TECH MEETUP BERLIN, 22-24 SEPTEMBER 2014 What’s new in Sling Models 1.1 Stefan Seifert, pro!vision GmbH

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