SCR Annotations for Fun and Profit

M
Mike PfaffIndependent Consultant at MP Productions
SCR Annotations
    for Fun and Profit
Lightningtalks.adaptTo(Berlin, 2012)

               Mike Pfaff
        Freelance CQ Consultant

         mikepfaff83@gmail.com
Huh?

Short intro on how to use SCR annotations to
easily define servlets and services, use
references to access services, define
configurable components, etc.

Show ease of “new style” annotations
compared to “old style” JavaDoc comments
How to: Use annotations

...
<dependencies>
    ...
    <dependency>
        <groupId>org.apache.felix</groupId>
        <artifactId>org.apache.felix.scr.annotations</artifactId>
        <version>1.7.0</version>
        <scope>provided</scope>
    </dependency>
    ...
</dependencies>
...
How to: Maven plugin

...
<build>
    <plugins>
         <plugin>
             <groupId>org.apache.felix</groupId>
             <artifactId>maven-scr-plugin</artifactId>
             <version>1.8.0</version>
         </plugin>
         ...
    </plugins>
</build>
...
Reference: Old Style


...

/**
* @scr.reference
*/
private SlingRepository repo;

...
Reference: New Style


...

@Reference
private SlingRepository repo;

...
Servlet: Old Style

/**
* Foo Bar Servlet
*
* @scr.component metatype="false"
* @scr.service interface="javax.servlet.Servlet"
* @scr.property name="sling.servlet.methods" value="POST"
* @scr.property name="sling.servlet.selectors" value="foobar"
* @scr.property name="sling.servlet.resourceTypes" value="adaptto2012/
components/page"
**/
public class FooBarServlet extends SlingAllMethodsServlet {

    ...

}
Servlet: New Style

/**
* Foo Bar Servlet
**/

@SlingServlet(
        resourceTypes = {"adaptto2012/components/page"},
        methods       = {"POST"},
        selectors     = {"foobar"}
)
public class FooBarServlet extends SlingAllMethodsServlet {

    ...

}
SlingServlet options

Based on resourceType (recommended!)
  resourceTypes - Required
  selectors - Optional
  extensions - Optional
  methods - Optional
Based on path (caution - no access control!)
  paths - Required
Configurable Component:
        Define
@Component(
    label = "adaptTo() - Sample Configurable Component",
    description = "This is a sample for a configurable component",
    metatype = true
)
public class SampleConfigurableComponent {
    @Property(
        label = "Hostname",
        description = "Hostname of some external service"
    )
    static final String PROP_HOSTNAME = "hostname";
}
Configurable Component:
      Configure
Configuration: Retrieve


protected void activate(ComponentContext context) {
     Dictionary configProperties = context.getProperties();
     String hostName = (String)configProperties.get(PROP_HOSTNAME);
}
Other annotations

Define a service (which you can then @Reference)
    @Service



Sling filters:
    @SlingFilter
    @SlingFilterScope


Override default method names:
     @Activate
     @Deactivate
     @Modified
Some best practices

Only set metatype to true if you want your
component to be configurable through the
Apache Felix web console. Defaults are false
for annotations (so better than JavaDoc tags)

Always set nice labels and descriptions to
avoid cryptic entries in the Apache Felix web
console (also applies to non configurable
components!)
Documentation
SCR Annotations
http://felix.apache.org/site/scr-
annotations.html

Sling SCR Annotations
http://felix.apache.org/site/extending-scr-
annotations.html

Maven Plugin
http://felix.apache.org/site/apache-felix-
maven-scr-plugin.html
Thank you!
1 of 16

Recommended

