AEM R6 annotation
R6 annotation allow us to add configurations to OSGI Services with the help of @Designate annotation. @Designate is must to have ocd(object class definition) property to declare service name as mentioned in below syntax.
@Designate(ocd = PracticeServiceOCDConfiguration.class)
Follow below steps to create ocd configurations and consume it as part of @Component annotation services:
- Create a configuration interface with the hep of @interface annotation and provide @ObjectClassDefinition annotation on top of the class.
Declare configuration for all the properties with the help of @AttributeDefinition annotation using name, description and type of property as shown below.
2. Create below interface named as PracticeOCDService.java having and a public method getName() and getCountries() methods.
3. Create below class PracticeOCDServiceImp.java which implements PracticeOCDService.java interface and implement getName() and getCountries() methods.
import com.javadoubts.core.services.PracticeOCDService;
import com.javadoubts.core.services.PracticeServiceOCDConfiguration;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.metatype.annotations.Designate;
@Component(service = PracticeOCDService.class, immediate = true)
@Designate(ocd = PracticeServiceOCDConfiguration.class)
public class PracticeOCDServiceImpl implements PracticeOCDService {
private String name;
private String[] supportedCountries;
@Activate
protected void activate(PracticeServiceOCDConfiguration config) {
this.name = config.name();
this.supportedCountries = config.supported_countries();
}
@Override
public String getName() {
return name;
}
@Override
public String[] getCountries() {
return supportedCountries;
}
}
Mention @Component annotation on top of the class. Declare Service interface as part of service property and add one more property immediate=true to instantiate component immediately else declare it as false to instantiate on demand.
Mention one more @Designate annotation on top of service implementation class. Declare ocd property and assign PracticeServiceOCDConfiguration.clas as value.
Override activate method to get configuration values defined as part of OCD configuration.
4. Create one Servlet(SlingServletByPathForOCD.java) to consume above created service(PracticeOCDService.java) with the help of @Reference annotation. We are going to user servlet as part of current implementation.
import com.javadoubts.core.services.PracticeOCDService;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.servlets.HttpConstants;
import org.apache.sling.api.servlets.SlingSafeMethodsServlet;
import org.osgi.framework.BundleContext;
import org.osgi.framework.Constants;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
import javax.annotation.Resource;
import javax.servlet.Servlet;
import java.io.IOException;
@Component(
service=Servlet.class,
property={
Constants.SERVICE_DESCRIPTION + "=Custom Servlet",
"sling.servlet.methods=" + HttpConstants.METHOD_POST,
"sling.servlet.paths=" + "/bin/practice/ocd"
}
)
public class SlingServletByPathForOCD extends SlingSafeMethodsServlet {
private static final long serialVersionUID = 1L;
@Reference
PracticeOCDService practiceOCDService;
@Resource(name="BundleContext")
private BundleContext context;
@Override
protected void doGet(final SlingHttpServletRequest req,
final SlingHttpServletResponse resp) throws IOException {
resp.setContentType("text/plain");
resp.getWriter().write("Hello World, " + practiceOCDService.getName() +
", Country at index 1 = "+practiceOCDService.getCountries()[1]);
}
}
5. Open System console components (http://localhost:4502/system/console/components) to verify OSGI service and Servlet successfully installation on AEM instance showing status as active.
6. Verify service and its configurations on below URL
http://localhost:4502/system/console/configMgr
7. Hit servlet URL as http://localhost:4502/bin/practice/ocd in browser to check output:
Run mode Configurations
Run mode allows us to declare environment specific properties and values.
If run mode configuration is not defined for any service, It will consider configuration values for all properties defined in java class by default.
Below is the configuration file we created for PracticeOCDServiceImp.java class.
Run mode configuration file name follows specific naming convention as <package_name>.<Service_Impl_java_class_name>.xml
e.g. com.javadoubts.core.services.impl.PracticeOCDServiceImpl.xml
Configuration File Location
Configurations created inside config folder will be available for all environments and AEM instances.
Verify service and its latest run mode configurations on below URL
http://localhost:4502/system/console/configMgr
6. Hit servlet URL as http://localhost:4502/bin/practice/ocd in browser to check output:
This time it will read values from config run mode configurations.
Imran Khan, Adobe Community Advisor, AEM certified developer and Java Geek, is an experienced AEM developer with over 11 years of expertise in designing and implementing robust web applications. He leverages Adobe Experience Manager, Analytics, and Target to create dynamic digital experiences. Imran possesses extensive expertise in J2EE, Sightly, Struts 2.0, Spring, Hibernate, JPA, React, HTML, jQuery, and JavaScript.