Official OSGi Declarative Services Annotations in AEM

Adobe Experience Manager | AEM/CQ | Apache Sling

Official OSGi Declarative Services Annotations in AEM

By now you're pretty comfortable writing OSGi components and services using the Felix SCR annotations. However, with AEM 6.2 and greater comes support for the official OSGi Declarative Services annotations. This is exciting for two reasons. First, it's the official implementation; and second, it provides access to future improvements and specification advancements. The Felix Maven SCR plugin probably won't be providing much in the way of future enhancements. In fact, taken directly from the from the Apache Felix Maven SCR Plugin website:

"While the Apache Felix Maven SCR Plugin is a great tool for developing OSGi components using Declarative Services you should use the official annotations from the OSGi R6 specification. The development of the Apache Felix SCR Plugin is in maintenance mode."

The Apache Sling project and the Adobe Experience Manager product are both moving in this direction and it's suggested that you consider it for your project as well. The migration is fairly easy and both annotation styles will work side-by-side while you complete the switch-over.

If you want to skip directly to the examples, I've created a sample project with a service, servlet, filter, event handler and scheduler using the new annotation hosted on GitHub at: https://github.com/nateyolles/aem-osgi-annotation-demo.

Declarative Services

Remember that declarative services is a compile time process. In order for the DS annotations to be effective, they must be handled during the build process. The Apache Felix SCR annotations require the maven-scr-plugin while the OSGi DS annotations require the maven-bundle-plugin version 3.2.0 or greater.

With the maven-scr-plugin you may be used to finding the DS output under /target/classes/OSGI-INF and /target/classes/OSGI-INF/metatype. With the maven-bundle-plugin you will have to unzip the compiled artifact (the jar file) to find the DS output in its /OSG-INF directory.

Java Packages

Rather than using org.apache.felix.scr.annotations.*, you'll use org.osgi.service.component.annotations.* and org.osgi.service.metatype.annotations.*.

Dependencies

Rather than using the maven-scr-plugin, you need the maven-bundle-plugin version 3.2.0 or greater.

You also need the artifacts org.osgi.service.metatype.annotations and org.osgi.service.component.annotations (currently version 1.3.0) rather than org.osgi.core and org.osgi.compendium. See the provided sample project's POM file for more specifics.

Once you update your project's dependencies, you'll find that your IDE will inform you that the org.apache.felix.scr.annotations.* annotations are deprecated.

Service Configuration

The most noticeable difference between Felix SCR annotations and OSGi DS annotations is where service reference properties are defined. With Felix annotations everything is in the Properties and Property annotations either at the head of the class file or inline. OSGi DS annotations move the service reference properties to it's own class.

The annotations will move to their own class which declutters the component or service. For components with a large amount of options, you might find that you like an independent class, while a component with only one or two properties may be fine as a subclass.

You'll also immediately notice the Activate method becomes much cleaner as the need to use org.apache.sling.commons.osgi.PropertiesUtil to provide default values has been replaced.



Service Reference Properties

The AttributeDefinition method naming convention will immediately seem out of place. It's very Pythonic looking with its use of underscores. You don't need to write your method names that way, but the reason for doing so is that the underscores are converted to dots for display in the Felix console and your OSGi configs. For example, you're probably familiar with seeing properties defined as something like resource.resolver.searchpath. To achieve this in your configuration class, your method would be named resource_resolver_searchpath.

Provided is an example of how to create each individual property type with the newer OSGi annotations:





SlingServlet Annotation

The SlingServlet annotation is a special case - it's a convenience annotation and unfortunately it's not available anymore. However, the idea is that it will be available in the future as a custom annotation provided by Sling working in the OSGi DS framework when R7 is released. Your current SCR SlingServlet looks something like this (although without all the available properties set):



Instead, use a regular component annotation with service type Servlet.class. The service reference properties for paths, extensions, selectors, methods and resourceTypes are simple String properties. When setting array properties, set each entry on a new line. See the Apache Sling docs on Servlet Registration for the available service reference properties. When setting a non-String property value such as an Integer or Boolean, include the type such as service.ranking:Integer=100. Here's the OSGi version of that same servlet (again, you probably won't use all the available properties in your servlet):



Expanded Examples

View more examples in the demonstration project on GitHub: https://github.com/nateyolles/aem-osgi-annotation-demo.

Further Reading

Carsten Ziegeler's blog posts: ( @cziegeler):
Feike Visser's blog post ( @heervisscher):
Adobe Marketing Cloud:

31 comments

Roger Blanton | May 29, 2017 at 04:50 PM | Reply

Great examples and information!

Radha Krishna | May 31, 2017 at 09:52 PM | Reply