Runmodes and Configs for Fun and Profit by
Runmodes and Configs for Fun and ProfitRunmodes and Configs for Fun and Profit
Runmodes and Configs for Fun and ProfitMike Pfaff
1.9K views14 slides
Declarative Services Dependency Injection OSGi style by
Declarative Services Dependency Injection OSGi styleDeclarative Services Dependency Injection OSGi style
Declarative Services Dependency Injection OSGi styleFelix Meschberger
5.9K views33 slides
Unit Testing and Coverage for AngularJS by
Unit Testing and Coverage for AngularJSUnit Testing and Coverage for AngularJS
Unit Testing and Coverage for AngularJSKnoldus Inc.
4.8K views22 slides
AngularJS Unit Test by
AngularJS Unit TestAngularJS Unit Test
AngularJS Unit TestChiew Carol
871 views47 slides
AngularJS Unit Testing by
AngularJS Unit TestingAngularJS Unit Testing
AngularJS Unit TestingPrince Norin
466 views31 slides
Jquery- One slide completing all JQuery by
Jquery- One slide completing all JQueryJquery- One slide completing all JQuery
Jquery- One slide completing all JQueryKnoldus Inc.
3.1K views29 slides

More Related Content

What's hot

Intro to testing Javascript with jasmine by
Intro to testing Javascript with jasmineIntro to testing Javascript with jasmine
Intro to testing Javascript with jasmineTimothy Oxley
9.4K views15 slides
AngularJS Unit Testing w/Karma and Jasmine by
AngularJS Unit Testing w/Karma and JasmineAngularJS Unit Testing w/Karma and Jasmine
AngularJS Unit Testing w/Karma and Jasminefoxp2code
1.7K views18 slides
Unit Testing Express Middleware by
Unit Testing Express MiddlewareUnit Testing Express Middleware
Unit Testing Express MiddlewareMorris Singer
22.1K views58 slides
Unit testing in JavaScript with Jasmine and Karma by
Unit testing in JavaScript with Jasmine and KarmaUnit testing in JavaScript with Jasmine and Karma
Unit testing in JavaScript with Jasmine and KarmaAndrey Kolodnitsky
3.3K views22 slides
Quick tour to front end unit testing using jasmine by
Quick tour to front end unit testing using jasmineQuick tour to front end unit testing using jasmine
Quick tour to front end unit testing using jasmineGil Fink
3K views31 slides
Unit testing of java script and angularjs application using Karma Jasmine Fra... by
Unit testing of java script and angularjs application using Karma Jasmine Fra...Unit testing of java script and angularjs application using Karma Jasmine Fra...
Unit testing of java script and angularjs application using Karma Jasmine Fra...Samyak Bhalerao
1.1K views17 slides

What's hot(20)

