Import-Package: org.osgi.framework
Export-Package: com.example.library.module
package com.example.library.module;
public interface GreetingService {
void sayHello();
}
package com.example.library.module;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceRegistration;
public class GreetingServiceImpl implements GreetingService, BundleActivator {
private ServiceRegistration<GreetingService> registration;
@Override
public void start(BundleContext context) throws Exception {
registration = context.registerService(GreetingService.class, this, null);
}
@Override
public void stop(BundleContext context) throws Exception {
registration.unregister();
}
@Override
public void sayHello() {
System.out.println("Hello OSGi!");
}
}
package com.example.library.user;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import com.example.library.module.GreetingService;
public class GreetingUser implements BundleActivator {
private GreetingService service;
@Override
public void start(BundleContext context) throws Exception {
ServiceReference<GreetingService> reference = context.getServiceReference(GreetingService.class);
service = context.getService(reference);
service.sayHello();
}
@Override
public void stop(BundleContext context) throws Exception {
context.ungetService(service);
}
}