Hi Nate, Very useful information. If I make any changes to the configuration using Felix console, the changes will be stored under /apps/system/config/{PID}. 1. How do we make this run mode specific? 2. If we make any changes in felix console, do we have to commit the new change to repository?

Nikhil | June 20, 2017 at 06:10 AM | Reply

Awesome !!

Andras Fejer | July 07, 2017 at 02:35 PM | Reply

Thanks for the guide Nate! I was trying to follow the guide by Feike Visser (http://blogs.adobe.com/experiencedelivers/experience-management/osgi/using-osgi-annotations-aem6-2/), but for @Reference I'm getting a compilation failure error ("annotation type not applicable to this kind of declaration". Can you maybe provide an example for the correct usage of the @Reference annotation?

Aanchal Sikka | November 09, 2017 at 05:03 AM | Reply

Hello Andras, I had encountered a similar error. I was able to fix it, by using @Reference annotation in following manner: private transient ColorOptionsService colorOptionsService; @Reference(service = ColorOptionsService.class, cardinality = ReferenceCardinality.MANDATORY, policy = ReferencePolicy.STATIC, unbind = "unsetColorOptionsService") protected void setColorOptionsService(ColorOptionsService colorOptionsService) { this.colorOptionsService = colorOptionsService; }

Shivani Garg | July 17, 2017 at 05:43 AM | Reply

Hi Nate,Nice Article. I have just one query that in SCR annotations, We use metatype true in servlets and can all the paths,resourceType editable in configurationMgr,but now There properties are not changable at runtime.SO do we have any alternative for this as well?

Varun Rawat | November 17, 2017 at 02:45 PM | Reply

Hi, We are facing issues while writing Test Cases for a Servlet written in R6 Annotations. Could you let me know, which Test Case API i can use to write test cases.

Muzaffar Hussain | November 17, 2017 at 02:54 PM | Reply

I am unable to generate xml files inside OSG-INF. Followed above steps as mentioned. Could you help ?

Satish | January 04, 2018 at 08:22 AM | Reply

Hi Nate , MY configuration serveing is not getting active. It is in unsatifies state . Below is my code. @ObjectClassDefinition(name = "My Service Configuration", description = "Service Configuration") public @interface MyServiceConfiguration { @AttributeDefinition(name = "Config Value", description = "Configuration value") String configValue(); @AttributeDefinition(name = "MultipleValues", description = "Multi Configuration values") String[] getStringValues(); @AttributeDefinition(name = "NumberValue", description = "Number values", type=AttributeType.INTEGER) int getNumberValue(); } Below is implementation class . I do have service interface. @Component(service=MySimpleService.class,configurationPolicy=ConfigurationPolicy.REQUIRE) @Designate(ocd=MyServiceConfiguration.class) public class MySimpleServiceImpl implements MySimpleService { @Reference private MyServiceConfiguration config; @Activate public void activate(MyServiceConfiguration config){ this.config = config; } @Override public String getSimpleValue() { // TODO Auto-generated method stub return "hello"+config.configValue(); } } Can you please help me in to understand the isseu

DOWNLOAD AVAST ANTIVIRUS | April 01, 2019 at 09:46 AM | Reply

This is exclusive information, If you need any kind of help to DOWNLOAD AVAST ANTIVIRUS then you can visit our website for more information and get solution of the issues.

Alisha Ross | April 08, 2019 at 06:17 AM | Reply

For instance it adds Cash,Clams and Unlock All Characters and Outfits which are fully plain indispensable in Household Man The Quest for Stuff. Regards <a href="https://www.deepwebsiteslinks.com/empire-market-review/">Ross Alisha</a>

Auto Insurance Lawyers in Los Angeles | April 13, 2019 at 06:37 AM | Reply

Good Blogâ ¦.Thanks for sharing your informative and amazing blog with us, itâ s very helpful for everyone. If you have any kind of issues regarding this accident attorney in Los Angeles and also you can contact us with our experts. For more details: https://www.lawyersoncall.org/auto-insurance/auto-insurance-lawyers-in-los-angeles/

Microsoft contact support number | April 13, 2019 at 06:46 AM | Reply

Good Blogâ ¦.Thanks for sharing your informative and amazing blog with us, itâ s very helpful for everyone. If you have any kind of issues regarding Microsoft contact support number and contact our technical experts.

Motorcycle Accident Lawyer in New York | April 15, 2019 at 06:50 AM | Reply

Thanks for sharing this informative and valuable. Get contact for motorcycle accident lawyer in New York is here to help you if you are facing any issue regarding motorcycle accident lawyer in New York. Then feel free and get in touch with our experts via motorcycle accident lawyer in New York. For more help contact us on our website: https://www.lawyersoncall.org/motorcycle-accident/new-york-motorcycle-accident-lawyer/

How Can You Update A TomTom One XL? | April 17, 2019 at 05:15 AM | Reply

TomTom GPS helps the users in this case and to make proper use of the device, it is very necessary that the users must Update a TomTom One XL device on a regular basis. http://www.tomtommapupdate.com/update/how-can-you-update-a-tomtom-one-xl/

HP laserjet Error Code 52 | April 17, 2019 at 05:19 AM | Reply

Thank You for the magnificent post.While facing the Error Code 52 in HP Printer, visit our website and get complete steps that would help you eradicate the HP Laserjet Error 52 from your HP Printer. http://www.hphelpnumber.net/general/how-to-solve-laserjet-error-code-52-scanner-error/

Auto Insurance Lawyers in New York | April 17, 2019 at 08:44 AM | Reply

Nice blog. It's a very informative and useful blog. If you have any kind of issues regarding in the accident attorney in New York lawyers according to your case, contact with our expert advocates. https://www.lawyersoncall.org/auto-insurance/auto-insurance-lawyers-in-new-york/

AOL customer care support number | April 18, 2019 at 07:05 AM | Reply

Nice blog thank you for sharing this useful and great information. Now, if you have any kind of issues about this AOL customer care support number. So, get in touch with our experts via our website. http://www.emailcustomercares.com/aol-support.htmlaol customer care support number

Personal Injury Lawyer in Houston, TX | April 20, 2019 at 07:53 AM | Reply

Excellent and great blog thanks for giving this thing. If you are having any kinds of problems related according to claims then in that condition visit ours on website. https://www.lawyersoncall.org/personal-injury/personal-injury-lawyer-in-houston-tx/

Lesneidib | April 23, 2019 at 02:46 AM | Reply

Cialis Y Viagra Diferencias <a href=http://buycial.com>cialis 5 mg best price usa</a> Order Now Doryx Vibramycin

Accident Attorney in Houston, TX | April 27, 2019 at 05:10 AM | Reply

Nice blog. It's a very informative and useful blog. If you have any kind of issues regarding in this auto insurance lawyers in Houston, TX, and contact with our expert advocates. They will help to take the full amount of adjustment. https://www.lawyersoncall.org/auto-insurance/auto-insurance-lawyers-in-houston-tx/

TomTom Go 620 | April 29, 2019 at 06:14 AM | Reply

TomTom GO 620 helps users in this case and to make proper use of the device, it is very necessary that the users must update TomTom Go 620 Maps on regular basis. http://www.tomtommapupdate.com/update/how-can-you-update-tomtom-go-620-maps/

MAGELLAN MAPS UPDATE | May 02, 2019 at 07:12 AM | Reply

This a great post thank you for sharing good stuff with us if you need any update GPS MAGELLAN MAPS UPDATE halp for you can visit our website for the problem of the Solution. http://www.updatemygps.com/magellan-gps-update-magellan-maps-update/

Carriepi | May 02, 2019 at 07:53 PM | Reply

Carrie Underwood is my favourite country singer. She is young, beautiful and charming woman in her 30s. Her strong voice takes me away from all problems of this planet so I start enjoy my life and listen songs created by her mind. Now she is on a Cry Pretty 360 Tour started in May of 2019. The concerts scheduled for the whole 2019, up to the 31st of October. Tickets are available for all men and women with different income. If you love contry music, then you must visit at least one Carrie's concert. All tour dates are available at the <a href=https://carrieunderwoodconcerts.com>Carrie Underwood tour Cincinatti</a>. Open the website and make yourself familiar with all powerful Carrie Underwood concerts in 2019!

norton support | May 05, 2019 at 12:40 AM | Reply

Norton Anti-Virus Customer Support Number (1 (877) 839-9982) Norton Anti-Virus Customer Support Number (1 (877) 839-9982) http://nortontollfreenumbers.com/ Norton Toll-Free Number has a team of Experts To prived Norton Support for you. Norton Antivirus is the best anti-malware software which is designed and developed by Symantec Corporation since 1991. It is one of the most uses manufacturers of computer security products which protect your system from adware, viruses, malware & malicious software. Norton Antivirus software is use for protects your system from all Internet viruses, malware and Predators. http://nortontollfreenumbers.com/ Before any malware hits your device, Norton Antivirus locates and blocks it for securing your computer from any danger. Norton Anti-Virus Customer Support Number (1 (877) 839-9982) Nortonsupportnumber Nortonsupport Nortoncastumernumber Nortoncastumerphonenumber Nortontolfreenumber\ Nortonphonenumber Nortoncastumer Nortonsupportphonenumber Nortontolnumber Nortoncastumertolnumber

norton support | May 06, 2019 at 07:46 PM | Reply

Norton Anti-Virus Customer Support Number (1 (877) 839-9982 http://nortontollfreenumbers.com/ Norton Toll-Free Number has a team of Experts To prived Norton Support for you. Norton Antivirus is the best anti-malware software which is designed and developed by Symantec Corporation since 1991. It is one of the most uses manufacturers of computer security products which protect your system from adware, viruses http://nortontollfreenumbers.com/, malware & malicious software. Norton Antivirus software is use for protects your system from all Internet viruses, malware and Predators. Before any malware hits your device, Norton Antivirus locates and blocks it for securing your computer from any danger. Norton Anti-Virus Customer Support Number (1 (877) 839-9982) http://nortontollfreenumbers.com/ nortonsupport nortonsupportnumber nortoncastumernumber nortoncastumerphonenumber nortontolfreenumber nortonphonenumber nortoncastumer nortonnumnber nortonphonenumnber nortontolfreephonenumber

ChainTuddy | May 08, 2019 at 10:40 AM | Reply

The Chainsmokers are the best EDM band at the moment! You want proofs? Go listen to them! And then go to their concert! The Chainsmokers band has a lot of concerts in 2019 and 2020! Their <a href=https://chainsmokersconcerts.com>Chainsmokers tour|tour</a> is awaited very much and all concert dates has been set in stone.

yahoo support | May 11, 2019 at 11:43 PM | Reply

yahoo customer service ||yahoo support number +1 8778399982 Yahoo email Support (+1-877-839-9982) service the robust platform for emailing brings best of service experience every time for users approaching with yahoo email issues. Indisputably, at every instance we bring exceptional solution for our subscribers, among which we are definitely the best. We practice and believe in effective and complete resolution for being the best service provider as on date. At this podium we bring best in class Yahoo Phone Support for all the occurring issues within short span. You can take the help of Yahoo Support Number as and when required. The technical issues and mailing troubles can be easily taken care of by our expert support specialists. http://nortontollfreenumbers.com/yahoo-toll-free-number.html http://nortontollfreenumbers.com/yahoo-customer-support-number.html #yahoocontactnumber #yahoocustomercarenumber #yahooemailsupport #yahoocustomercare #yahoocustomersupport #yahoosupport #yahoocontactphonenumber #yahoohelp #yahoomail #yahooemail #yahoohelpnumber #yahoocustomernumber #yahooservice #yahooservicenumber #yahootollfree #Yahootollfreenumber #yahooerror #yahoocustomerservicenumber #yahooemailsupportnumber #yahoocustomersupportnumber #yahoosupporthelplinenumber #yahoomailsupportnumber #yahoomailcustomerservice #yahoonumber #yahoomailhelp #yahoosupportphonenumber

Videos upload | May 17, 2019 at 09:10 AM | Reply

Thank you for sharing this blog Get in touch for video submission sites is here to help you if you are facing any issue regarding videos upload. Then feel free and get in touch with our experts via for more help visit our website: https://www.technically-speaking.org/seo/video-submission-site-list/

MiraK | May 22, 2019 at 09:01 PM | Reply

Miranda Lambert is my favourite US singer. She is young, beautiful and charming female in her 30s. Her strong voice takes me away from all issues of this planet so I start enjoy my life and listen songs created by her voice. Now the singer is on a Roadside Bars & Pink Guitars Tour started in September of 2019 featuring Tenille Townes, Caylee Hammack and others. The concerts scheduled for this year, up to the 23th of November. Ticket prices are moderate and available for all men and women with different income. If you love country music as mush as I, then you must visit at least one of her concert. All tour dates are available at the [url=https://mirandalambertconcertdates.com]Miranda Lambert tour dates[/url]. Open the website and make yourself familiar with all Miranda Lambert concerts in 2020!

nortonsupport | June 14, 2019 at 07:52 PM | Reply

NortonAnti-Virus Customer support Number +1 800-381-8904 http://nortontollfreenumbers.com/ Norton Toll-Free Number has ateam of Experts http://nortontollfreenumbers.com/for you. Norton Antivirus is the best anti-malware software which is designed and developed by Symantec Corporation since 1991. It is one of the most uses manufacturers of computer security products which protect your system from adware, viruses, malware & malicious software. Norton Antivirus software is use for protects your system from all Internet viruses, malware and Predatorshttp://nortontollfreenumbers.commalwarehits your device, Norton Antivirus locates and blocks itfor securing yourcomputer from any danger. #nortonsupport #nortonsupportnumber #nortoncastumernumber #nortoncastumerphonenumber #nortontolfreenumber #nortonphonenumber #nortoncastumer #nortonnumnber #nortonphonenumnber

Leave a Comment