Intro to testing Javascript with jasmine by Timothy Oxley
Intro to testing Javascript with jasmineIntro to testing Javascript with jasmine
Intro to testing Javascript with jasmine
Timothy Oxley9.4K views
AngularJS Unit Testing w/Karma and Jasmine by foxp2code
AngularJS Unit Testing w/Karma and JasmineAngularJS Unit Testing w/Karma and Jasmine
AngularJS Unit Testing w/Karma and Jasmine
foxp2code1.7K views
Unit Testing Express Middleware by Morris Singer
Unit Testing Express MiddlewareUnit Testing Express Middleware
Unit Testing Express Middleware
Morris Singer22.1K views
Unit testing in JavaScript with Jasmine and Karma by Andrey Kolodnitsky
Unit testing in JavaScript with Jasmine and KarmaUnit testing in JavaScript with Jasmine and Karma
Unit testing in JavaScript with Jasmine and Karma
Andrey Kolodnitsky3.3K views
Quick tour to front end unit testing using jasmine by Gil Fink
Quick tour to front end unit testing using jasmineQuick tour to front end unit testing using jasmine
Quick tour to front end unit testing using jasmine
Gil Fink3K views
Unit testing of java script and angularjs application using Karma Jasmine Fra... by Samyak Bhalerao
Unit testing of java script and angularjs application using Karma Jasmine Fra...Unit testing of java script and angularjs application using Karma Jasmine Fra...
Unit testing of java script and angularjs application using Karma Jasmine Fra...
Samyak Bhalerao1.1K views
A Tour of PostgREST by begriffs
A Tour of PostgRESTA Tour of PostgREST
A Tour of PostgREST
begriffs11.4K views
JavaScript Test-Driven Development with Jasmine 2.0 and Karma by Christopher Bartling
JavaScript Test-Driven Development with Jasmine 2.0 and Karma JavaScript Test-Driven Development with Jasmine 2.0 and Karma
JavaScript Test-Driven Development with Jasmine 2.0 and Karma
Unit Testing Express and Koa Middleware in ES2015 by Morris Singer
Unit Testing Express and Koa Middleware in ES2015Unit Testing Express and Koa Middleware in ES2015
Unit Testing Express and Koa Middleware in ES2015
Morris Singer5.2K views
SenchaCon 2016: Learn the Top 10 Best ES2015 Features - Lee Boonstra by Sencha
SenchaCon 2016: Learn the Top 10 Best ES2015 Features - Lee Boonstra  SenchaCon 2016: Learn the Top 10 Best ES2015 Features - Lee Boonstra
SenchaCon 2016: Learn the Top 10 Best ES2015 Features - Lee Boonstra
Sencha420 views
PostgREST Design Philosophy by begriffs
PostgREST Design PhilosophyPostgREST Design Philosophy
PostgREST Design Philosophy
begriffs13.3K views
Advanced Jasmine - Front-End JavaScript Unit Testing by Lars Thorup
Advanced Jasmine - Front-End JavaScript Unit TestingAdvanced Jasmine - Front-End JavaScript Unit Testing
Advanced Jasmine - Front-End JavaScript Unit Testing
Lars Thorup24.2K views
Test-Driven Development of AngularJS Applications by FITC
Test-Driven Development of AngularJS ApplicationsTest-Driven Development of AngularJS Applications
Test-Driven Development of AngularJS Applications
FITC9.3K views

Similar to SCR Annotations for Fun and Profit

Declarative Services - Dependency Injection OSGi Style by
Declarative Services - Dependency Injection OSGi StyleDeclarative Services - Dependency Injection OSGi Style
Declarative Services - Dependency Injection OSGi StyleFelix Meschberger
14 views36 slides
Declarative Services - Dependency Injection OSGi Style by
Declarative Services - Dependency Injection OSGi StyleDeclarative Services - Dependency Injection OSGi Style
Declarative Services - Dependency Injection OSGi StyleFelix Meschberger
2.1K views36 slides
Scala at Netflix by
Scala at NetflixScala at Netflix
Scala at NetflixManish Pandit
8.8K views52 slides
Servlets 3.0 - Asynchronous, Extensibility, Ease-of-use @ JavaOne Brazil 2010 by
Servlets 3.0 - Asynchronous, Extensibility, Ease-of-use @ JavaOne Brazil 2010Servlets 3.0 - Asynchronous, Extensibility, Ease-of-use @ JavaOne Brazil 2010
Servlets 3.0 - Asynchronous, Extensibility, Ease-of-use @ JavaOne Brazil 2010Arun Gupta
5.3K views34 slides
Field injection, type safe configuration, and more new goodies in Declarative... by
Field injection, type safe configuration, and more new goodies in Declarative...Field injection, type safe configuration, and more new goodies in Declarative...
Field injection, type safe configuration, and more new goodies in Declarative...bjhargrave
1.8K views26 slides
Intro to Laravel 4 by
Intro to Laravel 4Intro to Laravel 4
Intro to Laravel 4Singapore PHP User Group
5K views38 slides

Similar to SCR Annotations for Fun and Profit(20)

