public interface GreetingService {
void sayHello();
}
public class GreetingServiceImpl implements GreetingService {
public void sayHello() {
System.out.println("Hello, OSGi!");
}
public void start(BundleContext context) {
context.registerService(GreetingService.class.getName(), new GreetingServiceImpl(), null);
}
public void stop(BundleContext context) {
context.unregisterService(GreetingService.class.getName());
}
}
public class Main {
public static void main(String[] args) {
ServiceReference<GreetingService> reference = context.getServiceReference(GreetingService.class);
GreetingService service = context.getService(reference);
service.sayHello();
context.ungetService(reference);
}
}
public class MyBundleActivator implements BundleActivator {
public void start(BundleContext context) throws Exception {
System.out.println("Bundle is started.");
}
public void stop(BundleContext context) throws Exception {
System.out.println("Bundle is stopped.");
}
}