May 9, 2020
Estimated Post Reading Time ~

How to read config properties in AEM from ConfigMgr in console

Initially observe how to set a custom configuration in AEM here.

1)Create a sling:OsgiConfig node in crxde

Name: MyOsgi_Custom_config
Type: sling:OsgiConfig

2) To this node, we need to add our own key-value pairs

CustomProp_1 boolean myValue1
CustomProp_2 boolean myValue2

3) Save and check-in system/console/configMgr for our custom configuration.

-------And we will get this config property for our slingservlet as follows-------

package com.myApp.configProps;

import java.util.Dictionary;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.osgi.service.cm.Configuration;
import org.osgi.service.cm.ConfigurationAdmin;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@SuppressWarnings("unchecked")
@Component(metatype = false, immediate = true)
@Service
public class ConfigPropReader{

    private static final Logger log = LoggerFactory.getLogger(ConfigPropReader.class);  
    private final String CONFIG_NAME = "MyOsgi_Custom_config";
    private boolean CustomProp_1 = true;
     
    @Reference
    private ConfigurationAdmin configAdmin;

    private void readConfigProp(){
    try{
   Configuration config = (Configuration)configAdmin.getConfiguration(CONFIG_NAME);
   Dictionary props = config.getProperties();
   CustomProp_1 = ((Boolean)props.get("CustomProp_1")).booleanValue();  
   
  }catch (Exception ex) {
  System.out.println( ex.getMessage());
  }
    }
}


By aem4beginner

No comments:

Post a Comment

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