Declarative Services - Dependency Injection OSGi Style by Felix Meschberger
Declarative Services - Dependency Injection OSGi StyleDeclarative Services - Dependency Injection OSGi Style
Declarative Services - Dependency Injection OSGi Style
Declarative Services - Dependency Injection OSGi Style by Felix Meschberger
Declarative Services - Dependency Injection OSGi StyleDeclarative Services - Dependency Injection OSGi Style
Declarative Services - Dependency Injection OSGi Style
Felix Meschberger2.1K views
Servlets 3.0 - Asynchronous, Extensibility, Ease-of-use @ JavaOne Brazil 2010 by Arun Gupta
Servlets 3.0 - Asynchronous, Extensibility, Ease-of-use @ JavaOne Brazil 2010Servlets 3.0 - Asynchronous, Extensibility, Ease-of-use @ JavaOne Brazil 2010
Servlets 3.0 - Asynchronous, Extensibility, Ease-of-use @ JavaOne Brazil 2010
Arun Gupta5.3K views
Field injection, type safe configuration, and more new goodies in Declarative... by bjhargrave
Field injection, type safe configuration, and more new goodies in Declarative...Field injection, type safe configuration, and more new goodies in Declarative...
Field injection, type safe configuration, and more new goodies in Declarative...
bjhargrave1.8K views
Bt0083 server side programing 2 by Techglyphs
Bt0083 server side programing  2Bt0083 server side programing  2
Bt0083 server side programing 2
Techglyphs106 views
dokumen.tips_rediscovering-spring-with-spring-boot1 (1).pdf by Appster1
dokumen.tips_rediscovering-spring-with-spring-boot1 (1).pdfdokumen.tips_rediscovering-spring-with-spring-boot1 (1).pdf
dokumen.tips_rediscovering-spring-with-spring-boot1 (1).pdf
Appster13 views
dokumen.tips_rediscovering-spring-with-spring-boot1.pdf by Appster1
dokumen.tips_rediscovering-spring-with-spring-boot1.pdfdokumen.tips_rediscovering-spring-with-spring-boot1.pdf
dokumen.tips_rediscovering-spring-with-spring-boot1.pdf
Appster12 views
JavaOne India 2011 - Servlets 3.0 by Arun Gupta
JavaOne India 2011 - Servlets 3.0JavaOne India 2011 - Servlets 3.0
JavaOne India 2011 - Servlets 3.0
Arun Gupta829 views
AEM Sightly Deep Dive by Gabriel Walt
AEM Sightly Deep DiveAEM Sightly Deep Dive
AEM Sightly Deep Dive
Gabriel Walt4.1K views
Asp.Net Ajax Component Development by Chui-Wen Chiu
Asp.Net Ajax Component DevelopmentAsp.Net Ajax Component Development
Asp.Net Ajax Component Development
Chui-Wen Chiu1.2K views
Developing RESTful WebServices using Jersey by b_kathir
Developing RESTful WebServices using JerseyDeveloping RESTful WebServices using Jersey
Developing RESTful WebServices using Jersey
b_kathir3.1K views
Xitrum Web Framework Live Coding Demos / Xitrum Web Framework ライブコーディング by scalaconfjp
Xitrum Web Framework Live Coding Demos / Xitrum Web Framework ライブコーディングXitrum Web Framework Live Coding Demos / Xitrum Web Framework ライブコーディング
Xitrum Web Framework Live Coding Demos / Xitrum Web Framework ライブコーディング
scalaconfjp2.9K views
Xitrum @ Scala Matsuri Tokyo 2014 by Ngoc Dao
Xitrum @ Scala Matsuri Tokyo 2014Xitrum @ Scala Matsuri Tokyo 2014
Xitrum @ Scala Matsuri Tokyo 2014
Ngoc Dao44.5K views
Salesforce Lightning Web Components Overview by Nagarjuna Kaipu
Salesforce Lightning Web Components OverviewSalesforce Lightning Web Components Overview
Salesforce Lightning Web Components Overview
Nagarjuna Kaipu84 views

Recently uploaded

