@Component public class MyService implements IMyService { public String sayHello() { return "Hello OSGi!"; } } @Component public class MyComponent { @Reference private IMyService myService; public void doSomething() { String message = myService.sayHello(); System.out.println(message); } } BundleContext bundleContext = FrameworkUtil.getBundle(MyComponent.class).getBundleContext(); Bundle bundle = bundleContext.installBundle("file:/path/to/mybundle.jar"); bundle.start(); Bundle[] bundles = bundleContext.getBundles(); for (Bundle bundle : bundles) { if (bundle.getSymbolicName().equals("mybundle")) { bundle.stop(); bundle.uninstall(); break; } } Bundle[] bundles = bundleContext.getBundles(); for (Bundle bundle : bundles) { if (bundle.getSymbolicName().equals("mybundle")) { bundle.update(new FileInputStream("/path/to/updatedbundle.jar")); break; } } public interface IMyExtensionPoint { void doSomething(); } @Component public class MyExtension implements IMyExtensionPoint { public void doSomething() { System.out.println("Doing something..."); } } @Component public class MyComponent { @Reference private IMyExtensionPoint myExtension; public void doSomethingWithExtension() { myExtension.doSomething(); } }


上一篇:
下一篇:
切换中文