Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 755275
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T15:06:40+00:00 2026-05-14T15:06:40+00:00

I am new to OSGi and came across several examples about OSGi services. For

  • 0

I am new to OSGi and came across several examples about OSGi services.

For example:

import org.osgi.framework.*;
import org.osgi.service.log.*;

public class MyActivator implements BundleActivator {
  public void start(BundleContext context) throws Exception {
    ServiceReference logRef = 
      context.getServiceReference(LogService.class.getName());
  }
}

My question is, why do you use

getServiceReference(LogService.class.getName())

instead of

getServiceReference("LogService")

If you use LogService.class.getName() you have to import the Interface. This also means that you have to import the package org.osgi.services.log in your MANIFEST.MF.

Isn’t that completely counterproductive if you want to reduce dependencies to push loose coupling? As far as I know one advantage of services is that the service consumer doesn’t have to know the service publisher. But if you have to import one specific Interface you clearly have to know who’s providing it. By only using a string like “LogService” you would not have to know that the Interface is provided by org.osgi.services.log.LogService.

What am I missing here?

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T15:06:40+00:00Added an answer on May 14, 2026 at 3:06 pm

    Looks like you’ve confused implementation and interface

    Using the actual interface for the name (and importing the interface , which you’ll end up doing anyway) reenforces the interface contract that services are designed around. You don’t care about the implemenation of a LogService but you do care about the interface. Every LogService will need to implement the same interface, hence your use of the interface to get the service. For all you know the LogService is really a wrapper around SLF4J provided by some other bundle. All you see is the interface. That’s the loose coupling you’re looking for. You don’t have to ship the interface with every implementation. Leave the interface it’s own bundle and have multiple implementations of that interface.

    Side note: ServiceTracker is usually easier to use, give it a try!

    Added benefits: Using the interface get the class name avoids spelling mistakes, excessive string literals, and makes refactoring much easier.

    After you’ve gotten the ServiceReference, your next couple lines will likely involve this:

    Object logSvc = content.getService(logRef)
    
    // What can you do with logSvc now?!? It's an object, mostly useless
    
    // Cast to the interface ... YES! Now you need to import it!
    LogSerivce logger = (LogService)logSvc;
    
    logger.log(LogService.LOG_INFO, "Interfaces are a contract between implementation and consumer/user");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am new to OSGi. Whatever tutorials i read i am unable to find
I am pretty new to the whole Java and OSGi world and I have
I'm tracking OSGi bundles like this: BundleTracker<Foo> bundleTracker = new BundleTracker<>(context, Bundle.ACTIVE, new BundleTrackerCustomizer<Foo>(){
New to PHP and MySQL, have heard amazing things about this website from Leo
i'm fairly new to osgi and am trying to get a functional proof of
I'm new whith OSGi, but it is interesting. Is it possible to interact between
For a new project I'm looking at what OSGi has to offer in terms
I'm developing OSGI bundle which uses RingFIFOBuffer from the package org.apache.commons.collections This is the
I'm currently testing out osgi (Spring DM) on a new application. The application needs
I am writing an OSGI service for JBoss7.x and I need to create a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.