Migrating VMware Infra to KVM Using CloudStack - Nicolas Vazquez - ShapeBlue by
Migrating VMware Infra to KVM Using CloudStack - Nicolas Vazquez - ShapeBlueMigrating VMware Infra to KVM Using CloudStack - Nicolas Vazquez - ShapeBlue
Migrating VMware Infra to KVM Using CloudStack - Nicolas Vazquez - ShapeBlueShapeBlue
147 views20 slides
Developments to CloudStack’s SDN ecosystem: Integration with VMWare NSX 4 - P... by
Developments to CloudStack’s SDN ecosystem: Integration with VMWare NSX 4 - P...Developments to CloudStack’s SDN ecosystem: Integration with VMWare NSX 4 - P...
Developments to CloudStack’s SDN ecosystem: Integration with VMWare NSX 4 - P...ShapeBlue
120 views62 slides
The Power of Heat Decarbonisation Plans in the Built Environment by
The Power of Heat Decarbonisation Plans in the Built EnvironmentThe Power of Heat Decarbonisation Plans in the Built Environment
The Power of Heat Decarbonisation Plans in the Built EnvironmentIES VE
67 views20 slides
Import Export Virtual Machine for KVM Hypervisor - Ayush Pandey - University ... by
Import Export Virtual Machine for KVM Hypervisor - Ayush Pandey - University ...Import Export Virtual Machine for KVM Hypervisor - Ayush Pandey - University ...
Import Export Virtual Machine for KVM Hypervisor - Ayush Pandey - University ...ShapeBlue
48 views17 slides
Microsoft Power Platform.pptx by
Microsoft Power Platform.pptxMicrosoft Power Platform.pptx
Microsoft Power Platform.pptxUni Systems S.M.S.A.
74 views38 slides
KVM Security Groups Under the Hood - Wido den Hollander - Your.Online by
KVM Security Groups Under the Hood - Wido den Hollander - Your.OnlineKVM Security Groups Under the Hood - Wido den Hollander - Your.Online
KVM Security Groups Under the Hood - Wido den Hollander - Your.OnlineShapeBlue
154 views19 slides

Recently uploaded(20)

