How to Read OSGi Configuration in OSGI Service class not in Activator Bundle Class?



 I want to read OSGI Configuration and following this blog

@Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY, policy = ReferencePolicy.STATIC)
private ConfigurationAdmin configAdmin;
private Configuration conf = null;
private Logger logger = LoggerFactory.getLogger(ConfigurationUtilImpl.class);
public String getConfig(String pid,String key){
try {
conf = configAdmin.getConfiguration(pid);
if(null!=conf.getProperties() && null!=conf.getProperties().get(key)){
return conf.getProperties().get(key).toString();




I always get ConfigurationAdmin null.


Servlet and JSP can retrieve this his way but i need it OSGI JAVA Service.

<%@page import=""%>%@page import=""%>

Configuration conf = sling.getService("");
String myProp = (String) conf.getProperties().get("");

But I want it in OSGI service class even I can get it in start method of Bundle Activator.

private ServiceRegistration ppcService;
@Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY, policy = ReferencePolicy.DYNAMIC)
    private ConfigurationAdmin configAdmin;
    private Configuration conf = null;
public void start(BundleContext context) {"Open Web Starting bundle", context.getBundle()
        ServiceReference configurationAdminReference = context

        configAdmin = (ConfigurationAdmin) context

        if (null != configAdmin) {      

            try {
                conf = configAdmin.getConfiguration("Config PID");
                if (conf != null) {

          "****************** "

                            + conf.getProperties().get(""));


            } catch (Exception ex) {
                ex.printStackTrace();            }


Pom XML has entry for configuration admin Dependency and scr.mvn.plugin.

Any Idea?


Thanks Chandra

Accepted Solutions (1)

Accepted Solutions (1)




 BundleContext bundleContext = FrameworkUtil.getBundle(MyClass.class).getBundleContext();
     ServiceReference<?> configurationAdminReference = 
     ConfigurationAdmin confAdmin = (ConfigurationAdmin) bundleContext.getService(configurationAdminReference);

Answers (0)