SlideShare a Scribd company logo
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!

More Related Content

PDF
Runmodes and Configs for Fun and Profit
ODP
Declarative Services Dependency Injection OSGi style
ODP
Unit Testing and Coverage for AngularJS
PDF
AngularJS Unit Test
PPTX
AngularJS Unit Testing
ODP
Jquery- One slide completing all JQuery
PDF
Karma - JS Test Runner
PDF
Angularjs - Unit testing introduction
Runmodes and Configs for Fun and Profit
Declarative Services Dependency Injection OSGi style
Unit Testing and Coverage for AngularJS
AngularJS Unit Test
AngularJS Unit Testing
Jquery- One slide completing all JQuery
Karma - JS Test Runner
Angularjs - Unit testing introduction

What's hot (20)

PDF
Intro to testing Javascript with jasmine
PDF
AngularJS Unit Testing w/Karma and Jasmine
PDF
Unit Testing Express Middleware
PPTX
Unit testing in JavaScript with Jasmine and Karma
PDF
Quick tour to front end unit testing using jasmine
PPTX
Unit testing of java script and angularjs application using Karma Jasmine Fra...
PPTX
A Tour of PostgREST
PDF
JavaScript Test-Driven Development with Jasmine 2.0 and Karma
PDF
Unit Testing Express and Koa Middleware in ES2015
PDF
Angular testing
PDF
using Mithril.js + postgREST to build and consume API's
PPTX
Unit testing JavaScript: Jasmine & karma intro
PDF
JavaScript TDD with Jasmine and Karma
PPTX
Full Stack Unit Testing
PDF
Painless JavaScript Testing with Jest
PPTX
SenchaCon 2016: Learn the Top 10 Best ES2015 Features - Lee Boonstra
ODP
Angular JS Unit Testing - Overview
PPTX
PostgREST Design Philosophy
PDF
Advanced Jasmine - Front-End JavaScript Unit Testing
PDF
Test-Driven Development of AngularJS Applications
Intro to testing Javascript with jasmine
AngularJS Unit Testing w/Karma and Jasmine
Unit Testing Express Middleware
Unit testing in JavaScript with Jasmine and Karma
Quick tour to front end unit testing using jasmine
Unit testing of java script and angularjs application using Karma Jasmine Fra...
A Tour of PostgREST
JavaScript Test-Driven Development with Jasmine 2.0 and Karma
Unit Testing Express and Koa Middleware in ES2015
Angular testing
using Mithril.js + postgREST to build and consume API's
Unit testing JavaScript: Jasmine & karma intro
JavaScript TDD with Jasmine and Karma
Full Stack Unit Testing
Painless JavaScript Testing with Jest
SenchaCon 2016: Learn the Top 10 Best ES2015 Features - Lee Boonstra
Angular JS Unit Testing - Overview
PostgREST Design Philosophy
Advanced Jasmine - Front-End JavaScript Unit Testing
Test-Driven Development of AngularJS Applications
Ad

Similar to SCR Annotations for Fun and Profit (20)

PDF
adaptTo2018-LightningTalk-New-Sling-Servlet-Annotations-Konrad-Windszus.pdf
PDF
five Sling features you should know
PPTX
Integration patterns in AEM 6
DOC
Sel study notes
PDF
Declaring Server App Components in Pure Java
PDF
Extending spring
PDF
Java onebrazil hk2
PPT
Spring 3.1: a Walking Tour
PDF
Spring 3 - Der dritte Frühling
PDF
Boston 2011 OTN Developer Days - Java EE 6
PDF
What's New in Spring 3.1
PDF
PDF
Java EE 6 = Less Code + More Power
PDF
Java EE 6 & GlassFish = Less Code + More Power @ DevIgnition
PDF
Java EE 6 & GlassFish = Less Code + More Power at CEJUG
PDF
Spring 3 - An Introduction
PDF
Java EE 6 & GlassFish v3: Paving the path for the future - Spark IT 2010
PDF
2012 04-06-v2-tdp-1163-java e-evsspringshootout-final
PPTX
Spring 3.1 to 3.2 in a Nutshell - Spring I/O 2012
PDF
Extending Spring for Custom Usage
adaptTo2018-LightningTalk-New-Sling-Servlet-Annotations-Konrad-Windszus.pdf
five Sling features you should know
Integration patterns in AEM 6
Sel study notes
Declaring Server App Components in Pure Java
Extending spring
Java onebrazil hk2
Spring 3.1: a Walking Tour
Spring 3 - Der dritte Frühling
Boston 2011 OTN Developer Days - Java EE 6
What's New in Spring 3.1
Java EE 6 = Less Code + More Power
Java EE 6 & GlassFish = Less Code + More Power @ DevIgnition
Java EE 6 & GlassFish = Less Code + More Power at CEJUG
Spring 3 - An Introduction
Java EE 6 & GlassFish v3: Paving the path for the future - Spark IT 2010
2012 04-06-v2-tdp-1163-java e-evsspringshootout-final
Spring 3.1 to 3.2 in a Nutshell - Spring I/O 2012
Extending Spring for Custom Usage
Ad

Recently uploaded (20)

PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PPT
Teaching material agriculture food technology
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PPTX
breach-and-attack-simulation-cybersecurity-india-chennai-defenderrabbit-2025....
PDF
KodekX | Application Modernization Development
PDF
[발표본] 너의 과제는 클라우드에 있어_KTDS_김동현_20250524.pdf
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Machine learning based COVID-19 study performance prediction
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
solutions_manual_-_materials___processing_in_manufacturing__demargo_.pdf
PDF
Modernizing your data center with Dell and AMD
PPTX
Cloud computing and distributed systems.
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Electronic commerce courselecture one. Pdf
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
Spectral efficient network and resource selection model in 5G networks
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Teaching material agriculture food technology
“AI and Expert System Decision Support & Business Intelligence Systems”
Per capita expenditure prediction using model stacking based on satellite ima...
breach-and-attack-simulation-cybersecurity-india-chennai-defenderrabbit-2025....
KodekX | Application Modernization Development
[발표본] 너의 과제는 클라우드에 있어_KTDS_김동현_20250524.pdf
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Machine learning based COVID-19 study performance prediction
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
The Rise and Fall of 3GPP – Time for a Sabbatical?
solutions_manual_-_materials___processing_in_manufacturing__demargo_.pdf
Modernizing your data center with Dell and AMD
Cloud computing and distributed systems.
Mobile App Security Testing_ A Comprehensive Guide.pdf
The AUB Centre for AI in Media Proposal.docx
Electronic commerce courselecture one. Pdf
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Spectral efficient network and resource selection model in 5G networks

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