Migrating VMware Infra to KVM Using CloudStack - Nicolas Vazquez - ShapeBlue by ShapeBlue
Migrating VMware Infra to KVM Using CloudStack - Nicolas Vazquez - ShapeBlueMigrating VMware Infra to KVM Using CloudStack - Nicolas Vazquez - ShapeBlue
Migrating VMware Infra to KVM Using CloudStack - Nicolas Vazquez - ShapeBlue
ShapeBlue147 views
Developments to CloudStack’s SDN ecosystem: Integration with VMWare NSX 4 - P... by ShapeBlue
Developments to CloudStack’s SDN ecosystem: Integration with VMWare NSX 4 - P...Developments to CloudStack’s SDN ecosystem: Integration with VMWare NSX 4 - P...
Developments to CloudStack’s SDN ecosystem: Integration with VMWare NSX 4 - P...
ShapeBlue120 views
The Power of Heat Decarbonisation Plans in the Built Environment by IES VE
The Power of Heat Decarbonisation Plans in the Built EnvironmentThe Power of Heat Decarbonisation Plans in the Built Environment
The Power of Heat Decarbonisation Plans in the Built Environment
IES VE67 views
Import Export Virtual Machine for KVM Hypervisor - Ayush Pandey - University ... by ShapeBlue
Import Export Virtual Machine for KVM Hypervisor - Ayush Pandey - University ...Import Export Virtual Machine for KVM Hypervisor - Ayush Pandey - University ...
Import Export Virtual Machine for KVM Hypervisor - Ayush Pandey - University ...
ShapeBlue48 views
KVM Security Groups Under the Hood - Wido den Hollander - Your.Online by ShapeBlue
KVM Security Groups Under the Hood - Wido den Hollander - Your.OnlineKVM Security Groups Under the Hood - Wido den Hollander - Your.Online
KVM Security Groups Under the Hood - Wido den Hollander - Your.Online
ShapeBlue154 views
Updates on the LINSTOR Driver for CloudStack - Rene Peinthor - LINBIT by ShapeBlue
Updates on the LINSTOR Driver for CloudStack - Rene Peinthor - LINBITUpdates on the LINSTOR Driver for CloudStack - Rene Peinthor - LINBIT
Updates on the LINSTOR Driver for CloudStack - Rene Peinthor - LINBIT
ShapeBlue138 views
Live Demo Showcase: Unveiling Dell PowerFlex’s IaaS Capabilities with Apache ... by ShapeBlue
Live Demo Showcase: Unveiling Dell PowerFlex’s IaaS Capabilities with Apache ...Live Demo Showcase: Unveiling Dell PowerFlex’s IaaS Capabilities with Apache ...
Live Demo Showcase: Unveiling Dell PowerFlex’s IaaS Capabilities with Apache ...
ShapeBlue52 views
Zero to Cloud Hero: Crafting a Private Cloud from Scratch with XCP-ng, Xen Or... by ShapeBlue
Zero to Cloud Hero: Crafting a Private Cloud from Scratch with XCP-ng, Xen Or...Zero to Cloud Hero: Crafting a Private Cloud from Scratch with XCP-ng, Xen Or...
Zero to Cloud Hero: Crafting a Private Cloud from Scratch with XCP-ng, Xen Or...
ShapeBlue128 views
Transitioning from VMware vCloud to Apache CloudStack: A Path to Profitabilit... by ShapeBlue
Transitioning from VMware vCloud to Apache CloudStack: A Path to Profitabilit...Transitioning from VMware vCloud to Apache CloudStack: A Path to Profitabilit...
Transitioning from VMware vCloud to Apache CloudStack: A Path to Profitabilit...
ShapeBlue86 views
GDG Cloud Southlake 28 Brad Taylor and Shawn Augenstein Old Problems in the N... by James Anderson
GDG Cloud Southlake 28 Brad Taylor and Shawn Augenstein Old Problems in the N...GDG Cloud Southlake 28 Brad Taylor and Shawn Augenstein Old Problems in the N...
GDG Cloud Southlake 28 Brad Taylor and Shawn Augenstein Old Problems in the N...
James Anderson142 views
Setting Up Your First CloudStack Environment with Beginners Challenges - MD R... by ShapeBlue
Setting Up Your First CloudStack Environment with Beginners Challenges - MD R...Setting Up Your First CloudStack Environment with Beginners Challenges - MD R...
Setting Up Your First CloudStack Environment with Beginners Challenges - MD R...
ShapeBlue105 views
Centralized Logging Feature in CloudStack using ELK and Grafana - Kiran Chava... by ShapeBlue
Centralized Logging Feature in CloudStack using ELK and Grafana - Kiran Chava...Centralized Logging Feature in CloudStack using ELK and Grafana - Kiran Chava...
Centralized Logging Feature in CloudStack using ELK and Grafana - Kiran Chava...
ShapeBlue74 views
Business Analyst Series 2023 - Week 4 Session 7 by DianaGray10
Business Analyst Series 2023 -  Week 4 Session 7Business Analyst Series 2023 -  Week 4 Session 7
Business Analyst Series 2023 - Week 4 Session 7
DianaGray10110 views
"Surviving highload with Node.js", Andrii Shumada by Fwdays
"Surviving highload with Node.js", Andrii Shumada "Surviving highload with Node.js", Andrii Shumada
"Surviving highload with Node.js", Andrii Shumada
Fwdays49 views
Backroll, News and Demo - Pierre Charton, Matthias Dhellin, Ousmane Diarra - ... by ShapeBlue
Backroll, News and Demo - Pierre Charton, Matthias Dhellin, Ousmane Diarra - ...Backroll, News and Demo - Pierre Charton, Matthias Dhellin, Ousmane Diarra - ...
Backroll, News and Demo - Pierre Charton, Matthias Dhellin, Ousmane Diarra - ...
ShapeBlue121 views
Elevating Privacy and Security in CloudStack - Boris Stoyanov - ShapeBlue by ShapeBlue
Elevating Privacy and Security in CloudStack - Boris Stoyanov - ShapeBlueElevating Privacy and Security in CloudStack - Boris Stoyanov - ShapeBlue
Elevating Privacy and Security in CloudStack - Boris Stoyanov - ShapeBlue
ShapeBlue149 views
DRBD Deep Dive - Philipp Reisner - LINBIT by ShapeBlue
DRBD Deep Dive - Philipp Reisner - LINBITDRBD Deep Dive - Philipp Reisner - LINBIT
DRBD Deep Dive - Philipp Reisner - LINBIT
ShapeBlue110 views

