<dependency> <groupId>org.springframework.osgi</groupId> <artifactId>spring-osgi-core</artifactId> <version>1.2.1</version> </dependency> import org.osgi.framework.BundleContext; import org.springframework.osgi.context.BundleContextAware; public class MyBundleContextAwareClass implements BundleContextAware { private BundleContext bundleContext; @Override public void setBundleContext(BundleContext bundleContext) { this.bundleContext = bundleContext; } // ... } <bean id="myBundleContextAwareClass" class="com.example.MyBundleContextAwareClass"> <property name="bundleContext" ref="bundleContext" /> </bean> import org.osgi.framework.ServiceReference; public class MyServiceConsumer { private BundleContext bundleContext; public void setBundleContext(BundleContext bundleContext) { this.bundleContext = bundleContext; } public void doSomething() { ServiceReference serviceReference = bundleContext.getServiceReference(MyService.class.getName()); MyService myService = (MyService) bundleContext.getService(serviceReference); bundleContext.ungetService(serviceReference); } }


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