Here, I am going to explain how to add OSGI annotations to custom service, the SCR annotations are deprecated in AEM 6.3, so the people who are working on AEM 6.3 must use OSGI annotations, first you need to add below dependencies

Step 1: add dependencies

<dependency>
  <groupId>org.osgi</groupId>
  <artifactId>org.osgi.service.component.annotations</artifactId>
  <version>1.3.0</version>
</dependency>
<dependency>
  <groupId>org.osgi</groupId>
  <artifactId>org.osgi.annotation</artifactId>
  <version>6.0.0</version>
</dependency>
<dependency>
  <groupId>org.osgi</groupId>
  <artifactId>org.osgi.service.metatype.annotations</artifactId>
  <version>1.3.0</version>
</dependency>

Step 2: import packages

Once the dependencies are added run mvn eclipse:eclipse command, then import below packages in your class file

import org.osgi.service.component.annotations.*;
import org.osgi.service.metatype.annotations.*;

Step 3: create service

I have created simple FeedService interface which has only one method getFeedUrl() 

package com.aem.toolkit.service;
public interface FeedService {
	String getFeedUrl();
}

Step 4: define configuration

I am creating configuration in a separate class file which is a FeedConfiguration but, you can create an inner class for configuration

package com.aem.toolkit.service;
import org.osgi.service.metatype.annotations.AttributeDefinition;
import org.osgi.service.metatype.annotations.ObjectClassDefinition;
@ObjectClassDefinition(name = "Feed Configuration", description = "Feed Configuration")
public @interface FeedConfiguration {
	@AttributeDefinition(name = "Feed Url", description = "Feed Url")
	String feedUrl();
	@AttributeDefinition(name = "Feed Id", description = "Feed Id")
	String feedId();
}

Step 5: service implementation

Using @Designate annotation you can load the FeedConfiguration, to read the configuration properties in the class file inject the FeedConfiguration in the activate method and read the properties using config instance

package com.aem.toolkit.service;
import org.apache.sling.settings.SlingSettingsService;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.ConfigurationPolicy;
import org.osgi.service.component.annotations.Reference;
import org.osgi.service.metatype.annotations.Designate;
@Component(service = FeedService.class, configurationPolicy = ConfigurationPolicy.REQUIRE,immediate=true)
@Designate(ocd = FeedConfiguration.class)
public class FeedServiceImpl implements FeedService {
	private FeedConfiguration config;
	@org.apache.felix.scr.annotations.Reference
	private SlingSettingsService settings;
	@Activate
	public void activate(FeedConfiguration config) {
		this.config = config;
	}
	@Override
	public String getFeedUrl() {
		// TODO Auto-generated method stub
		return config.feedUrl();
	}
}

 
 

By coderss

One thought on “AEM Code Snippets : OSGI annotations – Service”
  1. I am trying to follow this , but it looks like I cannot see my service listed inside bundle unless I explicity save my osgi configuration via /system/console/configMgr. When I deploy code , osgi config configuration binding shows new or unbound configuration .Is it something to do with initializing congig object or declaring component as factory

Leave a Reply

Your email address will not be published. Required fields are marked *