SCR Annotations for Fun and Profit

  • 1. SCR Annotations for Fun and Profit Lightningtalks.adaptTo(Berlin, 2012) Mike Pfaff Freelance CQ Consultant mikepfaff83@gmail.com
  • 2. Huh? Short intro on how to use SCR annotations to easily define servlets and services, use references to access services, define configurable components, etc. Show ease of “new style” annotations compared to “old style” JavaDoc comments
  • 3. How to: Use annotations ... <dependencies> ... <dependency> <groupId>org.apache.felix</groupId> <artifactId>org.apache.felix.scr.annotations</artifactId> <version>1.7.0</version> <scope>provided</scope> </dependency> ... </dependencies> ...
  • 4. How to: Maven plugin ... <build> <plugins> <plugin> <groupId>org.apache.felix</groupId> <artifactId>maven-scr-plugin</artifactId> <version>1.8.0</version> </plugin> ... </plugins> </build> ...
  • 5. Reference: Old Style ... /** * @scr.reference */ private SlingRepository repo; ...
  • 7. Servlet: Old Style /** * Foo Bar Servlet * * @scr.component metatype="false" * @scr.service interface="javax.servlet.Servlet" * @scr.property name="sling.servlet.methods" value="POST" * @scr.property name="sling.servlet.selectors" value="foobar" * @scr.property name="sling.servlet.resourceTypes" value="adaptto2012/ components/page" **/ public class FooBarServlet extends SlingAllMethodsServlet { ... }
  • 8. Servlet: New Style /** * Foo Bar Servlet **/ @SlingServlet( resourceTypes = {"adaptto2012/components/page"}, methods = {"POST"}, selectors = {"foobar"} ) public class FooBarServlet extends SlingAllMethodsServlet { ... }
  • 9. SlingServlet options Based on resourceType (recommended!) resourceTypes - Required selectors - Optional extensions - Optional methods - Optional Based on path (caution - no access control!) paths - Required
  • 10. Configurable Component: Define @Component( label = "adaptTo() - Sample Configurable Component", description = "This is a sample for a configurable component", metatype = true ) public class SampleConfigurableComponent { @Property( label = "Hostname", description = "Hostname of some external service" ) static final String PROP_HOSTNAME = "hostname"; }
  • 12. Configuration: Retrieve protected void activate(ComponentContext context) { Dictionary configProperties = context.getProperties(); String hostName = (String)configProperties.get(PROP_HOSTNAME); }
  • 13. Other annotations Define a service (which you can then @Reference) @Service Sling filters: @SlingFilter @SlingFilterScope Override default method names: @Activate @Deactivate @Modified
  • 14. Some best practices Only set metatype to true if you want your component to be configurable through the Apache Felix web console. Defaults are false for annotations (so better than JavaDoc tags) Always set nice labels and descriptions to avoid cryptic entries in the Apache Felix web console (also applies to non configurable components!)
  • 15. Documentation SCR Annotations http://felix.apache.org/site/scr- annotations.html Sling SCR Annotations http://felix.apache.org/site/extending-scr- annotations.html Maven Plugin http://felix.apache.org/site/apache-felix- maven-scr-plugin.html