public interface GreetingService {
String greet(String name);
}
public class GreetingServiceImpl implements GreetingService {
public String greet(String name) {
return "Hello, " + name + "!";
}
}
public class Main {
public static void main(String[] args) throws Exception {
FrameworkFactory factory = ServiceLoader.load(FrameworkFactory.class).iterator().next();
Map<String, String> config = new HashMap<>();
Framework framework = factory.newFramework(config);
framework.init();
framework.start();
BundleContext bundleContext = framework.getBundleContext();
Bundle bundle = bundleContext.installBundle("file:///path/to/bundle.jar");
bundle.start();
ServiceReference<GreetingService> serviceReference = bundleContext.getServiceReference(GreetingService.class);
GreetingService greetingService = bundleContext.getService(serviceReference);
String greeting = greetingService.greet("Alice");
System.out.println(greeting);
bundle.stop();
bundle.uninstall();
framework.stop();
framework.waitForStop(0);
}
}