September 19, 2020
Estimated Post Reading Time ~

metatype=true in AEM

Why we add parameter metatype=true in @Component annotation?
When we add parameter metatype=true then Apache Felix generates a metatype.xml file for that component, if we don’t then there will no metaype.xml file.
Metatype.xml file contains an element <OCD> which has parameters name and description of the component. Under the <OCD> we have another ta <AD> which has detail information of property which we want to make visible and configurable on Apache Felix console localhost:4502/system/console/configMgr.
So if we specified ten properties in our component then there will be ten <AD> elements under the <OCD>

Metatype.xml file also contains the element <Designate> with attribute PID and <Object> which has parameter OCDREF under the <Designate> element, which represent and store the configured values. Value for PID and OCDREF is always used to the same and that is also the name of the file which you will find in the CRX.  

When we add metatype=true in the parameter of @component that means we have made the property of that component or service configurable and visible on Felix console, which is an easy and helpful way to configure properties of any component and service. Felix web console uses the meta-type information to show user-friendly and easy the GUI configuration.

interface MetaTypeService helps to obtain the meta-type information of any bundle and component. MetaType Service will examine the specified bundle for meta type documents to create the returned MetaTypeInformation object.
If the specified bundle does not contain any meta type documents, then a MetaTypeInformation object will be returned that wrappers any ManagedService or ManagedServiceFactory services registered by the specified bundle that implements MetaTypeProvider. Thus the MetaType Service can be used to retrieve meta type information for bundles that contain a meta-type document or which provide their own MetaTypeProvider objects.

Where I could find the metatype.xml file in my project? Or Where metatype.xml file exist?
[PROJECT NAME]\core\bin\target\classes\OSGI-INF\metatype

Example of a metafile.xml
<?xml version="1.0" encoding="UTF-8"?><metatype:MetaData xmlns:metatype="http://www.osgi.org/xmlns/metatype/v1.0.0" localization="OSGI-INF/metatype/com.rashid.jorvee.aem.services.MetaTypeExample">

<OCD id="com.rashid.jorvee.aem.services.MetaTypeExample" name="%com.rashid.jorvee.aem.services.MetaTypeExample.name" description="%com.rashid.jorvee.aem.services.MetaTypeExample.description">

<AD id="MetaType.userID" type="String" name="%com.rashid.jorvee.aem.services.MetaTypeExample.MetaType.userID.name" description="%com.rashid.jorvee.aem.services.MetaTypeExample.MetaType.userID.description"/>

<AD id="MetaType.contact" type=" Integer " name="%com.rashid.jorvee.aem.services.MetaTypeExample.MetaType. contact.name" description="%com.rashid.jorvee.aem.services.MetaTypeExample.MetaType.contact.description"/>

    <AD id="MetaType.address" type="String" default="Boston, united States" name="%com.rashid.jorvee.aem.services.MetaTypeExample.MetaType.address.name"         description="%com.rashid.jorvee.aem.services.MetaTypeExample.MetaType.address.description"/>

    </OCD>
    <Designate pid="com.rashid.jorvee.aem.services.MetaTypeExample">
        <Object ocdref="com.rashid.jorvee.aem.services.MetaTypeExample"/>
    </Designate>
</metatype:MetaData>

In the above example, we have created a service class MetaTypeExample which have the following configurable properties.
  1. userID
  2. contact
  3. address


By aem4beginner

No comments:

Post a Comment

If you have any doubts or questions, please let us know.